In a doubly linked list, each node contains, besides the next-node link, a second link field pointing to the previous node in the sequence. The two links may be called forward(s) and backwards, or next and prev(previous).
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *back;
node *forw;
}*list;
void add(int item)
{
node *tem=new node;
tem->info=item;
list->forw=tem;
tem->back=list;
list=tem;
cout<<"\nItem Added";
}
void remove(int item)
{
if(list==NULL)
{
cout<<"\nUnder flow :List is empty";
return;
}
node *tem=list;
while(tem!=NULL)
{
if(tem->info==item)
{
node *ltem=tem->forw;
ltem->back=tem->back;
ltem=tem->back;
ltem->forw=tem->forw;
delete tem;
cout<<"\nItem Deleted";
return;
}
tem=tem->back;
}
cout<<"\nItem not found";
}
void show()
{
node *tem=list;
cout<<"\nList Item :";
while(tem!=NULL)
{
cout<<" "<<tem->info;
tem=tem->back;
}
}
void main()
{
clrscr();
int op;
int item;
while(op!=4)
{
cout<<"\nSelect Option\n1.add\n2.remove\n3.show\n4.exit\n";
cin>>op;
switch(op)
{
case 1:
cout<<"\nEnter the item to add : ";
cin>>item;
add(item);
break;
case 2:
cout<<"\nEnter the item to remove : ";
cin>>item;
remove(item);
break;
case 3:
show();
break;
case 4:
cout<<"Press any key to exit";
break;
default:
cout<<"Invalid";
}
getch();
}
}
For More Visit : http://en.wikipedia.org/wiki/Linked_list
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *back;
node *forw;
}*list;
void add(int item)
{
node *tem=new node;
tem->info=item;
list->forw=tem;
tem->back=list;
list=tem;
cout<<"\nItem Added";
}
void remove(int item)
{
if(list==NULL)
{
cout<<"\nUnder flow :List is empty";
return;
}
node *tem=list;
while(tem!=NULL)
{
if(tem->info==item)
{
node *ltem=tem->forw;
ltem->back=tem->back;
ltem=tem->back;
ltem->forw=tem->forw;
delete tem;
cout<<"\nItem Deleted";
return;
}
tem=tem->back;
}
cout<<"\nItem not found";
}
void show()
{
node *tem=list;
cout<<"\nList Item :";
while(tem!=NULL)
{
cout<<" "<<tem->info;
tem=tem->back;
}
}
void main()
{
clrscr();
int op;
int item;
while(op!=4)
{
cout<<"\nSelect Option\n1.add\n2.remove\n3.show\n4.exit\n";
cin>>op;
switch(op)
{
case 1:
cout<<"\nEnter the item to add : ";
cin>>item;
add(item);
break;
case 2:
cout<<"\nEnter the item to remove : ";
cin>>item;
remove(item);
break;
case 3:
show();
break;
case 4:
cout<<"Press any key to exit";
break;
default:
cout<<"Invalid";
}
getch();
}
}
0 comments:
Post a Comment