Docsity

Operations on Doubly Linked List, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
18-EE-21
Lab # 06
Title:
To implement different operations on Doubly linked list
Objective:
To perform operation on Doubly linked list
How to insert, delete node in linked list and traversing of doubly linked list
Equipment Required:
PC/ Laptop
Dev C++ software
Doubly linked list:
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 Single Linked List. Following are the important terms to understand
the concept of doubly linked list.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
Prev − Each link of a linked list contains a link to the previous link called Prev.
Linked List − A Linked List contains the connection link to the first link called First and to the
last link called Last.
Doubly Linked List Representation:
Basic Operations:
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Insert Last − Adds an element at the end of the list.
Delete Last − Deletes an element from the end of the list.
Insert After − Adds an element after an item of the list.
Delete − Deletes an element from the list using the key.
Display forward − Displays the complete list in a forward manner.
Display backward − Displays the complete list in a backward manner.
LAB TASKS
1. Write a program that insert a node at the start of doubly linked list?
1 | P a g e
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Operations on Doubly Linked List and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 06

 Title:

To implement different operations on Doubly linked list

 Objective:

 To perform operation on Doubly linked list

 How to insert, delete node in linked list and traversing of doubly linked list

 Equipment Required:

 PC/ Laptop

 Dev C++ software

 Doubly linked list:

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 Single Linked List. Following are the important terms to understand

the concept of doubly linked list.

 Link − Each link of a linked list can store a data called an element.

 Next − Each link of a linked list contains a link to the next link called Next.

 Prev − Each link of a linked list contains a link to the previous link called Prev.

 Linked List − A Linked List contains the connection link to the first link called First and to the

last link called Last.

 Doubly Linked List Representation:

 Basic Operations:

Following are the basic operations supported by a list.

 Insertion − Adds an element at the beginning of the list.

 Deletion − Deletes an element at the beginning of the list.

 Insert Last − Adds an element at the end of the list.

 Delete Last − Deletes an element from the end of the list.

 Insert After − Adds an element after an item of the list.

 Delete − Deletes an element from the list using the key.

 Display forward − Displays the complete list in a forward manner.

 Display backward − Displays the complete list in a backward manner.

LAB TASKS

1. Write a program that insert a node at the start of doubly linked list?

Function:

void double_llist::add_begin(int value) { if (start == NULL) { cout<<"\n\n\t\tFirst Create the list."<<endl; return; } struct node *temp; temp = new(struct node); temp->prev = NULL; temp->info = value; temp->next = start; start->prev = temp; start = temp; cout<<"\n\n\t\tElement Inserted Sucessfully...!!!"<<endl; }

2. Write a program that insert a node at last of doubly linked list?

3. Write a program that insert a node after a node of doubly linked list?

Note: Same Function for task # 2 and 3

Function:

void double_llist::add_after(int value, int pos) { if (start == NULL) { cout<<"\n\n\t\tFirst Create the list"<<endl; return; } struct node *tmp, *q; int i; q = start; for (i = 0;i < pos - 1;i++) { q = q->next; if (q == NULL) { cout<<"\n\n\t\tThere are less than "; cout<<pos<<" elements."<<endl; return; } } tmp = new(struct node); tmp->info = value; if (q->next == NULL) { q->next = tmp; tmp->next = NULL; tmp->prev = q; } else { tmp->next = q->next; tmp->next->prev = tmp; q->next = tmp; tmp->prev = q; } cout<<"\n\n\t\tElement Inserted Sucessfully...!!!"<<endl; }

4. Write a program that delete the first node of doubly linked list?

5. Write a program that delete last node of doubly linked list?

Menu Based program for doubly Linked list

Program code:

#include

#include<stdio.h> #include<windows.h> #include #include using namespace std; struct node { int info; struct node *next; struct node prev; }start; class double_llist { public: void create_list(int value); void add_begin(int value); void add_after(int value, int position); void delete_element(int value); void search_element(int value); void display_dlist(); void count(); void reverse(); double_llist() { start = NULL; } }; int main() { int choice, element, position; double_llist dl; while (true) { cout<<"\n\t\t>>>>>>>> Menu of Doubly Linked list <<<<<<<<"; cout<<"\n"; cout<<"\n-----------------------------------------------------------------------"; cout<<"\n\tENTER 1: Insertion node at the beginning"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 2: Insert node at given position"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 3: Displaying linked list"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 4: Total Element in linked list"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 5: Traversing linked list"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 6: Delete a Particular Node"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 7: To Exit"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\n\tPlease Enter Your Choice: "; cin>>choice; switch ( choice ) { case 1: system("cls"); cout<<"\n\n\t\tEnter the element: "; cin>>element; dl.create_list(element); Sleep(500);system("cls"); break;

case 2: system("cls"); cout<<"\n\n\t\tEnter the element: "; cin>>element; cout<<"\n\n\n\t\tEnter the postion: "; cin>>position; dl.add_after(element, position); Sleep(500);system("cls"); break; case 3: system("cls"); cout<<"\n\n\t\t"; dl.display_dlist(); Sleep(5000);system("cls"); break; case 4: system("cls"); dl.count(); Sleep(1000);system("cls");break; case 5: system("cls"); if (start == NULL) { cout<<"\n\n\t\tList is empty, Nothing to reverse.....!!!"<<endl; Sleep(1000);system("cls"); break; } dl.reverse(); dl.display_dlist(); Sleep(5000);system("cls"); break; case 6: system("cls"); if (start == NULL) { cout<<"\n\n\t\tList empty, Nothing to delete....!!!"<<endl; Sleep(1000);system("cls"); break; } cout<<"\n\n\t\tEnter the element for deletion: "; cin>>element; dl.delete_element(element); Sleep(500);cout<<"\n\n\t\tNew ";dl.display_dlist(); Sleep(5000);system("cls"); break; case 7: cout<<"\n\tExiting..........."<<endl; exit(1); default: cout<<"\n\n\t\t\aINVALID CHOICE ?? \a\n";Sleep(1000);system("cls");break; } } return 0; } void double_llist::create_list(int value) { struct node *s, *temp; temp = new(struct node); temp->info = value; temp->next = NULL; if (start == NULL) { temp->prev = NULL; start = temp;

struct node *tmp, *q; if (start->info == value) { tmp = start; start = start->next; start->prev = NULL; cout<<"\n\n\t\tElement Deleted Sucessfully...!!!"<<endl; free(tmp); return; } q = start; while (q->next->next != NULL) { if (q->next->info == value) { tmp = q->next; q->next = tmp->next; tmp->next->prev = q; cout<<"\n\n\t\tElement Deleted Sucessfully...!!!"<<endl; free(tmp); return; } q = q->next; } if (q->next->info == value) { tmp = q->next; free(tmp); q->next = NULL; cout<<"\n\n\t\tElement Deleted Sucessfully...!!!"<<endl; return; } cout<<"\n\n\t\tElement "<<value<<" not found"<<endl; } void double_llist::display_dlist() { struct node *q; if (start == NULL) { cout<<"\n\n\t\tList empty, Nothing to display...!!!"<<endl; return; } q = start; cout<<"Doubly Link List is : "; while (q != NULL) { cout<<q->info<<" <-> "; q = q->next; } cout<<"NULL"<<endl; } void double_llist::count() { struct node *q = start; int cnt = 0; while (q != NULL) { q = q->next; cnt++; } cout<<"\n\n\t\tNumber of elements are: "<<cnt<<endl; } void double_llist::reverse() {

struct node *p1, *p2; p1 = start; p2 = p1->next; p1->next = NULL; p1->prev = p2; while (p2 != NULL) { p2->prev = p2->next; p2->next = p1; p1 = p2; p2 = p2->prev; } start = p1; cout<<"\n\n\t\tList Reversed Sucessfully...!!!"<<endl; Sleep(500); cout<<"\n\n\t\tNew "; }

Program Output:

Option#1 is used to insert node at beginning of doubly linked list as shown in figure below

Option#2 is used to insert node at given position (open choice) where user want to insert of

doubly linked list as shown in figure below

Option#7 is used to Exit from the program