|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Doble circular linked list..
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct dclinklist
{
struct dclinklist *prev; /** Stores address of previous node **/
int no;
struct dclinklist *next; /** stores address of next node **/
};
/** Redefining dclinklist as node **/
typedef struct dclinklist node;
void init(node*); /** Input function **/
void ins_aft(node*); /** Function inserting before **/
node* ins_bef(node*); /** Function inserting after **/
node* del(node*);
//node* del_front(node*);
//node* del_rear(node*);
void disp(node*);
//void ins_rear(node*);
//void ins_front(node*);
node *head;
void main()
{
char ch;
int opt, item;
static int flag=0;
head=(node*)malloc(sizeof(node));
head->next=NULL;
head->prev=NULL;
do
{
again:
printf("\nEnter your option\n");
printf("\n1. To intialise first element \n");
printf("\n2. to add at front end after first element\n");
printf("\n3. to add at rear end \n");
printf("\n4. To add before a specified node\n");
printf("\n5. To add after a specified node\n");
printf("\n6. Delete a particular node\n");
printf("\n7. Display all the nodes\n");
scanf("%d",&opt);
if(flag==0 && opt!=1)
{
printf("\nNo. You must first initialize at least one node\n");
goto again;
}
if(opt==4 && head->next==head)
{
printf("\nYou cannot delete the one and only the single node\n");
goto again;
}
if(flag==0 && opt==1)
flag=1;
switch(opt)
{
case 1:
init(head);
break;
case 2:
ins_front(head);
break;
case 3:
ins_rear(head);
break;
case 4:
head=ins_bef(head);
break;
case 5:
ins_aft(head);
break;
case 6:
head=del(head);
break;
case 7:
disp(head);
break;
case 8:
head=del_front(head);
break;
case 9:
head=del_rear(head);
break;
default:
printf("Choose correct option\n");
break;
}
printf("\nDo you wish to continue[y/n]\n");
ch=(char)getche();
}while(ch=='Y' || ch=='y');
printf("\nPress any key to exit\n");
getch();
}
/*** Function for inputing data ***/
void init(node *start)
{
start->prev=start;
printf("\nEnter a number\n");
scanf("%d",&start->no);
start->next=start;
//while(start->next!=NULL)
//start=start->next;
/* start->prev=start;*/
//start->next=start;
//node *newnode;
// newnode=(node*)malloc(sizeof(node));
//newnode=start;
//printf("\nEnter a number\n");
// scanf("%d",&newnode->no);
//newnode->next=NULL;
//newnode->prev=start;
}
/*** Function for inserting a node after a specified node ***/
void ins_aft(node *start)
{
int rno; /* number for inserting a node */
int flag=0;
node *newnode; /* New inputed node*/
node *current; /* Node for travelling the linked list*/
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the number after which you want to insert a node\n");
scanf("%d",&rno);
init(newnode);
current=start;
while(current->next!=start)
{
/*** Insertion checking for all nodes except last ***/
if(current->no==rno)
{
newnode->next=current->next;
current->next->prev=newnode;
current->next=newnode;
newnode->prev=current;
flag=1;
}
current=current->next;
}
if(flag==0 && current->next==start && current->no==rno)
{
/*** Insertion checking for last node ***/
newnode->next=current->next; /* Start is being copied */
current->next->prev=newnode;
current->next=newnode;
newnode->prev=current;
flag=1;
}
if(flag==0 && current->next==NULL)
printf("\nNo match found\n");
}
/*** Function for inserting a node before a specified node ***/
node* ins_bef(node *start)
{
int rno; /* Roll number for inserting a node*/
node *newnode; /* New inputed node */
node *current; /* Node for travelling the linked list*/
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the number before which you want to insert a node\n");
scanf("%d",&rno);
init(newnode);
current=start;
if(current->no==rno)
{
/*** Insertion checking for first node ***/
newnode->next=current;
current->prev=newnode;
while(current->next!=start)
current=current->next;
newnode->prev=current;
current->next=newnode;
start=newnode;
return(start);
}
while(current->next!=start)
{
/*** Insertion checking for all node except first ***/
if(current->next->no==rno)
{
newnode->next=current->next;
current->next->prev=newnode;
current->next=newnode;
newnode->prev=current;
return(start);
}
current=current->next;
}
/*
If the function does not return from any return statement.
There is no match to insert before the input roll number.
*/
printf("\nMatch not found\n");
return(start);
}
/*** Function for deleting a specified node ***/
node* del(node *start)
{
int rno; /* number for deleting a node*/
node *delnode; /* Node to be deleted */
node *current; /* Node for travelling the linked list*/
printf("\nEnter the number whose node you want to delete\n");
scanf("%d",&rno);
current=start;
if(current->no==rno)
{
/*** Checking condition for deletion of first node ***/
delnode=current;
while(current->next!=start)
current=current->next;
current->next=start->next;
start->next->prev=current;
start=start->next;
free(delnode);
return(start);
}
else
{
while(current->next->next!=start)
{
/*** Checking condition for deletion of ***/
/*** all nodes except first and last node ***/
if(current->next->no==rno)
{
delnode=current->next;
current->next=current->next->next;
current->next->prev=current;
free(delnode);
return(start);
}
current=current->next;
}
if(current->next->next==start && current->next->no==rno)
{
/*** Checking condition for deletion of last node ***/
delnode=current->next;
free(delnode);
current->next=start;
return(start);
}
}
printf("\nMatch not found\n");
return(start);
}
/*** Function for displaying the linked list ***/
void disp(node *start)
{
node *current; /* Node for travelling the linked list*/
current=start;
while(current->next!=start)
{
printf("\n %d",current->no);
current=current->next;
}
printf("\n %d",current->no);
}
/*** Function for inserting a node at rear end ***/
//void ins_rear(node *start)
//{
//
// node *newnode;
// node *current;
// newnode=(node*)malloc(sizeof(node));
//
// init(newnode);
// current=start;
// while(current->next!=start)
// current=current->next;
//
// current->next=newnode;
//
// newnode->next=start;
// newnode->prev=current;
//
//}
void ins_rear(node *start)
{
node *newnode;
node *current;
newnode=(node*)malloc(sizeof(node));
init(newnode);
current=start;
while(current->next!=start)
current=current->next;
current->next=newnode;
newnode->next=start;
newnode->prev=current;
}
//void ins_front(node *head)
//{
// node *newnode;
// node *cur;
// newnode=(node*)malloc(sizeof(node));
//
// init(newnode);
// head->prev=newnode;
// newnode->next=head;
//
// newnode->prev=head->prev;
//
//
//}
void ins_front(node *head)
{
int item;
node *newnode;
node *cur;
newnode=(node*)malloc(sizeof(node));
init(newnode);
/*
printf("Enter the no to be insert\n");
scanf("%d",&item);*/
cur=head->prev;
head->prev=newnode;
newnode->next=head;
newnode->prev=cur;
/*head=newnode;
return(head);*/
/*cur=head->next;
head->next=newnode;
newnode->prev=head;
newnode->next=cur;
cur->prev=newnode;*/
}
node* del_front(node *head)
{
node *cur;
node *nextnode;
cur=head->prev;
nextnode=cur->prev;
head->prev=nextnode;
nextnode->next=head;
printf("The node to be deleted is %d\n", cur->no);
free(cur);
return head;
}
node* del_rear(node *head)
{
node *cur;
node *prevnode;
if(head->prev==head)
{
printf("List is empty\n");
return head;
}
cur=head->prev;
prevnode=cur->prev;
head->prev=prevnode;
prevnode->next=head;
printf("The node to be deleted is %d\n", cur->no);
free(cur);
return head;
}
i did a progrm for doublew circular linked is.. i am not getting output for 3 fumctions.. 1)Insert_frontt 2)Delete from front emd. 3)Delete at rear en. can any one help me in regarig link list... Please thanks im advanc |
|
#2
|
|||
|
|||
|
Since you didn't define a display, how can u sure that u have logic error ?
I hope this help. |
|
#3
|
|||
|
|||
|
what exactly are you getting for the output and what output do you expect?
|
|
#4
|
||||
|
||||
|
You have the prototypes for those functions commented out. You should be getting compiler warnings, at least.
Your code is a mess. I would suggest that you clean it up before asking people to wade through it and figure out what problems you have that you didn't bother to explain properly. If you look at your own code you will see that del_front and del_rear do the same thing.
__________________
C/C++ pointers (Original in the "Commonly Asked Questions" thread). |
|
#5
|
||||
|
||||
|
A few minor points noted at a casual glance:
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF #define KINSEY (rand() % 7) λ Scheme is the Red Pill Scheme in Short • Understanding the C/C++ Preprocessor Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1 FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Doble circular linked list.. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|