You are on page 1of 2

Debre Markos University

Department of Information Technology


Data Structure and Algorithm Lab Manual

Lab 5: Implementation of doubly linked list


Tools used: Quincy 2005v.1.3 editor
Objective:
Students should be able to know:
- What doubly linked lists are;
- How operations like insertion and deletion are implemented on doubly linked lists

#include<iostream.h> temp->prev = t; temp1 = current; {


struct Dlink t=temp; while(temp1->next != Dlink *temp;
{ }} NULL) temp=new Dlink;
int data; void DDeleteFront() { temp->data=item;
Dlink *next; { temp2 = temp1; temp->prev=NULL;
Dlink *prev; Dlink *temp; temp1 = temp1->next; temp->next=current;
}; if(current = = NULL) } current->prev=temp;
Dlink *current = cout<<"Empty list.\n"; temp2->next = NULL; current=temp;
NULL,*t; else delete temp1; }
void creatDlink(int d) { }} void Ddisplay()
{ temp = current; void DinsertEnd(int item) {
Dlink *temp; current = current- { Dlink *t;
temp = new Dlink; >next; Dlink *temp1,*temp2; if(current = = NULL)
temp-> data = d; current->prev = temp1=current; cout<<"Empty list.\n";
temp -> next = NULL; NULL; temp2=new Dlink; else
temp->prev=NULL; delete temp; temp2->data=item; {
if(current = = NULL) }} temp2->next=NULL; t = current;
{ void DDeleteEnd() while(temp1->next! while(t != NULL)
current = temp; { =NULL) {
t=current; Dlink *temp1,*temp2; {temp1=temp1->next;} cout<<t-> data<<" ";
} if(current = = NULL) temp1->next=temp2; t = t->next;
else cout<<"Empty list.\n"; temp2->prev=temp1; }}
{ else } cout<<endl;
t->next = temp; { void DinsertFront(int item) }
DDeleteFront();
int main() Ddisplay();
{
int m, value;
cout<<"How many elements in the doubly linked
list? "; cout<<"\nEnter an element to INSERT at the END: ";
cin>>m; cin>>value;
for(int i=1;i<=m ; i ++) cout<<"\nAfter inserting "<<value<<" at the end, it becomes: \n\t";
{ DinsertEnd(value);
cout<<"\tEnter element "<<i<<" :"; Ddisplay();
cin>>value; cout<<"\nEnter an element to INSERT at FRONT: ";
creatDlink(value); cin>>value;
} cout<<"\nAfter inserting "<<value<<" at front, it becomes: \n\t";
cout<<"\nThe doubly linked list elements are: \n\ DinsertFront(value);
t"; Ddisplay();
Ddisplay(); cout<<"\nAfter the END element is DELETED, it becomes: \n\t";
cout<<"\nWhen FRONT element is DELETED, it DDeleteEnd();
becomes:\n\t"; Ddisplay();
}

You might also like