code for double link list with data structure

Definition: A variant of a linked list in which each item has a link to the previous item as well as the next. This allows easily accessing list items backward as well as forward and deleting any item in constant time.

Also known as two-way linked list, symmetrically linked list.

Below is the simple exapmle code which will make you understand how to program it with C.

#include <iostream.h>
#include <process.h>

class doubly
{
doubly * next;
doubly * pre;
int data,ch;
public:
doubly * link(doubly * );
void traverse (doubly * );
};

doubly * doubly::link(doubly * temp)
{
doubly * newlink;
doubly * cur;
newlink = new doubly;
cout<<”\nEnter Element (only possitive number):”;
cin>>data;
cur = temp;
if(temp == NULL)
{
newlink->data = data;
newlink->pre = NULL;
newlink->next = NULL;
temp = newlink;
return temp;
}

else
{
while(cur->next != NULL)
{
cur = cur->next ;
}
newlink->data = data;
newlink->pre = cur;
newlink->next = NULL;
cur->next = newlink;
return temp;
}
}

void doubly::traverse(doubly *temp)
{
int ch;
doubly * dummy;
dummy = temp;
cout<<”\n[1] Start From Begainning\n[2] Start From End\n”;
cout<<”\nEnter Your Choice  :”;
cin>>ch;
switch(ch)
{
case 1:
if(dummy != NULL)
{
while(temp->next !=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
if (temp->next == NULL)
cout<<temp->data<<endl;
}
break;
case 2:
if(temp != NULL)
{
temp = dummy;
while(dummy->next !=NULL)
{
dummy=dummy->next;
}
while(dummy->pre !=NULL)
{
cout<<dummy->data<<endl;
dummy=dummy->pre;

}
if (dummy->pre == NULL)
cout<<dummy->data<<endl;
}
break;
}
}

void main()
{
doubly *first=NULL,d1;
int choice;
while(1)
{
cout<<”\n************** DOUBLY LINK LIST **************\n\n”;
cout<<”Choices Are :-\n=>[1] For Insert \n=>[2] For Traverse \n=>[3] For Exit”;
cout<<”\n\nEnter Your choice : “;
cin>>choice;

switch (choice)
{
case 1:
first=d1.link(first);
break;

case 2:
d1.traverse(first);
break;
case 3:
exit(0);

}

}
}

Simple Code for Link List – Data Structure

Its very first program for link list, which helps you to learn Pointer & Link List easily.

Please do not enter any Data type other then  Integer Data otherwise It’ll go in Infinite Loop.

#include <iostream.h>
#include <process.h>
class linklist
{
linklist *next;
int data;
public:
linklist * ifirst(linklist *);
void traverse(linklist *);
linklist * ilast(linklist *);
void del(linklist *);
};
linklist * linklist::ifirst(linklist *temp)
{
linklist *newlink;
newlink=new linklist;
cout<<"\nEnter Data\n";
cin>>newlink->data;
newlink->next=temp;
temp=newlink;
return temp;
}
void linklist::traverse(linklist *temp)
{
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}

}
linklist * linklist::ilast(linklist *temp)
{
linklist *newlink;
newlink=new linklist;
if (temp==NULL)
{
linklist *newlink;
newlink=new linklist;
cout<<"\nEnter Data :";
cin>>newlink->data;
newlink->next=temp;
temp=newlink;
}
else
{
linklist *cur;
cur=temp;
while(cur->next!=NULL)
{
cur=cur->next;
}
cout<<"\nEnter Data :";
cin>>newlink->data;
newlink->next=NULL;
cur->next=newlink;
}
return temp;

}
void main()
{
linklist *first=NULL,l1;
int choice;
while(1)
{
cout<<"**************** LINK LIST (Only For Integer) ****************\n\n";
cout<<"Choices Are :-\n=> [1] For Insert First\n=> [2] For Insert Last\n=> [3] For Traverse\n=> [4] For Exit";
cout<<"\n\nEnter Your choice : ";
cin>>choice;

switch (choice)
{
case 1:
first=l1.ifirst(first);
break;
case 2:
first=l1.ilast(first);
break;
case 3:
l1.traverse(first);
break;
case 4:
exit(0);

}
}
}