Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment -1.3
Student Name: Gulshan Kumar Singh UID: 21BCS2858
Branch: CSE Section/Group: 608-B
Semester: 3rd Date of Performance: 10/09/2022
Subject Name: Data Structure Subject Code: 21CSH211

Aim of the practical: Write a menu driven program that maintains a


linear linked list whose elements are stored in on ascending order and
implements the following operations (using separate functions):
a) Insert a new element
b) Delete an existing element
c) Search an element
d) Display all the elements

Algorithm:
1. Start.
2. Declare global and local variables for the menu-driven
program.
3. Use of for loop and if-else statements for working of
operation like Insert, Delete, Locate and Display.
4. Use while loop for iteration and declare Switch-case
statements to execute the block of codes.
5. Stop

System Requirements: Online gdb compiler, C++ compiler


DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Program code:
#include <iostream>
using namespace std;
struct Node
{
int info;
Node *link;
}*start;
void insert();
void display();
void search();
void delete_element();
int main()
{
int ch,
flag=0;
start=NULL;
while(flag!=1)
{ cout<<"\nENTER YOUR CHOICE \n";
cout<<"1)INSERT A NEW ELEMENT\n";
cout<<"2)DELETE AN ELEMENT\n";
cout<<"3)SEARCH AN ELEMENT\n";
cout<<"4)DISPLAY ELEMENT\n";
cout<<"5)Exit:\n";
cin>>ch;
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

switch (ch)
{
case 1:
insert();
break;
case 2:
delete_element();
break;
case 3:
search();
break;
case 4:
display();
break;
case 5:
flag=1;
break;
default:
cout<<"invalid choice"<<endl;
break;
}
}
}
void insert()
{
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Node *temp=new Node;


cout<<"enter info";
cin>>temp->info;
temp->link=NULL;
if(start==NULL)
{
start=temp;
return;
}
else
{
struct Node *p=start;
while(p->link!=NULL)
{
p=p->link;
}
p->link=temp;
}
}
void display()
{
struct Node* ptr=start;
cout<<"elements are"<<endl;
while(true)
{
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

cout<<ptr->info<<endl;
if(ptr->link==NULL)
{
break;
}
ptr=ptr->link;
}
}
void delete_element()
{
struct Node *prev=NULL;
struct Node *end=start;
while(end->link)
{
prev=end;
end=end->link;
}
prev->link=NULL;
delete end;
cout<<"ELEMENT DELETED"<<endl;
}
void search()
{
int elem,fg=0;
struct Node *current=start;
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

cout<<"enter element to be searched"<<endl;


cin>>elem;
while(current!=NULL)
{
if(current->info==elem)
{
fg=1;
break;
}
current=current->link;
}
if(fg==1)
{
cout<<"ELEMENT FOUND"<<endl;
}
else
{
cout<<"ELEMENT NOT FOUND"<<endl;
}
}
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Output:
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Learning outcomes (What I have learnt):


1. Analyze and Compare searching and sorting techniques.
2. Understand the concept of Linked lists and their algorithms.
3. Evaluate appropriate data structure while designing the
algorithms.
4. Identifying the basic concept of loops and conditional
statements
5. Identify the basic operations like insertion, deletion and
traversing on data structures.

You might also like