Lab 08 - Linked List Part II Representation and Functions
Lab 08 - Linked List Part II Representation and Functions
Date: []
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.
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;
temp=tail;
if(head==NULL)
{
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;
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:
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.
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.