Data structure and Algorithm lab reports, Lab Reports of Data Structures and Algorithms

The process of performing operations on a linked list, including inserting, deleting, and traversing nodes. It provides a typical declaration of a node and explains how to insert a node at the beginning, end, or in between a linked list. The document also includes sample code in C++ for a menu-driven program that allows users to perform various operations on a linked list.

Typology: Lab Reports

2020/2021

Available from 01/15/2022

syed-afaq-hussain-shah
syed-afaq-hussain-shah 🇺🇸

14 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
18-EE-21
Lab # 05
Title:
Implementation of Insertion, Deletion and Traversing of nodes on linked list
Objective:
To perform operation linked list
How to insert, delete node in linked list and traversing of linked list
Equipment Required:
PC/ Laptop
Dev C++ software
Linked list:
Linked list is a collection of nodes, these nodes being connected to each other using pointer usually referred to as
next pointer which stores pointer to immediate next node in linked list. Last node stores NULL as next pointer and
that signifies end of list. Typical declaration of node is as follows
Any node in linked list contains data which can be anything like integer, character, or string or any other structure
itself. It also contains a pointer of same type as node type which points to node next to this node as explained above.
Typical example of linked list is shown in figure below:
Linked list operations: Insert node in linked list:
Process of adding a node into a linked list is called as insertion. There are three places where a node can be inserted
into linked list.
1. At start or head of linked list
2. At end or tail of linked list
3. In between linked list
Before going into details of placing a node, we first need to create a node. To create any node, three steps are to be
performed:
1. Allocated node
2. Set data of allocated node to value provided.
3. Set next pointer to NULL (we will modify it later when we place node in list).
1 | P a g e
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Data structure and Algorithm lab reports and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 05

 Title:

Implementation of Insertion, Deletion and Traversing of nodes on linked list

 Objective:

 To perform operation linked list

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

 Equipment Required:

 PC/ Laptop

 Dev C++ software

 Linked list:

Linked list is a collection of nodes, these nodes being connected to each other using pointer usually referred to as

next pointer which stores pointer to immediate next node in linked list. Last node stores NULL as next pointer and

that signifies end of list. Typical declaration of node is as follows

Any node in linked list contains data which can be anything like integer, character, or string or any other structure

itself. It also contains a pointer of same type as node type which points to node next to this node as explained above.

Typical example of linked list is shown in figure below:

 Linked list operations: Insert node in linked list:

Process of adding a node into a linked list is called as insertion. There are three places where a node can be inserted

into linked list.

1. At start or head of linked list

2. At end or tail of linked list

3. In between linked list

Before going into details of placing a node, we first need to create a node. To create any node, three steps are to be

performed:

1. Allocated node

2. Set data of allocated node to value provided.

3. Set next pointer to NULL (we will modify it later when we place node in list).

 Insertion at front of linked list:

When node is inserted at front, next pointer of new node created is set to the present head pointer and head

pointer is updated to new node.

Insertion in linked list at head can be summarized as follows:

1. Create a node with data and next pointer as null

2. Change next pointer of new node to point to head.

3. Change head of list, now updated to new node.

 Insertion at end or tail of linked list:

One way to insert a node at end of linked list to traverse entire list and find the last node, and then change next

pointer of last node to point to newly created list.

When node is inserted at head, every time a new node is inserted, head pointer of linked list changes. Whereas

when node is inserted at tail, one needs to scan the whole list in order to reach last node and insert new node.

But there is no way we can now current node’s previous. That means we need to traverse with two nodes, one

current and other previous which just one node behind current. Initially current is set to head and previous to NULL,

before moving current to current’ next next, previous is set to current node. When node to be deleted is found,

previous will be just one node behind, and we can link previous’s next to current’s next as

prev->next = current->next

LAB TASKS

Write a program to implement following operations of Linked list using array data structure

1. Insertion at the beginning

2. Insertion in the middle

3. Insertion in the last

4. Displaying linked list

5. Counting the number of elements of the list

6. Searching a user entered data from the list

7. Deletion of an element of list

Note: Proper menu should be defined for the easiness of the user

Program Code:

#include #include<stdio.h> #include<windows.h> #include #include using namespace std; struct node { int info; struct node next; }start; class single_llist { public: node* create_node(int); void insert_begin(); void insert_pos(); void insert_last(); void delete_pos(); void search(); void display(); void count(); single_llist() { start = NULL; } }; main () { int ch, nodes, element, position, i; single_llist sl; start = NULL; while(true) {

cout<<"\n\t\t>>>>>>>> Menu of 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: Insert node at last"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 4: Displaying linked list"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 5: Total Element in linked list"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 6: Search Element in linked list"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 7: Delete a Particular Node"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 8: To Exit"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\n\tPlease Enter Your Choice: "; cin>>ch; switch(ch) { case 1: system("cls"); sl.insert_begin();Sleep(1000);system("cls"); break; case 2: system("cls"); sl.insert_pos();Sleep(1000);system("cls"); break; case 3: system("cls"); sl.insert_last();Sleep(1000);system("cls"); break; case 4: system("cls"); sl.display();Sleep(3000);system("cls"); break; case 5: system("cls"); sl.count();Sleep(3000);system("cls"); break; case 6: system("cls"); sl.search();Sleep(3000);system("cls"); break; case 7: system("cls"); sl.delete_pos();Sleep(1000);system("cls"); break; case 8: cout<<"\n\tExiting..........."<<endl; exit(1); break; default: cout<<"\n\n\t\t\aINVALID CHOICE ?? \a\n";Sleep(1000);system("cls");break; }

start = temp; start->next = NULL; } else { ptr = start; start = temp; start->next = ptr; } } else if (pos > 1 && pos <= counter) { s = start; for (i = 1; i < pos; i++) { ptr = s; s = s->next; } ptr->next = temp; temp->next = s; } else { cout<<"\n\n\n\t\tPositon out of range......."<<endl; } } void single_llist::insert_last() { int value; cout<<"\n\n\n\t\tEnter the value to be inserted: "; cin>>value; struct node *temp, *s; temp = create_node(value); s = start; while (s->next != NULL) { s = s->next; } temp->next = NULL; s->next = temp; cout<<"\n\n\t\tElement Inserted at last Sucessfully...."<<endl; } void single_llist::display() { struct node *temp; if (start == NULL) { cout<<"\n\n\n\t\tThe List is Empty......!!!"<<endl; return; } temp = start; cout<<"\n\n\n\t\tElements of list are: "; while (temp != NULL) { cout<<temp->info<<"->"; temp = temp->next; } cout<<"NULL"<<endl;

void single_llist::search() { int value, pos = 0; bool flag = false; if (start == NULL) { cout<<"\n\n\n\t\tList is empty"<<endl; return; } cout<<"\n\n\n\t\tEnter the value to be searched: "; cin>>value; struct node *s; s = start; while (s != NULL) { pos++; if (s->info == value) { flag = true; cout<<"\n\n\n\t\tElement "<<value<<" is found at position "<<pos<<endl; } s = s->next; } if (!flag) cout<<"\n\n\n\t\tElement "<<value<<" not found in the list"<<endl; } void single_llist::delete_pos() { int pos, i, counter = 0; if (start == NULL) { cout<<"\n\n\n\t\tList is empty"<<endl; return; } cout<<"\n\n\n\t\tEnter the position of value to be deleted: "; cin>>pos; struct node *s, *ptr; s = start; if (pos == 1) { start = s->next; } else { while (s != NULL) { s = s->next; counter++; } if (pos > 0 && pos <= counter) { s = start; for (i = 1;i < pos;i++) { ptr = s; s = s->next; }

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

Option#2 is used to insert node at given position as shown in figure below

Option#3 is used to insert node at the end of linked list as shown in figure below

Option#4 is used to Display the linked list as shown in figure below

Option#5 is used to Display the Total Element in Linked list as shown in figure below

Option#6 is used to Search Element in Linked list as shown in figure below

Option#7 is used to Deleted Element in Linked list as shown in figure below

Option#8 is used to Exit from the program