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

DS TRHU C++ PROGRAMS-2

The document outlines the implementation of a singly linked list in C++, detailing various operations such as insertion, deletion, updating, searching, sorting, and displaying elements. It includes a main function that provides a menu for user interaction to perform these operations. The code demonstrates handling of edge cases like empty lists and invalid positions for insertion and deletion.
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)
3 views

DS TRHU C++ PROGRAMS-2

The document outlines the implementation of a singly linked list in C++, detailing various operations such as insertion, deletion, updating, searching, sorting, and displaying elements. It includes a main function that provides a menu for user interaction to perform these operations. The code demonstrates handling of edge cases like empty lists and invalid positions for insertion and deletion.
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/ 115

ROLL NO: EXP.

NO: DATE:

Experiment 1. Implementation of Singly linked list

Procedure:

#include<iostream>
#include<cstdio>
#include<cstdlib>
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 sort();
void search();
void update();
void reverse();
void display();
single_llist()
{
start = NULL;
}
};
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;

1
ROLL NO: EXP.NO: DATE:

cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<endl;
cout<<"3.Insert node at position"<<endl;
cout<<"4.Sort Link List"<<endl;
cout<<"5.Delete a Particular Node"<<endl;
cout<<"6.Update Node Value"<<endl;
cout<<"7.Search Element"<<endl;
cout<<"8.Display Linked List"<<endl;
cout<<"9.Reverse Linked List "<<endl;
cout<<"10.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Sort Link List: "<<endl;
sl.sort();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node: "<<endl;
sl.delete_pos();
break;
case 6:
cout<<"Update Node Value:"<<endl;
2
ROLL NO: EXP.NO: DATE:

sl.update();
cout<<endl;
break;
case 7:
cout<<"Search element in Link List: "<<endl;
sl.search();
cout<<endl;
break;
case 8:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 9:
cout<<"Reverse elements of Link List"<<endl;
sl.reverse();
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
node *single_llist::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
3
ROLL NO: EXP.NO: DATE:

}
}
void single_llist::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}

void single_llist::insert_last()
{
int value;
cout<<"Enter 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<<"Element Inserted at last"<<endl;
}

void single_llist::insert_pos()
4
ROLL NO: EXP.NO: DATE:

{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
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
5
ROLL NO: EXP.NO: DATE:

{
cout<<"Positon out of range"<<endl;
}
}
void single_llist::sort()
{
struct node *ptr, *s;
int value;
if (start == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
ptr = start;
while (ptr != NULL)
{
for (s = ptr->next;s !=NULL;s = s->next)
{
if (ptr->info > s->info)
{
value = ptr->info;
ptr->info = s->info;
s->info = value;
}
}
ptr = ptr->next;
}
}
void single_llist::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
6
ROLL NO: EXP.NO: DATE:

{
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;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}

/*
* Update a given Node
*/
void single_llist::update()
{
int value, pos, i;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the node postion to be updated: ";
7
ROLL NO: EXP.NO: DATE:

cin>>pos;
cout<<"Enter the new value: ";
cin>>value;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start->info = value;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<<pos<<" elements";
return;
}
s = s->next;
}
s->info = value;
}
cout<<"Node Updated"<<endl;
}

/*
* Searching an element
*/
void single_llist::search()
{
int value, pos = 0;
bool flag = false;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the value to be searched: ";
cin>>value;
struct node *s;
s = start;
8
ROLL NO: EXP.NO: DATE:

while (s != NULL)
{
pos++;
if (s->info == value)
{
flag = true;
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
}
s = s->next;
}
if (!flag)
cout<<"Element "<<value<<" not found in the list"<<endl;
}

/*
* Reverse Link List
*/
void single_llist::reverse()
{
struct node *ptr1, *ptr2, *ptr3;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (start->next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
9
ROLL NO: EXP.NO: DATE:

start = ptr2;
}

/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}

$ g++ singlelinkedlist.cpp
$ a.out

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element

10
ROLL NO: EXP.NO: DATE:

8.Display Linked List


9.Reverse Linked List
10.Exit
Enter your choice : 8
Display elements of link list
The List is Empty.

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 5
Delete a particular node:
List is empty

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
11
ROLL NO: EXP.NO: DATE:

9.Reverse Linked List


10.Exit
Enter your choice : 6
Update Node Value:
List is empty

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 7
Search element in Link List:
List is empty

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
12
ROLL NO: EXP.NO: DATE:

10.Exit
Enter your choice : 3
Inserting Node at a given position:
Enter the value to be inserted: 1010
Enter the postion at which node to be inserted: 5
Positon out of range

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 1
Inserting Node at Beginning:
Enter the value to be inserted: 100
Element Inserted at beginning

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
13
ROLL NO: EXP.NO: DATE:

8.Display Linked List


9.Reverse Linked List
10.Exit
Enter your choice : 1
Inserting Node at Beginning:
Enter the value to be inserted: 200
Element Inserted at beginning

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 8
Display elements of link list
Elements of list are:
200->100->NULL

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
14
ROLL NO: EXP.NO: DATE:

8.Display Linked List


9.Reverse Linked List
10.Exit
Enter your choice : 2
Inserting node at last:
Enter the value to be inserted: 50
Element Inserted at last

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 2
Inserting node at last:
Enter the value to be inserted: 150
Element Inserted at last

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
15
ROLL NO: EXP.NO: DATE:

8.Display Linked List


9.Reverse Linked List
10.Exit
Enter your choice : 8
Display elements of link list
Elements of list are:
200->100->50->150->NULL

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 3
Inserting node at a given position:
Enter the value to be inserted: 1111
Enter the position at which node to be inserted: 4

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
16
ROLL NO: EXP.NO: DATE:

8.Display Linked List


9.Reverse Linked List
10.Exit
Enter your choice : 8
Display elements of link list
Elements of list are:
200->100->50->1111->150->NULL

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 3
Inserting node at a given position:
Enter the value to be inserted: 1010
Enter the position at which node to be inserted: 100
Position out of range

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
17
ROLL NO: EXP.NO: DATE:

7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 8
Display elements of link list
Elements of list are:
200->100->50->1111->150->NULL

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 5
Delete a Particular node:
Enter the position of value to be deleted: 1

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
18
ROLL NO: EXP.NO: DATE:

8.Display Linked List


9.Reverse Linked List
10.Exit
Enter your choice : 8
Display elements of link list
Elements of list are:
100->50->1111->150->NULL

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 6
Update Node Value:
Enter the node position to be updated: 1
Enter the new value: 1010
Node Updated

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
19
ROLL NO: EXP.NO: DATE:

7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 8
Display elements of link list
Elements of list are:
1010->50->1111->150->NULL

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 7
Search element in Link List:
Enter the value to be searched: 50
Element 50 is found at position 2

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
20
ROLL NO: EXP.NO: DATE:

6.Update Node Value


7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 9
Reverse elements of Link List

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 8
Display elements of link list
Elements of list are:
150->1111->50->1010->NULL

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
21
ROLL NO: EXP.NO: DATE:

8.Display Linked List


9.Reverse Linked List
10.Exit
Enter your choice : 4
Sort Link List:

---------------------------------

Operations on singly linked list

---------------------------------
1.Insert Node at beginning
2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Experiment 2.

Implementation of Doubly linked list.

* C++ Program to Implement Doubly Linked List


*/
Procedure:

#include<iostream>
#include<cstdio>
#include<cstdlib>
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
struct node *next;

22
ROLL NO: EXP.NO: DATE:

struct node *prev;


}*start;

/*
Class Declaration
*/
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;
}
};

/*
* Main: Conatins Menu
*/
int main()
{
int choice, element, position;
double_llist dl;
while (1)
{
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<"Operations on Doubly linked list"<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1.Create Node"<<endl;
cout<<"2.Add at begining"<<endl;
cout<<"3.Add after position"<<endl;
cout<<"4.Delete"<<endl;
cout<<"5.Display"<<endl;
cout<<"6.Count"<<endl;
23
ROLL NO: EXP.NO: DATE:

cout<<"7.Reverse"<<endl;
cout<<"8.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch ( choice )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.create_list(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element: ";
cin>>element;
dl.add_begin(element);
cout<<endl;
break;
case 3:
cout<<"Enter the element: ";
cin>>element;
cout<<"Insert Element after postion: ";
cin>>position;
dl.add_after(element, position);
cout<<endl;
break;
case 4:
if (start == NULL)
{
cout<<"List empty,nothing to delete"<<endl;
break;
}
cout<<"Enter the element for deletion: ";
cin>>element;
dl.delete_element(element);
cout<<endl;
break;
case 5:
dl.display_dlist();
cout<<endl;
break;
24
ROLL NO: EXP.NO: DATE:

case 6:
dl.count();
break;
case 7:
if (start == NULL)
{
cout<<"List empty,nothing to reverse"<<endl;
break;
}
dl.reverse();
cout<<endl;
break;
case 8:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}

/*
* Create Double Link List
*/
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;
}
else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
25
ROLL NO: EXP.NO: DATE:

temp->prev = s;
}
}

/*
* Insertion at the beginning
*/
void double_llist::add_begin(int value)
{
if (start == NULL)
{
cout<<"First 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<<"Element Inserted"<<endl;
}

/*
* Insertion of element at a particular position
*/
void double_llist::add_after(int value, int pos)
{
if (start == NULL)
{
cout<<"First 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)
26
ROLL NO: EXP.NO: DATE:

{
cout<<"There 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<<"Element Inserted"<<endl;
}

/*
* Deletion of element from the list
*/
void double_llist::delete_element(int value)
{
struct node *tmp, *q;
/*first element deletion*/
if (start->info == value)
{
tmp = start;
start = start->next;
start->prev = NULL;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = start;
27
ROLL NO: EXP.NO: DATE:

while (q->next->next != NULL)


{
/*Element deleted in between*/
if (q->next->info == value)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = q->next;
}
/*last element deleted*/
if (q->next->info == value)
{
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}
cout<<"Element "<<value<<" not found"<<endl;
}

/*
* Display elements of Doubly Link List
*/
void double_llist::display_dlist()
{
struct node *q;
if (start == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}
q = start;
cout<<"The Doubly Link List is :"<<endl;
while (q != NULL)
{
28
ROLL NO: EXP.NO: DATE:

cout<<q->info<<" <-> ";


q = q->next;
}
cout<<"NULL"<<endl;
}

/*
* Number of elements in Doubly Link List
*/
void double_llist::count()
{
struct node *q = start;
int cnt = 0;
while (q != NULL)
{
q = q->next;
cnt++;
}
cout<<"Number of elements are: "<<cnt<<endl;
}

/*
* Reverse Doubly Link List
*/
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<<"List Reversed"<<endl;
}
29
ROLL NO: EXP.NO: DATE:

Output:

$ g++ doubly_llist.cpp
$ a.out
---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 2
Enter the element: 100
First Create the list.

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 3
Enter the element: 200
Insert Element after postion: 1
First Create the list.
30
ROLL NO: EXP.NO: DATE:

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 4
List empty,nothing to delete

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
List empty,nothing to display

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
31
ROLL NO: EXP.NO: DATE:

2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 6
Number of elements are: 0

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 7
List empty,nothing to reverse

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 1
Enter the element: 100
32
ROLL NO: EXP.NO: DATE:

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
100 <-> NULL

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 2
Enter the element: 200
Element Inserted

---------------------------------

Operations on Doubly linked list


33
ROLL NO: EXP.NO: DATE:

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
200 <-> 100 <-> NULL

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 3
Enter the element: 50
Insert Element after postion: 2
Element Inserted

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
34
ROLL NO: EXP.NO: DATE:

3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
200 <-> 100 <-> 50 <-> NULL

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 3
Enter the element: 150
Insert Element after postion: 3
Element Inserted

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
35
ROLL NO: EXP.NO: DATE:

7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
200 <-> 100 <-> 50 <-> 150 <-> NULL

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 6
Number of elements are: 4

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 4
Enter the element for deletion: 50
Element Deleted

36
ROLL NO: EXP.NO: DATE:

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
200 <-> 100 <-> 150 <-> NULL

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 6
Number of elements are: 3

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
37
ROLL NO: EXP.NO: DATE:

3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 7
List Reversed

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
Enter your choice : 5
The Doubly Link List is :
150 <-> 100 <-> 200 <-> NULL

---------------------------------

Operations on Doubly linked list

---------------------------------
1.Create Node
2.Add at begining
3.Add after
4.Delete
5.Display
6.Count
7.Reverse
8.Quit
38
ROLL NO: EXP.NO: DATE:

Enter your choice : 3


Enter the element: 200
Insert Element after postion: 100
There are less than 100 elements.

Experiment 3.
Implementations of multi stack in single array

Procedure
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

#define SIZE 10

int ar[SIZE];
int top1 = -1;
int top2 = SIZE;
//Functions to push data
void push_stack1 (int data)
{
if (top1 < top2 - 1)
{
ar[++top1] = data;
}
else
{
cout<<"Stack Full! Cannot Push\n";
}
}
void push_stack2 (int data)
{
if (top1 < top2 - 1)
{
ar[--top2] = data;
}
else

39
ROLL NO: EXP.NO: DATE:

{
cout<<"Stack Full! Cannot Push\n";
}
}
//Functions to pop data
void pop_stack1 ()
{
if (top1 >= 0)
{
int popped_value = ar[top1--];
cout<<”being popped from Stack 1\n"<<popped_value);
}
else
{
cout<<"Stack Empty! Cannot Pop\n";
}
}
void pop_stack2 ()
{
if (top2 < SIZE)
{
int popped_value = ar[top2++];
cout<<”is being popped from Stack 2\n"<<popped_value);
}
else
{
cout<<"Stack Empty! Cannot Pop\n";
}
}

//Functions to Print Stack 1 and Stack 2


void print_stack1 ()
{
int i;
for (i = top1; i >= 0; --i)
{
cout<<ar[i];
}
cout<<endl;
}
void print_stack2 ()
40
ROLL NO: EXP.NO: DATE:

{
int i;
for (i = top2; i < SIZE; ++i)
{
cout<<ar[i];
}
cout<<endl;

int main()
{
int ar[SIZE];
int i;
int num_of_ele;

cout<<"We can push a total of 10 values\n";


//Number of elements pushed in stack 1 is 6
//Number of elements pushed in stack 2 is 4
for (i = 1; i <= 6; ++i)
{
push_stack1 (i);
cout<<"Value Pushed in Stack 1 is %d\n"<<i;
}
for (i = 1; i <= 4; ++i)
{
push_stack2 (i);
cout<<"Value Pushed in Stack 2 is \n"<< i;
}

//Print Both Stacks


print_stack1 ();
print_stack2 ();

//Pushing on Stack Full


cout<<"Pushing Value in Stack 1 “;
push_stack1 (11);

//Popping All Elements From Stack 1


num_of_ele = top1 + 1;
while (num_of_ele)
41
ROLL NO: EXP.NO: DATE:

{
pop_stack1 ();
--num_of_ele;
}
//Trying to Pop From Empty Stack
pop_stack1 ();

return 0;
}
gcc TwoStacksSingleArray.c
./a.out
We can push a total of 10 values
Value Pushed in Stack 1 is 1
Value Pushed in Stack 1 is 2
Value Pushed in Stack 1 is 3
Value Pushed in Stack 1 is 4
Value Pushed in Stack 1 is 5
Value Pushed in Stack 1 is 6
Value Pushed in Stack 2 is 1
Value Pushed in Stack 2 is 2
Value Pushed in Stack 2 is 3
Value Pushed in Stack 2 is 4
654321
4321
Pushing Value in Stack 1 is 11
Stack Full! Cannot Push
6 is being popped from Stack 1
5 is being popped from Stack 1
4 is being popped from Stack 1
3 is being popped from Stack 1
2 is being popped from Stack 1
1 is being popped from Stack 1
Stack Empty! Cannot Pop

42
ROLL NO: EXP.NO: DATE:

Experiment 4.

Implementation of Circular Queue

/ * C++ Program to Implement Circular Queue

*/
Procedure:

#include <iostream>
#define MAX 5

using namespace std;


/*
* Class Circular Queue
*/
class Circular_Queue
{
private:
int *cqueue_arr;
int front, rear;
public:
Circular_Queue()
{
cqueue_arr = new int [MAX];
rear = front = -1;
}
/*
* Insert into Circular Queue
*/
void insert(int item)
{
if ((front == 0 && rear == MAX-1) || (front == rear+1))
{
cout<<"Queue Overflow \n";
return;
}
if (front == -1)

43
ROLL NO: EXP.NO: DATE:

{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item ;
}
/*
* Delete from Circular Queue
*/
void del()
{
if (front == -1)
{
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
/*
* Display Circular Queue
*/
void display()
44
ROLL NO: EXP.NO: DATE:

{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout<<"Queue is empty\n";
return;
}
cout<<"Queue elements :\n";
if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
}
else
{
while (front_pos <= MAX - 1)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
}
cout<<endl;
}
};
/*
* Main
*/
int main()
{
int choice, item;
Circular_Queue cq;
do
45
ROLL NO: EXP.NO: DATE:

{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the element for insertion in queue : ";
cin>>item;
cq.insert(item);
break;
case 2:
cq.del();
break;
case 3:
cq.display();
break;
case 4:
break;
default:
cout<<"Wrong choice\n";
}/*End of switch*/
}
while(choice != 4);
return 0;
}

Output:
$ g++ circular_queue.cpp
$ a.out
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1

46
ROLL NO: EXP.NO: DATE:

Input the element for insertion in queue : 3


1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 2
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 6
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 4
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :

47
ROLL NO: EXP.NO: DATE:

3 2 6 4 1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 2
Element deleted from queue is : 3
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :
2 6 4 1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 4

------------------
(program exited with code: 1)
Press return to continue

48
ROLL NO: EXP.NO: DATE:

Experiment 5.

Implementation of Binary Search trees.

/*
* C++ Program To Implement BST

*/

# include <iostream>

# include <cstdlib>

using namespace std;

/*

* Node Declaration

*/

struct node

int info;

struct node *left;

struct node *right;

}*root;

/*

* Class Declaration

*/

class BST

49
ROLL NO: EXP.NO: DATE:

public:

void find(int, node **, node **);

void insert(int);

void del(int);

void case_a(node *,node *);

void case_b(node *,node *);

void case_c(node *,node *);

void preorder(node *);

void inorder(node *);

void postorder(node *);

void display(node *, int);

BST()

root = NULL;

};

/*

* Main Contains Menu

*/

int main()

int choice, num;

BST bst;

50
ROLL NO: EXP.NO: DATE:

node *temp;

while (1)

cout<<"-----------------"<<endl;

cout<<"Operations on BST"<<endl;

cout<<"-----------------"<<endl;

cout<<"1.Insert Element "<<endl;

cout<<"2.Delete Element "<<endl;

cout<<"3.Inorder Traversal"<<endl;

cout<<"4.Preorder Traversal"<<endl;

cout<<"5.Postorder Traversal"<<endl;

cout<<"6.Display"<<endl;

cout<<"7.Quit"<<endl;

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

case 1:

temp = new node;

cout<<"Enter the number to be inserted : ";

cin>>temp->info;

bst.insert(root, temp);

case 2:

51
ROLL NO: EXP.NO: DATE:

if (root == NULL)

cout<<"Tree is empty, nothing to delete"<<endl;

continue;

cout<<"Enter the number to be deleted : ";

cin>>num;

bst.del(num);

break;

case 3:

cout<<"Inorder Traversal of BST:"<<endl;

bst.inorder(root);

cout<<endl;

break;

case 4:

cout<<"Preorder Traversal of BST:"<<endl;

bst.preorder(root);

cout<<endl;

break;

case 5:

cout<<"Postorder Traversal of BST:"<<endl;

bst.postorder(root);

cout<<endl;

52
ROLL NO: EXP.NO: DATE:

break;

case 6:

cout<<"Display BST:"<<endl;

bst.display(root,1);

cout<<endl;

break;

case 7:

exit(1);

default:

cout<<"Wrong choice"<<endl;

/*

* Find Element in the Tree

*/

void BST::find(int item, node **par, node **loc)

node *ptr, *ptrsave;

if (root == NULL)

*loc = NULL;

53
ROLL NO: EXP.NO: DATE:

*par = NULL;

return;

if (item == root->info)

*loc = root;

*par = NULL;

return;

if (item < root->info)

ptr = root->left;

else

ptr = root->right;

ptrsave = root;

while (ptr != NULL)

if (item == ptr->info)

*loc = ptr;

*par = ptrsave;

return;

ptrsave = ptr;

54
ROLL NO: EXP.NO: DATE:

if (item < ptr->info)

ptr = ptr->left;

else

ptr = ptr->right;

*loc = NULL;

*par = ptrsave;

/*

* Inserting Element into the Tree

*/

void BST::insert(node *tree, node *newnode)

if (root == NULL)

root = new node;

root->info = newnode->info;

root->left = NULL;

root->right = NULL;

cout<<"Root Node is Added"<<endl;

return;

55
ROLL NO: EXP.NO: DATE:

if (tree->info == newnode->info)

cout<<"Element already in the tree"<<endl;

return;

if (tree->info > newnode->info)

if (tree->left != NULL)

insert(tree->left, newnode);

else

tree->left = newnode;

(tree->left)->left = NULL;

(tree->left)->right = NULL;

cout<<"Node Added To Left"<<endl;

return;

else

if (tree->right != NULL)

56
ROLL NO: EXP.NO: DATE:

insert(tree->right, newnode);

else

tree->right = newnode;

(tree->right)->left = NULL;

(tree->right)->right = NULL;

cout<<"Node Added To Right"<<endl;

return;

/*

* Delete Element from the tree

*/

void BST::del(int item)

node *parent, *location;

if (root == NULL)

cout<<"Tree empty"<<endl;

57
ROLL NO: EXP.NO: DATE:

return;

find(item, &parent, &location);

if (location == NULL)

cout<<"Item not present in tree"<<endl;

return;

if (location->left == NULL && location->right == NULL)

case_a(parent, location);

if (location->left != NULL && location->right == NULL)

case_b(parent, location);

if (location->left == NULL && location->right != NULL)

case_b(parent, location);

if (location->left != NULL && location->right != NULL)

case_c(parent, location);

free(location);

/*

* Case A

*/

void BST::case_a(node *par, node *loc )

58
ROLL NO: EXP.NO: DATE:

if (par == NULL)

root = NULL;

else

if (loc == par->left)

par->left = NULL;

else

par->right = NULL;

/*

* Case B

*/

void BST::case_b(node *par, node *loc)

node *child;

if (loc->left != NULL)

child = loc->left;

else

59
ROLL NO: EXP.NO: DATE:

child = loc->right;

if (par == NULL)

root = child;

else

if (loc == par->left)

par->left = child;

else

par->right = child;

/*

* Case C

*/

void BST::case_c(node *par, node *loc)

node *ptr, *ptrsave, *suc, *parsuc;

ptrsave = loc;

ptr = loc->right;

while (ptr->left != NULL)

60
ROLL NO: EXP.NO: DATE:

ptrsave = ptr;

ptr = ptr->left;

suc = ptr;

parsuc = ptrsave;

if (suc->left == NULL && suc->right == NULL)

case_a(parsuc, suc);

else

case_b(parsuc, suc);

if (par == NULL)

root = suc;

else

if (loc == par->left)

par->left = suc;

else

par->right = suc;

suc->left = loc->left;

suc->right = loc->right;

61
ROLL NO: EXP.NO: DATE:

/*

* Pre Order Traversal

*/

void BST::preorder(node *ptr)

if (root == NULL)

cout<<"Tree is empty"<<endl;

return;

if (ptr != NULL)

cout<<ptr->info<<" ";

preorder(ptr->left);

preorder(ptr->right);

/*

* In Order Traversal

*/

void BST::inorder(node *ptr)

62
ROLL NO: EXP.NO: DATE:

if (root == NULL)

cout<<"Tree is empty"<<endl;

return;

if (ptr != NULL)

inorder(ptr->left);

cout<<ptr->info<<" ";

inorder(ptr->right);

/*

* Postorder Traversal

*/

void BST::postorder(node *ptr)

if (root == NULL)

cout<<"Tree is empty"<<endl;

return;

63
ROLL NO: EXP.NO: DATE:

if (ptr != NULL)

postorder(ptr->left);

postorder(ptr->right);

cout<<ptr->info<<" ";

/*

* Display Tree Structure

*/

void BST::display(node *ptr, int level)

int i;

if (ptr != NULL)

display(ptr->right, level+1);

cout<<endl;

if (ptr == root)

cout<<"Root->: ";

else

64
ROLL NO: EXP.NO: DATE:

for (i = 0;i < level;i++)

cout<<" ";

cout<<ptr->info;

display(ptr->left, level+1);

}
}
$ g++ bst.cpp
$ a.out
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 8
Root Node is Added
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display

65
ROLL NO: EXP.NO: DATE:

7.Quit
Enter your choice : 6
Display BST:

Root->: 8
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 9
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

9
Root->: 8
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
66
ROLL NO: EXP.NO: DATE:

6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 5
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

9
Root->: 8
5
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 11
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
67
ROLL NO: EXP.NO: DATE:

5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

11
9
Root->: 8
5
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 3
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 7
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
68
ROLL NO: EXP.NO: DATE:

3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

11
9
Root->: 8
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 10
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

11
69
ROLL NO: EXP.NO: DATE:

10
9
Root->: 8
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 2
Enter the number to be deleted : 10
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

11
9
Root->: 8
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
70
ROLL NO: EXP.NO: DATE:

2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 3
Inorder Traversal of BST:
3 5 7 8 9 11
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 4
Preorder Traversal of BST:
8 5 3 7 9 11
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 5
Postorder Traversal of BST:
3 7 5 11 9 8
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
71
ROLL NO: EXP.NO: DATE:

4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 2
Enter the number to be deleted : 8
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

11
Root->: 9
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 10
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
72
ROLL NO: EXP.NO: DATE:

2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

11
10
Root->: 9
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 15
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6
Display BST:

73
ROLL NO: EXP.NO: DATE:

15
11
10
Root->: 9
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 4
Preorder Traversal of BST:
9 5 3 7 11 10 15
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 5
Postorder Traversal of BST:
3 7 5 10 15 11 9
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
74
ROLL NO: EXP.NO: DATE:

6.Display
7.Quit
Enter your choice : 6
Display BST:

15
11
10
Root->: 9
7
5
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Delete Element
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 7

------------------
(program exited with code: 1)
Press return to continue

Experiment 6.

Implementation of Hash table.


/*

75
ROLL NO: EXP.NO: DATE:

*C++ Program to Implement Hash Tables

*/
#include<iostream>

#include<cstdlib>

#include<string>

#include<cstdio>

using namespace std;

const int TABLE_SIZE = 128;

/*

* HashEntry Class Declaration

*/

class HashEntry

public:

int key;

int value;

HashEntry(int key, int value)

this->key = key;

76
ROLL NO: EXP.NO: DATE:

this->value = value;

};

/*

* HashMap Class Declaration

*/

class HashMap

private:

HashEntry **table;

public:

HashMap()

table = new HashEntry * [TABLE_SIZE];

for (int i = 0; i< TABLE_SIZE; i++)

table[i] = NULL;

/*

* Hash Function

*/

77
ROLL NO: EXP.NO: DATE:

int HashFunc(int key)

return key % TABLE_SIZE;

/*

* Insert Element at a key

*/

void Insert(int key, int value)

int hash = HashFunc(key);

while (table[hash] != NULL && table[hash]->key != key)

hash = HashFunc(hash + 1);

if (table[hash] != NULL)

delete table[hash];

table[hash] = new HashEntry(key, value);

/*

* Search Element at a key

*/

int Search(int key)

78
ROLL NO: EXP.NO: DATE:

int hash = HashFunc(key);

while (table[hash] != NULL && table[hash]->key != key)

hash = HashFunc(hash + 1);

if (table[hash] == NULL)

return -1;

else

return table[hash]->value;

/*

* Remove Element at a key

*/

void Remove(int key)

int hash = HashFunc(key);

while (table[hash] != NULL)

if (table[hash]->key == key)

break;

hash = HashFunc(hash + 1);

79
ROLL NO: EXP.NO: DATE:

if (table[hash] == NULL)

cout<<"No Element found at key "<<key<<endl;

return;

else

delete table[hash];

cout<<"Element Deleted"<<endl;

~HashMap()

for (int i = 0; i < TABLE_SIZE; i++)

if (table[i] != NULL)

delete table[i];

delete[] table;

};

/*

* Main Contains Menu

80
ROLL NO: EXP.NO: DATE:

*/

int main()

HashMap hash;

int key, value;

int choice;

while (1)

cout<<"\n----------------------"<<endl;

cout<<"Operations on Hash Table"<<endl;

cout<<"\n----------------------"<<endl;

cout<<"1.Insert element into the table"<<endl;

cout<<"2.Search element from the key"<<endl;

cout<<"3.Delete element at a key"<<endl;

cout<<"4.Exit"<<endl;

cout<<"Enter your choice: ";

cin>>choice;

switch(choice)

case 1:

cout<<"Enter element to be inserted: ";

cin>>value;

81
ROLL NO: EXP.NO: DATE:

cout<<"Enter key at which element to be inserted: ";

cin>>key;

hash.Insert(key, value);

break;

case 2:

cout<<"Enter key of the element to be searched: ";

cin>>key;

if (hash.Search(key) == -1)

cout<<"No element found at key "<<key<<endl;

continue;

else

cout<<"Element at key "<<key<<" : ";

cout<<hash.Search(key)<<endl;

break;

case 3:

cout<<"Enter key of the element to be deleted: ";

cin>>key;
82
ROLL NO: EXP.NO: DATE:

hash.Remove(key);

break;

case 4:

exit(1);

default:

cout<<"\nEnter correct option\n";

return 0;

Output:

$ g++ hash_table.cpp
$ a.out
----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 12
Enter key at which element to be inserted: 1

83
ROLL NO: EXP.NO: DATE:

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 24
Enter key at which element to be inserted: 2

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 36
Enter key at which element to be inserted: 3

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 48
Enter key at which element to be inserted: 4

----------------------
Operations on Hash Table

----------------------
84
ROLL NO: EXP.NO: DATE:

1.Insert element into the table


2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 60
Enter key at which element to be inserted: 5

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 3
Element at key 3 : 36

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 5
Element at key 5 : 60

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 3
85
ROLL NO: EXP.NO: DATE:

Enter key of the element to be deleted: 4


Element Deleted

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 4
No element found at key 4

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 2
Element at key 2 : 24

----------------------
Operations on Hash Table

Experiment 7.

Implementation of Heaps.
#include <iostream>

using namespace std;

// A function to heapify the array.

void MaxHeapify(int a[], int i, int n)

86
ROLL NO: EXP.NO: DATE:

int j, temp;

temp = a[i];

j = 2*i;

while (j <= n)

if (j < n && a[j+1] > a[j])

j = j+1;

// Break if parent value is already greater than child value.

if (temp > a[j])

break;

// Switching value with the parent node if temp < a[j].

else if (temp <= a[j])

a[j/2] = a[j];

j = 2*j;

a[j/2] = temp;

return;

void HeapSort(int a[], int n)

int i, temp;

87
ROLL NO: EXP.NO: DATE:

for (i = n; i >= 2; i--)

// Storing maximum value at the end.

temp = a[i];

a[i] = a[1];

a[1] = temp;

// Building max heap of remaining element.

MaxHeapify(a, 1, i - 1);

void Build_MaxHeap(int a[], int n)

int i;

for(i = n/2; i >= 1; i--)

MaxHeapify(a, i, n);

int main()

int n, i;

cout<<"\nEnter the number of data element to be sorted: ";

cin>>n;

n++;

int arr[n];

for(i = 1; i < n; i++)

88
ROLL NO: EXP.NO: DATE:

cout<<"Enter element "<<i<<": ";

cin>>arr[i];

// Building max heap.

Build_MaxHeap(arr, n-1);

HeapSort(arr, n-1);

// Printing the sorted data.

cout<<"\nSorted Data ";

for (i = 1; i < n; i++)

cout<<"->"<<arr[i];

return 0;

Output:
Runtime Test Cases
Case 1:

Enter the number of data element to be sorted: 10


Enter element 1: 9
Enter element 2: 6
Enter element 3: 4
Enter element 4: 3
Enter element 5: 8

89
ROLL NO: EXP.NO: DATE:

Enter element 6: 7
Enter element 7: 5
Enter element 8: 2
Enter element 9: 0
Enter element 10: 1

Sorted Data ->0->1->2->3->4->5->6->7->8->9

Experiment 8

Implementation of Breadth First Search Techniques

Procedure:

#include<iostream>
#include<conio.h>
#include<stdlib.h>

90
ROLL NO: EXP.NO: DATE:

usingnamespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];

main()
{
int m;
cout<<"enterno of vertices";
cin>> n;
cout<<"ente no of edges";
cin>> m;
cout<<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin>>i>>j;
cost[i][j]=1;
}

cout<<"enter initial vertex";


cin>>v;
cout<<"Visitied vertices\n";
cout<< v;
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0&& visited[j]!=1&& visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v <<" ";
k++;
visit[v]=0; visited[v]=1;
}
}

91
ROLL NO: EXP.NO: DATE:

OUTPUT
enterno of vertices9
ente no of edges9

EDGES
12
23
15
14
47
78
89
26
57
enter initial vertex1
Visited vertices
12 4 5 3 6 7 8 9

Experiment 9.

Implementation of Depth First Search Techniques

Procedure:

#include<iostream>
#include<conio.h>
92
ROLL NO: EXP.NO: DATE:

#include<stdlib.h>
usingnamespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];

main()
{
int m;
cout<<"enterno of vertices";
cin>> n;
cout<<"ente no of edges";
cin>> m;
cout<<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin>>i>>j;
cost[i][j]=1;
}

cout<<"enter initial vertex";


cin>>v;
cout<<"ORDER OF VISITED VERTICES";
cout<< v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0&& visited[j]!=1&& visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v <<" ";
k++;
visit[v]=0; visited[v]=1;
}
}

93
ROLL NO: EXP.NO: DATE:

OUTPUT

enterno of vertices9
ente no of edges9

EDGES
12
23
26
15
14
47
57
78
89
enter initial vertex1
ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5

Experiment 10.

Implementation of Prim’s Algorithm

Procedure:

#include <iostream>

94
ROLL NO: EXP.NO: DATE:

#include <conio.h>

using namespace std;

struct node

int fr, to, cost;

}p[6];

int c = 0, temp1 = 0, temp = 0;

void prims(int *a, int b[][7], int i, int j)

a[i] = 1;

while (c < 6)

int min = 999;

for (int i = 0; i < 7; i++)

if (a[i] == 1)

for (int j = 0; j < 7; )

if (b[i][j] >= min || b[i][j] == 0)

j++;

95
ROLL NO: EXP.NO: DATE:

else if (b[i][j] < min)

min = b[i][j];

temp = i;

temp1 = j;

a[temp1] = 1;

p[c].fr = temp;

p[c].to = temp1;

p[c].cost = min;

c++;

b[temp][temp1] = b[temp1][temp]=1000;

for (int k = 0; k < 6; k++)

cout<<"source node:"<<p[k].fr<<endl;

cout<<"destination node:"<<p[k].to<<endl;

cout<<"weight of node"<<p[k].cost<<endl;

96
ROLL NO: EXP.NO: DATE:

int main()

int a[7];

for (int i = 0; i < 7; i++)

a[i] = 0;

int b[7][7];

for (int i = 0; i < 7; i++)

cout<<"enter values for "<<(i+1)<<" row"<<endl;

for (int j = 0; j < 7; j++)

cin>>b[i][j];

prims(a, b, 0, 0);

getch();

Output
enter values of adjacency matrix for a 7 node graph:

enter values for 1 row


0
3
6

97
ROLL NO: EXP.NO: DATE:

0
0
0
0
enter values for 2 row
3
0
2
4
0
0
0
enter values for 3 row
6
2
0
1
4
2
0
enter values for 4 row
0
4
1
0
2
0
4
enter values for 5 row
0
0
4
2
0
2
1
enter values for 6 row
00
0
2
0
98
ROLL NO: EXP.NO: DATE:

2
0
1
enter values for 7 row
0
0
0
4
1
1
0
MINIMUM SPANNING TREE AND ORDER OF TRAVERSAL:

source node:0
destination node:1
weight of node3
source node:1
destination node:2
weight of node2
source node:2
destination node:3
weight of node1
source node:2
destination node:5
weight of node2
source node:5
destination node:6
weight of node1
source node:6
destination node:4
weight of node1

Experiment 11.

Implementation of Dijkstra’s Algorithm

#include<iostream>
#include<conio.h>

99
ROLL NO: EXP.NO: DATE:

#include<stdio.h>
using namespace std;
int shortest(int ,int);
int cost[10][10],dist[20],i,j,n,k,m,S[20],v,totcost,path[20],p;
main()
{
int c;
cout <<"enter no of vertices";
cin >> n;
cout <<"enter no of edges";
cin >>m;
cout <<"\nenter\nEDGE Cost\n";
for(k=1;k<=m;k++)
{
cin >> i >> j >>c;
cost[i][j]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;
cout <<"enter initial vertex";
cin >>v;
cout << v<<"\n";
shortest(v,n);
}

int shortest(int v,int n)


{
int min;
for(i=1;i<=n;i++)

100
ROLL NO: EXP.NO: DATE:

{
S[i]=0;
dist[i]=cost[v][i];
}
path[++p]=v;
S[v]=1;
dist[v]=0;
for(i=2;i<=n-1;i++)
{
k=-1;
min=31999;
for(j=1;j<=n;j++)
{
if(dist[j]<min && S[j]!=1)
{
min=dist[j];
k=j;
}
}
if(cost[v][k]<=dist[k])
p=1;
path[++p]=k;
for(j=1;j<=p;j++)
cout<<path[j];
cout <<"\n";
//cout <<k;
S[k]=1;
for(j=1;j<=n;j++)
if(cost[k][j]!=31999 && dist[j]>=dist[k]+cost[k][j] && S[j]!=1)
dist[j]=dist[k]+cost[k][j];
}

101
ROLL NO: EXP.NO: DATE:

}
OUTPUT

enter no of vertices6
enter no of edges11

enter
EDGE Cost
1 2 50
1 3 45
1 4 10
2 3 10
2 4 15
3 5 30
4 1 10
4 5 15
5 2 20
5 3 35
653
enter initial vertex1
1
14
145
1452
13

Experiment 12.

Implementation of Kruskal’s Algorithm

Procedure:

102
ROLL NO: EXP.NO: DATE:

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p;

main()

int dup1,dup2;

cout<<"enter no of vertices";

cin >> n;

cout <<"enter no of edges";

cin >>m;

cout <<"EDGE Cost";

for(k=1;k<=m;k++)

cin >>i >>j >>c;

cost[i][j]=c;

cost[j][i]=c;

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

103
ROLL NO: EXP.NO: DATE:

if(cost[i][j]==0)

cost[i][j]=31999;

visit=1;

while(visit<n)

v=31999;

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]!=31999 && cost[i][j]<v && cost[i][j]!=-1 )

int count =0;

for(p=1;p<=n;p++)

if(visited[p]==i || visited[p]==j)

count++;

if(count >= 2)

for(p=1;p<=n;p++)

if(cost[i][p]!=31999 && p!=j)

dup1=p;

for(p=1;p<=n;p++)

104
ROLL NO: EXP.NO: DATE:

if(cost[j][p]!=31999 && p!=i)

dup2=p;

if(cost[dup1][dup2]==-1)

continue;

l=i;

k=j;

v=cost[i][j];

cout <<"edge from " <<l <<"-->"<<k;

cost[l][k]=-1;

cost[k][l]=-1;

visit++;

int count=0;

count1=0;

for(i=1;i<=n;i++)

if(visited[i]==l)

count++;

if(visited[i]==k)

count1++;

105
ROLL NO: EXP.NO: DATE:

if(count==0)

visited[++vst]=l;

if(count1==0)

visited[++vst]=k;

OUTPUT

enter no of vertices4
enter no of edges4
EDGE Cost
121
232
343
133

edge from 1–>2edge from 2–>3edge from 1–>3

Experiment 13.

Implementation of MergeSort

#include<iostream>

106
ROLL NO: EXP.NO: DATE:

#include<conio.h>

using namespace std;

void mergesort(int *,int,int);

void merge(int *,int,int,int);

int a[20],i,n,b[20];

main()

cout <<"\n enter no of elements";

cin >> n;

cout <<"enter the elements";

for(i=0;i<n;i++)

cin >> a[i];

mergesort(a,0,n-1);

cout <<" numbers after sort";

for(i=0;i<n;i++)

cout << a[i] << " ";

getch();

void mergesort(int a[],int i,int j)

107
ROLL NO: EXP.NO: DATE:

int mid;

if(i<j)

mid=(i+j)/2;

mergesort(a,i,mid);

mergesort(a,mid+1,j);

merge(a,i,mid,j);

void merge(int a[],int low,int mid ,int high)

int h,i,j,k;

h=low;

i=low;

j=mid+1;

while(h<=mid && j<=high)

if(a[h]<=a[j])

b[i]=a[h++];

else

b[i]=a[j++];

i++;

108
ROLL NO: EXP.NO: DATE:

if( h > mid)

for(k=j;k<=high;k++)

b[i++]=a[k];

else

for(k=h;k<=mid;k++)

b[i++]=a[k];

cout <<"\n";

for(k=low;k<=high;k++)

{ a[k]=b[k];

cout << a[k] <<" ";

Output:

N enter no of elements8 12 5 61 60 50 1 70 81
enter the elements
5 12
60 61
5 12 60 61
1 50
70 81

109
ROLL NO: EXP.NO: DATE:

1 50 70 81
1 5 12 50 60 61 70 81 numbers after sort1 5 12 50 60 61 70 81

Experiment 14.

Implementation of Quick Sort

110
ROLL NO: EXP.NO: DATE:

#include<iostream.h>
#include<conio.h>
int a[10],l,u,i,j;
void quick(int*,int,int);
void main()
{
clrscr();
cout<<"enter 10 elements";
for(i=0;i<10;i++)
cin>> a[i];
l=0;
u=9;
quick(a,l,u);
cout<<"sorted elements";
for(i=0;i<10;i++)
cout<< a[i]<<" ";
getch();
}

void quick(int a[],int l,int u)


{
int p,temp;
if(l<u)
{
p=a[l];
i=l;
j=u;
while(i<j)
{
while(a[i]<= p && i<j )
i++;
while(a[j]>p && i<=j )
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;}
}
temp=a[j];
a[j]=a[l];
111
ROLL NO: EXP.NO: DATE:

a[l]=temp;
cout<<"\n";
for(i=0;i<10;i++)
cout<<a[i]<<" ";
quick(a,l,j-1);
quick(a,j+1,u);
}
}

Output:

enter 10 elements5 2 3 16 25 1 20 7 8 61 14

1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 8 16 20 7 25 61
1 2 3 5 7 8 20 16 25 61
1 2 3 5 7 8 16 20 25 61
1 2 3 5 7 8 16 20 25 61

Experiment 15.

Implementation of Data Searching using divide and conquer technique

112
ROLL NO: EXP.NO: DATE:

Procedure:

#include<iostream>

using namespace std;

void FibonacciSearch(int *a, int start, int end, int *fab, int index, int item)
{
int i, mid;

// Assigning middle of the array using Fibonacci element.


mid = start+fab[index-2];

// Return if item found at mid index.


if(item == a[mid])
{
cout<<"\n item found at "<<mid<<" index.";
return;
}
// Return if item found at start index.
else if(item == a[start])
{
cout<<"\n item found at "<<start<<" index.";
return;
}
// Return if item found at end index.
else if(item == a[end])
{
cout<<"\n item found at "<<end<<" index.";
return;
}
// If mid becomes start or end of the sub-array then element not found.
else if(mid == start || mid == end)
{
cout<<"\nElement not found";
return;
}
// According to the item value choose the partion to proceed further.
else if(item > a[mid])
FibonacciSearch(a, mid, end, fab, index-1, item);

113
ROLL NO: EXP.NO: DATE:

else
FibonacciSearch(a, start, mid, fab, index-2, item);
}

main()
{
int n, i, biter, fab[20], a[20]={1, 9, 18, 24, 27, 35, 38, 41, 49, 53, 55, 66, 67, 72, 75, 77,
81, 89, 90, 97};
char ch;

fab[0] = 0;
fab[1] = 1;
i = 1;
while(fab[i] < 20)
{
i++;
fab[i] = fab[i-1]+fab[i-2];
}

up:
cout<<"\nEnter the Element to be searched: ";
cin>>n;

// Implement Fibonacci search.


FibonacciSearch(a, 0, 19, fab, i, n);

// Ask user to enter choice for further searching.


cout<<"\n\n\tDo you want to search more...enter choice(y/n)?";
cin>>ch;
if(ch == 'y' || ch == 'Y')
goto up;

return 0;
}

Output:

Runtime Test Cases


Case 1:

114
ROLL NO: EXP.NO: DATE:

Enter the Element to be searched: 38

Item found at 6 index.

Do you want to search more...enter choice(y/n)?y

Enter the Element to be searched: 66

Item found at 11 index.

Do you want to search more...enter choice(y/n)?y

Enter the Element to be searched: 89

Item found at 17 index.

Do you want to search more...enter choice(y/n)?y

Enter the Element to be searched: 1

Item found at 0 index.

Do you want to search more...enter choice(y/n)?y

Enter the Element to be searched: 97

Item found at 19 index.

Do you want to search more...enter choice(y/n)?n

115

You might also like