DS TRHU C++ PROGRAMS-2
DS TRHU C++ PROGRAMS-2
NO: DATE:
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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.
#include<iostream>
#include<cstdio>
#include<cstdlib>
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
struct node *next;
22
ROLL NO: EXP.NO: DATE:
/*
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:
/*
* 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:
/*
* 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
---------------------------------
---------------------------------
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.
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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
---------------------------------
---------------------------------
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:
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";
}
}
{
int i;
for (i = top2; i < SIZE; ++i)
{
cout<<ar[i];
}
cout<<endl;
int main()
{
int ar[SIZE];
int i;
int num_of_ele;
{
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.
*/
Procedure:
#include <iostream>
#define MAX 5
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:
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.
/*
* C++ Program To Implement BST
*/
# include <iostream>
# include <cstdlib>
/*
* Node Declaration
*/
struct node
int info;
}*root;
/*
* Class Declaration
*/
class BST
49
ROLL NO: EXP.NO: DATE:
public:
void insert(int);
void del(int);
BST()
root = NULL;
};
/*
*/
int main()
BST bst;
50
ROLL NO: EXP.NO: DATE:
node *temp;
while (1)
cout<<"-----------------"<<endl;
cout<<"Operations on BST"<<endl;
cout<<"-----------------"<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cin>>choice;
switch(choice)
case 1:
cin>>temp->info;
bst.insert(root, temp);
case 2:
51
ROLL NO: EXP.NO: DATE:
if (root == NULL)
continue;
cin>>num;
bst.del(num);
break;
case 3:
bst.inorder(root);
cout<<endl;
break;
case 4:
bst.preorder(root);
cout<<endl;
break;
case 5:
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;
/*
*/
if (root == NULL)
*loc = NULL;
53
ROLL NO: EXP.NO: DATE:
*par = NULL;
return;
if (item == root->info)
*loc = root;
*par = NULL;
return;
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
if (item == ptr->info)
*loc = ptr;
*par = ptrsave;
return;
ptrsave = ptr;
54
ROLL NO: EXP.NO: DATE:
ptr = ptr->left;
else
ptr = ptr->right;
*loc = NULL;
*par = ptrsave;
/*
*/
if (root == NULL)
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
return;
55
ROLL NO: EXP.NO: DATE:
if (tree->info == newnode->info)
return;
if (tree->left != NULL)
insert(tree->left, newnode);
else
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
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;
return;
/*
*/
if (root == NULL)
cout<<"Tree empty"<<endl;
57
ROLL NO: EXP.NO: DATE:
return;
if (location == NULL)
return;
case_a(parent, location);
case_b(parent, location);
case_b(parent, location);
case_c(parent, location);
free(location);
/*
* Case A
*/
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
*/
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
*/
ptrsave = loc;
ptr = loc->right;
60
ROLL NO: EXP.NO: DATE:
ptrsave = ptr;
ptr = ptr->left;
suc = ptr;
parsuc = ptrsave;
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:
/*
*/
if (root == NULL)
cout<<"Tree is empty"<<endl;
return;
if (ptr != NULL)
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
/*
* In Order Traversal
*/
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
*/
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<<" ";
/*
*/
int i;
if (ptr != NULL)
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
64
ROLL NO: EXP.NO: DATE:
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.
75
ROLL NO: EXP.NO: DATE:
*/
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
/*
*/
class HashEntry
public:
int key;
int value;
this->key = key;
76
ROLL NO: EXP.NO: DATE:
this->value = value;
};
/*
*/
class HashMap
private:
HashEntry **table;
public:
HashMap()
table[i] = NULL;
/*
* Hash Function
*/
77
ROLL NO: EXP.NO: DATE:
/*
*/
if (table[hash] != NULL)
delete table[hash];
/*
*/
78
ROLL NO: EXP.NO: DATE:
if (table[hash] == NULL)
return -1;
else
return table[hash]->value;
/*
*/
if (table[hash]->key == key)
break;
79
ROLL NO: EXP.NO: DATE:
if (table[hash] == NULL)
return;
else
delete table[hash];
cout<<"Element Deleted"<<endl;
~HashMap()
if (table[i] != NULL)
delete table[i];
delete[] table;
};
/*
80
ROLL NO: EXP.NO: DATE:
*/
int main()
HashMap hash;
int choice;
while (1)
cout<<"\n----------------------"<<endl;
cout<<"\n----------------------"<<endl;
cout<<"4.Exit"<<endl;
cin>>choice;
switch(choice)
case 1:
cin>>value;
81
ROLL NO: EXP.NO: DATE:
cin>>key;
hash.Insert(key, value);
break;
case 2:
cin>>key;
if (hash.Search(key) == -1)
continue;
else
cout<<hash.Search(key)<<endl;
break;
case 3:
cin>>key;
82
ROLL NO: EXP.NO: DATE:
hash.Remove(key);
break;
case 4:
exit(1);
default:
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:
----------------------
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:
----------------------
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>
86
ROLL NO: EXP.NO: DATE:
int j, temp;
temp = a[i];
j = 2*i;
while (j <= n)
j = j+1;
break;
a[j/2] = a[j];
j = 2*j;
a[j/2] = temp;
return;
int i, temp;
87
ROLL NO: EXP.NO: DATE:
temp = a[i];
a[i] = a[1];
a[1] = temp;
MaxHeapify(a, 1, i - 1);
int i;
MaxHeapify(a, i, n);
int main()
int n, i;
cin>>n;
n++;
int arr[n];
88
ROLL NO: EXP.NO: DATE:
cin>>arr[i];
Build_MaxHeap(arr, n-1);
HeapSort(arr, n-1);
cout<<"->"<<arr[i];
return 0;
Output:
Runtime Test Cases
Case 1:
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
Experiment 8
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;
}
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.
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;
}
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.
Procedure:
#include <iostream>
94
ROLL NO: EXP.NO: DATE:
#include <conio.h>
struct node
}p[6];
a[i] = 1;
while (c < 6)
if (a[i] == 1)
j++;
95
ROLL NO: EXP.NO: DATE:
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;
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];
a[i] = 0;
int b[7][7];
cin>>b[i][j];
prims(a, b, 0, 0);
getch();
Output
enter values of adjacency matrix for a 7 node graph:
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.
#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);
}
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.
Procedure:
102
ROLL NO: EXP.NO: DATE:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
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;
cin >>m;
for(k=1;k<=m;k++)
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++)
for(p=1;p<=n;p++)
if(visited[p]==i || visited[p]==j)
count++;
if(count >= 2)
for(p=1;p<=n;p++)
dup1=p;
for(p=1;p<=n;p++)
104
ROLL NO: EXP.NO: DATE:
dup2=p;
if(cost[dup1][dup2]==-1)
continue;
l=i;
k=j;
v=cost[i][j];
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
Experiment 13.
Implementation of MergeSort
#include<iostream>
106
ROLL NO: EXP.NO: DATE:
#include<conio.h>
int a[20],i,n,b[20];
main()
cin >> n;
for(i=0;i<n;i++)
mergesort(a,0,n-1);
for(i=0;i<n;i++)
getch();
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);
int h,i,j,k;
h=low;
i=low;
j=mid+1;
if(a[h]<=a[j])
b[i]=a[h++];
else
b[i]=a[j++];
i++;
108
ROLL NO: EXP.NO: DATE:
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];
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.
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();
}
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.
112
ROLL NO: EXP.NO: DATE:
Procedure:
#include<iostream>
void FibonacciSearch(int *a, int start, int end, int *fab, int index, int item)
{
int i, mid;
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;
return 0;
}
Output:
114
ROLL NO: EXP.NO: DATE:
115