Professional Documents
Culture Documents
LAB REPORT#04
Submitted by:
Mahnoor Inam
18-CE-009
Lab. Instructor
Tauqeer Anjum
Experiment # 04
Operations on Doubly Linked List
Objectives:
The objective of this lab is to understand the various operations on Doubly linked list in C++.
Software Tools:
Theory:
Doubly linked list is a type of linked list in which each node apart from storing its data has two
links. The first link points to the previous node in the list and the second link points to the next
node in the list.
Lab Task
Perform the following operations in doubly linked list
Insertion at the Start
Insertion at the Middle
Insertion at the End
Deletion at the start
Deletion at the Middle
Deletion at the end
Display forward linked list
Display backward linked list
Input
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
node *prev;
};
class list
{
private:
node *head, *tail;
public:
list()
{
head = NULL;
tail = NULL;
}
void insertEnd(int value)
{
node *temp = new node;
temp->data = value;
temp->next = NULL;
if(head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next= temp;
temp->prev=tail;
tail = temp;
}
}
void display()
{
node *current= new node;
current = head;
while(current->next != NULL)
{
cout<<current->data<<"\t";
current = current->next;
}
}
void displayrev()
{
node *temp= new node;
temp=tail;
while(temp!=NULL)
{ temp = temp->prev;
cout<<temp->data<<"\t";
}
}
~list()
{
cout << "\n\n Delete End Link List";
node *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
cout << "\n Deleted";
}
};
int main()
{
list obj;
obj.insertEnd(10);
obj.insertEnd(6);
obj.insertEnd(9);
obj.insertEnd(4);
obj.insertEnd(2);
obj.insertEnd(40);
cout << "\nInsert at end: \n";
obj.display();
cout<<endl;
cout<<"forward display:\n";
obj.display();
cout<<endl;
cout<<"backward display:\n";
obj.displayrev();
cout<<endl;
return 0;
}
Output:
Concusion
In this lab report we learn about doubly link list and perform different operations on it.