double link list

the previous post I have made an example of circular link list program, at this time I make a duble program link list, you have to understand is the concept of circular link list.
The following example program, please be run

 

#include<stdio.h>
#include<conio.h>
#include<iostream>

//using namespace std;
struct node{
int data;
struct node *prev,*next,*info;
};
typedef struct node node;
node *head,*last,*temp,*t,*p,search,item;
int d;
void addhead();
void addmiddle();
void addtail();
void delhead();
void delmiddle();
void deltail();
int Search();
void disp();


int main(){
int ch;
while(1){
printf(“\n1. add to head “);
printf(“\n2. add to Middle “);
printf(“\n3. add to tail “);
printf(“\n4. Delete from head “);
printf(“\n5. Delete from Middle “);
printf(“\n6. Delete from tail “);
printf(“\n7.earch”);
printf(“\n8. Exit”);
printf(“\nEnter your Choice : “);
scanf(“%d”,&ch);
switch(ch){
case 1:
addhead();
disp();
break;
case 2:
addmiddle();
disp();
break;
case 3:
addtail();
disp();
break;
case 4:
delhead();
disp();
break;
case 5:
delmiddle();
disp();
break;
case 6:
deltail();
disp();
break;
case 7:
Search();
disp();
break;
case 8:
exit(0);
default:
printf(“\nInvalid Choice”);
}
getch();
}
}

void addhead()
{
temp=(node*)malloc(sizeof(node));
printf(“\nEneter the Data : “);
scanf(“%d”,&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}
void addmiddle()
{
int d;
temp=(node*)malloc(sizeof(node));
printf(“\nEneter the Data : “);
scanf(“%d”,&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
t=head;
printf(“\nEnter the node after which insertion to be made : “);
scanf(“%d”,&d);
while(t!=NULL)
{
if(t->data==d)
{
temp->next=t->next;
temp->prev=t;
t->next=temp;
return;}
else
t=t->next;
}
printf(“\nadd node not found”);
}
}
void addtail()
{
node *t;
temp=(node*)malloc(sizeof(node));
printf(“\nEneter the Data : “);
scanf(“%d”,&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
t=head;
while(t->next!=NULL)
t=t->next;
t->next=temp;
temp->prev=t;
}
}
void delhaed()
{
if(head==NULL)
printf(“\nList is Empty”);
else
{
t=head;
printf(“\nDeleted node is %d\n”,t->data);
head=head->next;
head->prev=NULL;
free(t);
}
}
void delmiddle()
{
int d;
node *s,*n;
if(head==NULL)
printf(“\nList is Empty”);
else
{
printf(“\nEnter the node data to be deleted : “);
scanf(“%d”,&d);
if(head->data==d)
{
t=head;
head=head->next;
head->prev=NULL;
printf(“\nDeleted node is %d\n”,t->data);
free(t);
}
else
{
t=head;
while(t->next!=NULL)
{
if(t->data==d){
s=t;
printf(“\nDeleted node is %d\n”,s->data);
p=t->prev;
n=t->next;
p->next=t->next;
n->prev=p;
free(s);
}
else
{
p=p->next;
t=t->next;
}
}
}
} }
void deltail()
{
if(head==NULL)
printf(“\nList is Empty”);
else if(head->next==NULL)
{
t=head;
printf(“\nDeleted node is %d\n”,t->data);
head=NULL;
}
else
{
t=head;
while(t->next!=NULL)
{
t=t->next;
}
p=t->prev;
p->next=NULL;
printf(“\nDeleted node is %d\n”,t->data);
free(t);
}
}
//search

int Search()
{
while(head!=NULL)
{
if(head->info=item){ // if the values match,
//return head; // return the matching node.
head=head->next; }// otherwise, move on
}
system(“pause”);
return 0;
}

 

Please try🙂
if there is an error please leave a message.
thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: