0% found this document useful (0 votes)
9 views

Lab 08 - Linked List Part II Representation and Functions

The document discusses linked lists and provides examples of implementing doubly linked lists and circular linked lists in C++. It begins with an introduction to doubly linked lists and circular linked lists. The objectives are then stated as understanding how to implement doubly linked lists and circular linked lists using structures in C++. Examples are given of implementing various operations on a doubly linked list like insertion and deletion at the front and rear. Practice questions are provided to add functions for middle insertion/deletion. Students are tasked with implementing a circular singly linked list with various operations and optionally a circular doubly linked list.

Uploaded by

amikla911422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 08 - Linked List Part II Representation and Functions

The document discusses linked lists and provides examples of implementing doubly linked lists and circular linked lists in C++. It begins with an introduction to doubly linked lists and circular linked lists. The objectives are then stated as understanding how to implement doubly linked lists and circular linked lists using structures in C++. Examples are given of implementing various operations on a doubly linked list like insertion and deletion at the front and rear. Practice questions are provided to add functions for middle insertion/deletion. Students are tasked with implementing a circular singly linked list with various operations and optionally a circular doubly linked list.

Uploaded by

amikla911422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Computer Science Department

[0921-211]: [Data Structure and Algorithms]


Section: [Male/Female]

Lab [08]: [Linked List Part 2: representation and functions


(Doubly and Circular)]

Date: []

[0921-211]: [Data Structure and Algorithms] 1|Page


Introduction
Doubly Linked List is a variation of Linked list in which navigation is possible in both ways,
either forward and backward easily as compared to Singly Linked List. In Doubly Linked List,
each node consists of three parts: data, pointer to the next node in sequence (next pointer), and
pointer to the previous node (previous pointer). The first node of the list has its previous link
pointing to NULL similarly the last node of the list has its next node pointing to NULL.

Circular Linked List is a variation of Linked list where all nodes are connected to form a circle.
There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly
circular linked list.

Objectives
At the end of this lab session the students should be able to:
1- Implement Doubly Linked List using structure.
2- Implement different Doubly Linked List operations:
a. Insert at front
b. Insert at rear.
c. Delete from front.
d. Delete from rear.
e. Print the list in forward direction.
f. Print the list in backward direction
3- Implement Circular Linked List using structure.

Tools/Software Requirement
- Dev C++ IDE

Description

This lab manual contains details on how to implement Doubly Linked List using structure in C+
+. The lab manual is divided into 2 parts:
Part 1: Examples
- Students should go through the given examples.
- Use Dev C++ IDE to run the code.
Part 2: Lab Tasks
- Students should solve the given programming problem using stack.
- Use Dev C++ IDE to write your program.
Deliverables:
Students will be given an answer sheet and do the following.
1. Paste your code in column A and the output screen shot in column B.
2. Once finished upload your answer sheet in Blackboard.
3. Note: Do not forget to type your name and ID number in the answer sheet.

[0921-211]: [Data Structure and Algorithms] 2|Page


Part 1: Examples
A program that stores Employees Information implemented using Doubly Linked List is given as
follows:
A. Declaring Structure
struct Employee{
string name;
int id;
Employee *prev, *next;
};
Employee *temp=NULL, *head=NULL, *tail=NULL *cur=NULL;

B. Insert at Front
void InsertFront()
{
temp = new Employee;
cout<<"Enter Employee Name:\n";
cin>>temp->name;
cout<<"Enter Employee ID:\n";
cin>>temp->id;
temp->prev=NULL;
temp->next=NULL;

if (head==NULL)
{
head=temp;
tail=temp;
}
else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}

C. Insert at Rear
void InsertRear()
{
temp = new Employee;
cout<<"Enter Employee Name:\n";
cin>>temp->name;
cout<<"Enter Employee ID:\n";
cin>>temp->id;
temp->prev=NULL;
temp->next=NULL;

[0921-211]: [Data Structure and Algorithms] 3|Page


if (head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
temp->prev=tail;
tail=temp;
}
}

D. Delete from Front


void DeleteFront()
{
temp = head;
if(head==NULL)
{
cout<<"List is Empty!!\n";
}
else
{
if(head->next==NULL)
{
head=NULL;
tail=NULL;
}
else
{
head = head->next;
head->prev = NULL;
}
cout<<"Deleted:"<<temp->name << " " <<temp->id <<"\n";
delete temp;
}
cout<<endl;
}

E. Delete from Rear


void DeleteRear()
{

temp=tail;

if(head==NULL)
{

[0921-211]: [Data Structure and Algorithms] 4|Page


cout<< "List is Empty!!\n";
}

else
{
if(head->next==NULL)
{
head=NULL;
tail=NULL;
}
else
{
tail=tail->prev;
tail->next=NULL;
}
cout<<"Deleted:"<<temp->name << " " <<temp->id <<"\n";
delete temp;
}
cout<<endl;
}

F. Print Forward
void PrintForward()
{
cur=head;

if(head==NULL)
{
cout<<"List is Empty!!\n";
}
else
{
cout<<"List display: \n";
while(cur!=NULL)
{
cout<< cur->name <<" " <<cur->id <<"\n";
cur=cur->next;
}
cout<<endl;
}
}

G. Print Backward
void PrintBackward()
{
cur=tail;

[0921-211]: [Data Structure and Algorithms] 5|Page


if(head==NULL)
{
cout<<"List is Empty!!\n";
}
else
{
cout<<"List display: \n";
while(cur!=NULL)
{
cout<< cur->name <<" " <<cur->id <<"\n";
cur=cur->prev;
}
cout<<endl;
}
}

H. Driver Code
int main(){
int choice;

do{
cout << "1: Insert item at front\n";
cout << "2: Insert item at rear\n";
cout << "3: Delete item from front\n";
cout << "4: Delete item from rear\n";
cout << "5: Print List in forward direction\n";
cout << "6: Print List in backward direction\n";
cout << "7: Exit\n";
cout << "Enter your choice: \n";
cin>>choice;

switch (choice){
case 1:
InsertFront();
break;
case 2:
InsertRear();
break;
case 3:
DeleteFront();
break;
case 4:
DeleteRear();
break;
case 5:
PrintForward();
break;
case 6:

[0921-211]: [Data Structure and Algorithms] 6|Page


PrintBackward();
break;

case 7:
cout<<"Exiting Program\n";
break;
default:
cout<<"Error! wrong choice\n";
}
}while (choice!=7);

return 0;
}

Practice Questions:
1- Add a function in the program above to insert a new node at the middle.
2- Add a function in the program above to delete a new node from the middle.

Part 2: Lab Tasks (10 points)


Note: Copy this section into a new word file then save it. You will only submit this section of the
lab manual.

- Implement a Circular Singly Linked List. Providing the following operations:


a. Insert at front
b. Insert at rear.
c. Delete from front.
d. Delete from rear.
e. Print the list in forward direction.

Bonus Question
- Implement a Circular Doubly Linked List. Providing the following operations:
a. Insert at front
b. Insert at rear.
c. Delete from front.
d. Delete from rear.
e. Print the list in forward direction.
f. Print the list in backward direction.

[0921-211]: [Data Structure and Algorithms] 7|Page


[0921-211]: [Data Structure and Algorithms] 8|Page

You might also like