UNKNOWN //************************************** //INCLUDE files for :two-way linked list //************************************** #include <iostream.h> #include <conio.h> #include <stdlib.h> #include <alloc.h> //************************************** // Name: two-way linked list // Description:this is the implementation of two-way linked list in oop technology. if u are using the code for any purpose do favour me and vote for this code . // By: jetendar dherwani // // // Inputs:no // // Returns:no // //Assumes:u must be aware of by values passed in the functions by reference . a little bit of knowledge of two-way list // //Side Effects:no //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.3320/lngWId.3/qx/vb/scripts/ShowCode.htm //for details. //************************************** //this is the implementation of two way linked list //created by jetendar dherwani //concepts given by sir faheem qureshi //in simple linked list i.e one way list we have only one pointer //that helps us moving in forward direction(only forward not backward) . //this is the biggest flaw in simple linked list . //this flaw is covered or u can say removed in two-way list . //we have the same clas student through which we will perform some experiments #include <iostream.h> #include <conio.h> #include <alloc.h> #include <stdlib.h> class student { public: int data; student *next;//this will help in moving forward student *previous ; //this will help in moving backward public : student(void); void insert_beg(int); void insert_end(int,student *&); void insert_middle(int loc1,int element,student *&); void insert_byelement(int data,student *&); void del_beg(void); void del_end(student *&); void del_middle(int loc,student *&); int count(); void traverse(void); }; void student::del_middle(int loc,student *&end;) { student *ptr; ptr=next; for(int i=1;i&lt;=loc-1;i++) ptr=ptr-&gt;next; if(loc==count() ) { end=ptr-&gt;previous; ptr-&gt;previous-&gt;next=NULL; } else { ptr-&gt;next-&gt;previous=ptr-&gt;previous; ptr-&gt;previous-&gt;next=ptr-&gt;next; } } void student::del_end(student *&final;) { final-&gt;previous-&gt;next=NULL; final=final-&gt;previous-&gt;previous; } void student ::insert_byelement(int element,student *&end;) { student *ptr,*temp; ptr=next; while(1) { if(ptr-&gt;data &gt;= element) { temp=new student(); temp-&gt;data=element; temp-&gt;next=ptr; temp-&gt;previous=ptr-&gt;previous; (temp-&gt;previous)-&gt;next=temp; ptr-&gt;previous=temp; break; } else { if(ptr-&gt;next==NULL) { temp=new student(); temp-&gt;data=element; ptr-&gt;next=temp; temp-&gt;previous=ptr; end=ptr-&gt;next; break; } } ptr=ptr-&gt;next; } } void student::insert_middle(int loc,int element,student *&end;) { student *ptr,*temp; ptr=next; for(int i=1;i&lt;=loc-1;i++) ptr=ptr-&gt;next; if(ptr-&gt;next==NULL) { temp=new student(); ptr-&gt;next=temp; temp-&gt;previous=ptr; temp-&gt;data=element; end=temp; } else { temp=new student(); temp-&gt;data=element; temp-&gt;next=ptr-&gt;next; temp-&gt;previous=ptr; ptr-&gt;next=temp; temp-&gt;next-&gt;previous=temp; } } void student::del_beg(void) { student *ptr; ptr=next; ptr-&gt;next-&gt;previous=ptr-&gt;previous; next=ptr-&gt;next; } void student ::insert_end(int element,student *&ending;) { student *newptr,*ptr; ptr=ending; newptr=new student(); newptr-&gt;data=element; newptr-&gt;previous=ptr; ptr-&gt;next=newptr; ending=newptr; } void student::insert_beg(int element) { student *ptr,*newptr; ptr=next; newptr=new student(); newptr-&gt;previous=ptr-&gt;previous; newptr-&gt;data=element; newptr-&gt;next=ptr; next=newptr; ptr-&gt;previous=newptr; } student::student(void) { data=-1; next=NULL; previous=NULL; } void student ::traverse(void) { student *ptr; ptr=next; while(ptr!=NULL) { cout&lt;&lt;'\n'&lt;<ptr->data; ptr=ptr-&gt;next; } } int student::count(void) { int count=0; student *ptr; ptr=next; while(ptr!=NULL) { count=count+1; ptr=ptr-&gt;next; } return(count); } void main(void) { student *start,*end,*list; //we will need two pointers to point starting element of the list //and another to point to ending element of the list //and list is a generator clrscr(); list=new student(); start=list; end=list; list-&gt;insert_end(2,end); list-&gt;insert_end(3,end); list-&gt;insert_end(5,end); list-&gt;traverse(); cout&lt;&lt;'\n'&lt;&lt;"THE TOTAL NUMBER OF ELEMENTS ARE "&lt;<start->count(); list-&gt;insert_beg(0); list-&gt;traverse(); getch(); list-&gt;del_beg(); list-&gt;traverse(); getch(); list-&gt;insert_middle(3,1,end); list-&gt;traverse(); getch(); list-&gt;insert_byelement(1,end); list-&gt;insert_byelement(9,end); list-&gt;traverse(); getch(); cout&lt;&lt;'\n'&lt;&lt;"nnnnn"; getch(); list-&gt;del_end(end); list-&gt;traverse(); getch(); list-&gt;del_middle(5,end); list-&gt;traverse(); getch(); list-&gt;insert_end(9,end); list-&gt;traverse(); }