You are on page 1of 242

Queue using arrays

#include<iostream>

#include<conio.h>

#include<stdlib.h>

#define MAX_SIZE 100

using namespace std;

class Queue {

private:

int item, i;

int arr_queue[MAX_SIZE];

int rear;

int front;

public:
Queue() {

rear = 0;

front = 0;

void i​ nsert​(){

if (rear == MAX_SIZE)

cout<<"\n## Queue Reached Max!";

else {

cout<<"\nEnter The Value to be Insert : ";

cin>>item;

cout<<"\n## Position : "<< rear + 1<<" , Insert Value : "<< item;

arr_queue[rear++] = item;

void r​ emoveData​(){

if (front == rear)

cout<<"\n## Queue is Empty!";


else {

cout<<"\n## Position : "<< front <<" , Remove Value :"<<


arr_queue[front];

front++;

void d​ isplay​(){

cout<<"\n## Queue Size : "<< (rear - front);

for (i = front; i < rear; i++)

cout<<"\n## Position : "<< i <<" , Value : "<< arr_queue[i];

};

​ ain​(){
int m

int choice, exit_p = 1;

Queue obj;

cout<<"\nSimple Queue Example - Class and Memeber Functions in


C++";
do {

cout<<"\n\n Queue Main Menu";

cout<<"\n1.Insert \n2.Remove \n3.Display \nOthers to exit";

cout<<"\nEnter Your Choice : ";

cin>>choice;

switch (choice) {

case 1:

obj.insert();

break;

case 2:

obj.removeData();

break;

case 3:

obj.display();

break;

default:

exit_p = 0;

break;
}

} while (exit_p);

return 0;

Output

Simple Queue Example - Class and Memeber Functions in C++

Queue Main Menu

1.Insert

2.Remove

3.Display

Others to exit

Enter Your Choice : 1

Enter The Value to be Insert : 1

## Position : 1 , InsertValue : 1
Queue Main Menu

1.Insert

2.Remove

3.Display

Others toexit

Enter Your Choice : 1

Enter The Valueto be Insert : 2

## Position : 2 , InsertValue : 2

Queue Main Menu

1.Insert

2.Remove

3.Display

Others toexit

Enter Your Choice : 1

Enter The Valueto be Insert : 3


## Position : 3 , InsertValue : 3

Queue Main Menu

1.Insert

2.Remove

3.Display

Others toexit

Enter Your Choice : 2

## Position : 0 , Remove Value :1

Queue Main Menu

1.Insert

2.Remove

3.Display

Others toexit

Enter Your Choice : 3


## Queue Size : 2

## Position : 1 , Value : 2

## Position : 2 , Value : 3

1(a) singly linked list program In c++

#include<iostream>

class Node {

public:

int key;

int data;
Node * next;

Node() {

key = 0;

data = 0;

next = NULL;

Node(int k, int d) {

key = k;

data = d;

};

class SinglyLinkedList {

public:

Node * head;

SinglyLinkedList() {

head = NULL;
}

SinglyLinkedList(Node * n) {

head = n;

// 1. CHeck if node exists using key value

Node * nodeExists(int k) {

Node * temp = NULL;

Node * ptr = head;

while (ptr != NULL) {

if (ptr - > key == k) {

temp = ptr;

ptr = ptr - > next;

return temp;

}
// 2. Append a node to the list

void appendNode(Node * n) {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

if (head == NULL) {

head = n;

cout<< "Node Appended" <<endl;

} else {

Node * ptr = head;

while (ptr - > next != NULL) {

ptr = ptr - > next;

ptr - > next = n;

cout<< "Node Appended" <<endl;

}
}

// 3. Prepend Node - Attach a node at the start

void prependNode(Node * n) {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

n - > next = head;

head = n;

cout<< "Node Prepended" <<endl;

// 4. Insert a Node after a particular node in the list

void insertNodeAfter(int k, Node * n) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value: " << k <<endl;
} else {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

n - > next = ptr - > next;

ptr - > next = n;

cout<< "Node Inserted" <<endl;

// 5. Delete node by unique key

void deleteNodeByKey(int k) {

if (head == NULL) {

cout<< "Singly Linked List already Empty. Cant delete" <<endl;

} else if (head != NULL) {

if (head - > key == k) {

head = head - > next;


cout<< "Node UNLINKED with keys value : " << k <<endl;

} else {

Node * temp = NULL;

Node * prevptr = head;

Node * currentptr = head - > next;

while (currentptr != NULL) {

if (currentptr - > key == k) {

temp = currentptr;

currentptr = NULL;

} else {

prevptr = prevptr - > next;

currentptr = currentptr - > next;

if (temp != NULL) {

prevptr - > next = temp - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;

} else {

cout<< "Node Doesn't exist with key value : " << k <<endl;
}

// 6th update node

void updateNodeByKey(int k, int d) {

Node * ptr = nodeExists(k);

if (ptr != NULL) {

ptr - > data = d;

cout<< "Node Data Updated Successfully" <<endl;

} else {

cout<< "Node Doesn't exist with key value : " << k <<endl;

// 7th printing
void printList() {

if (head == NULL) {

cout<< "No Nodes in Singly Linked List";

} else {

cout<<endl<< "Singly Linked List Values : ";

Node * temp = head;

while (temp != NULL) {

cout<< "(" << temp - > key << "," << temp - > data << ") --> ";

temp = temp - > next;

};

int main() {
SinglyLinkedList s;

int option;

int key1, k1, data1;

do {

cout<< "\nWhat operation do you want to perform? Select Option


number. Enter 0 to exit." <<endl;

cout<< "1. appendNode()" <<endl;

cout<< "2. prependNode()" <<endl;

cout<< "3. insertNodeAfter()" <<endl;

cout<< "4. deleteNodeByKey()" <<endl;

cout<< "5. updateNodeByKey()" <<endl;

cout<< "6. print()" <<endl;

cout<< "7. Clear Screen" <<endl<<endl;

cin>> option;

Node * n1 = new Node();

//Node n1;

switch (option) {
case 0:

break;

case 1:

cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

s.appendNode(n1);

//cout<<n1.key<<" = "<<n1.data<<endl;

break;

case 2:

cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;


n1 - > data = data1;

s.prependNode(n1);

break;

case 3:

cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;

cin>> k1;

cout<< "Enter key & data of the New Node first: " <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

s.insertNodeAfter(k1, n1);

break;

case 4:
cout<< "Delete Node By Key Operation - \nEnter key of the Node to be
deleted: " <<endl;

cin>> k1;

s.deleteNodeByKey(k1);

break;

case 5:

cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;

cin>> key1;

cin>> data1;

s.updateNodeByKey(key1, data1);

break;

case 6:

s.printList();

break;

case 7:
system("cls");

break;

default:

cout<< "Enter Proper Option number " <<endl;

} while (option != 0);

return 0;

Output​:

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen
1

Append node operation

enter key and data of the node to be appended

Node appended

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Prepend node operation

Enter key and data of the node to be prepended

7
8

Node already exists with key value? Prepend another node with
different key value

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Prepend node operation

Enter key and data of the node to be prepended

Node prepended

What operation do you want to perform? Select option nunber.Enter 0


to exit
1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Insert node after operation

Enter key of existing node after which you want to insert this new node

Enter key and data of the new node first:

Node inserted

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()
3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Delete node by key operation

Enter key of the node to be deleted

Node unlinked with key value: 7

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen
5

Update node by key operation

Enter key and new node to be updated

10

Node data updated successfully

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Singly linked list values are (9,7)à(4,5)à(5,10)à(1,9)à

What operation do you want to perform? Select option nunber.Enter 0


to exit
1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

0
1(b)Doubly Linked List using C++

#include<iostream>

class Node {

public:

int key;

int data;

Node * next;

Node * previous;

Node() {

key = 0;

data = 0;

next = NULL;

previous = NULL;

Node(int k, int d) {

key = k;

data = d;
}

};

class DoublyLinkedList {

public:

Node * head;

DoublyLinkedList() {

head = NULL;

DoublyLinkedList(Node * n) {

head = n;

// 1. CHeck if node exists using key value

Node * nodeExists(int k) {

Node * temp = NULL;


Node * ptr = head;

while (ptr != NULL) {

if (ptr - > key == k) {

temp = ptr;

ptr = ptr - > next;

return temp;

// 2. Append a node to the list

void appendNode(Node * n) {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {
if (head == NULL) {

head = n;

cout<< "Node Appended as Head Node" <<endl;

} else {

Node * ptr = head;

while (ptr - > next != NULL) {

ptr = ptr - > next;

ptr - > next = n;

n - > previous = ptr;

cout<< "Node Appended" <<endl;

// 3. Prepend Node - Attach a node at the start

void prependNode(Node * n) {

if (nodeExists(n - > key) != NULL) {


cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

if (head == NULL) {

head = n;

cout<< "Node Prepended as Head Node" <<endl;

} else {

head - > previous = n;

n - > next = head;

head = n;

cout<< "Node Prepended" <<endl;

// 4. Insert a Node after a particular node in the list

void insertNodeAfter(int k, Node * n) {

Node * ptr = nodeExists(k);


if (ptr == NULL) {

cout<< "No node exists with key value: " << k <<endl;

} else {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

Node * nextNode = ptr - > next;

// inserting at the end

if (nextNode == NULL) {

ptr - > next = n;

n - > previous = ptr;

cout<< "Node Inserted at the END" <<endl;

//inserting in between

else {

n - > next = nextNode;

nextNode - > previous = n;


n - > previous = ptr;

ptr - > next = n;

cout<< "Node Inserted in Between" <<endl;

// 5. Delete node by unique key. Basically De-Link not delete

void deleteNodeByKey(int k) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value: " << k <<endl;

} else {

if (head - > key == k) {


head = head - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;

} else {

Node * nextNode = ptr - > next;

Node * prevNode = ptr - > previous;

// deleting at the end

if (nextNode == NULL) {

prevNode - > next = NULL;

cout<< "Node Deleted at the END" <<endl;

//deleting in between

else {

prevNode - > next = nextNode;

nextNode - > previous = prevNode;

cout<< "Node Deleted in Between" <<endl;

}
}

// 6th update node

void updateNodeByKey(int k, int d) {

Node * ptr = nodeExists(k);

if (ptr != NULL) {

ptr - > data = d;

cout<< "Node Data Updated Successfully" <<endl;

} else {

cout<< "Node Doesn't exist with key value : " << k <<endl;

// 7th printing

void printList() {
if (head == NULL) {

cout<< "No Nodes in Doubly Linked List";

} else {

cout<<endl<< "Doubly Linked List Values : ";

Node * temp = head;

while (temp != NULL) {

cout<< "(" << temp - > key << "," << temp - > data << ") <--> ";

temp = temp - > next;

};

int main() {

DoublyLinkedListobj;
int option;

int key1, k1, data1;

do {

cout<< "\nWhat operation do you want to perform? Select Option


number. Enter 0 to exit." <<endl;

cout<< "1. appendNode()" <<endl;

cout<< "2. prependNode()" <<endl;

cout<< "3. insertNodeAfter()" <<endl;

cout<< "4. deleteNodeByKey()" <<endl;

cout<< "5. updateNodeByKey()" <<endl;

cout<< "6. print()" <<endl;

cout<< "7. Clear Screen" <<endl<<endl;

cin>> option;

Node * n1 = new Node();

//Node n1;

switch (option) {

case 0:
break;

case 1:

cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.appendNode(n1);

//cout<<n1.key<<" = "<<n1.data<<endl;

break;

case 2:

cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;


obj.prependNode(n1);

break;

case 3:

cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;

cin>> k1;

cout<< "Enter key & data of the New Node first: " <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.insertNodeAfter(k1, n1);

break;

case 4:
cout<< "Delete Node By Key Operation - \nEnter key of the Node to be
deleted: " <<endl;

cin>> k1;

obj.deleteNodeByKey(k1);

break;

case 5:

cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;

cin>> key1;

cin>> data1;

obj.updateNodeByKey(key1, data1);

break;

case 6:

obj.printList();

break;

case 7:
system("cls");

break;

default:

cout<< "Enter Proper Option number " <<endl;

} while (option != 0);

return 0;

Output​:

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()
6.print()

7.clearscreen

Append node operation

Enter key and data of the node to be appended

7 8

Node appended at head node

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Prepend node operation

Enter key and data of the node to be prepended


80

Node prepended

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Insert node after operation

Enter key of existing node after which you want to insert this new node

Enter key and data of new node first :

3 88

Node inserted at the end


What operation do you want to perform? Select option nunber.Enter 0
to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Delete node by key operation b

Enter key of the node to be deleted :

Node unlinked with key value : 8

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()
5.updatenodebykey()

6.print()

7.clearscreen

Update node by key operation

Enter key and new node to be updated

3 8

Node data updated successfully

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Doubly Linked List values are (7,8)à(3,8)à


What operation do you want to perform? Select option nunber.Enter 0
to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

0
1(c) circular linked list program in c++

#include<iostream>

class Node {

public:

int key;

int data;

Node * next;

Node() {

key = 0;

data = 0;

next = NULL;

Node(int k, int d) {

key = k;

data = d;

};
class CircularLinkedList {

public:

Node * head;

CircularLinkedList() {

head = NULL;

// 1. CHeck if node exists using key value

Node * nodeExists(int k) {

Node * temp = NULL;

Node * ptr = head;

if (ptr == NULL) {

return temp;

} else {

do {
if (ptr - > key == k) {

temp = ptr;

ptr = ptr - > next;

} while (ptr != head);

return temp;

//return temp;

// 2. Append a node to the list

void appendNode(Node * new_node) {

if (nodeExists(new_node - > key) != NULL) {

cout<< "Node Already exists with key value : " <<

new_node - > key <<

". Append another node with different Key value" <<

endl;
} else {

if (head == NULL) {

head = new_node;

new_node - > next = head;

cout<< "Node Appended at first Head position" <<endl;

} else {

Node * ptr = head;

while (ptr - > next != head) {

ptr = ptr - > next;

ptr - > next = new_node;

new_node - > next = head;

cout<< "Node Appended" <<endl;

// 3. Prepend Node - Attach a node at the start

void prependNode(Node * new_node) {


if (nodeExists(new_node - > key) != NULL) {

cout<< "Node Already exists with key value : " <<

new_node - > key <<

". Append another node with different Key value" <<

endl;

} else {

if (head == NULL) {

head = new_node;

new_node - > next = head;

cout<< "Node Prepended at first Head position" <<endl;

} else {

Node * ptr = head;

while (ptr - > next != head) {

ptr = ptr - > next;

ptr - > next = new_node;

new_node - > next = head;

head = new_node;
cout<< "Node Prepended" <<endl;

// 4. Insert a Node after a particular node in the list

void insertNodeAfter(int k, Node * new_node) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value OF: " << k <<endl;

} else {

if (nodeExists(new_node - > key) != NULL) {

cout<< "Node Already exists with key value : " <<

new_node - > key <<

". Append another node with different Key value" <<

endl;

} else {

if (ptr - > next == head) {


new_node - > next = head;

ptr - > next = new_node;

cout<< "Node Inserted at the End" <<endl;

} else {

new_node - > next = ptr - > next;

ptr - > next = new_node;

cout<< "Node Inserted in between" <<endl;

// 5. Delete node by unique key

void deleteNodeByKey(int k) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value OF : " << k <<

endl;
} else {

if (ptr == head) {

if (head - > next == NULL) {

head = NULL;

cout<< "Head node Unlinked... List Empty";

} else {

Node * ptr1 = head;

while (ptr1 - > next != head) {

ptr1 = ptr1 - > next;

ptr1 - > next = head - > next;

head = head - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;

} else {

Node * temp = NULL;

Node * prevptr = head;

Node * currentptr = head - > next;


while (currentptr != NULL) {

if (currentptr - > key == k) {

temp = currentptr;

currentptr = NULL;

} else {

prevptr = prevptr - > next;

currentptr = currentptr - > next;

prevptr - > next = temp - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;

// 6th update node


void updateNodeByKey(int k, intnew_data) {

Node * ptr = nodeExists(k);

if (ptr != NULL) {

ptr - > data = new_data;

cout<< "Node Data Updated Successfully" <<endl;

} else {

cout<< "Node Doesn't exist with key value : " << k <<endl;

// 7th printing

void printList() {

if (head == NULL) {

cout<< "No Nodes in Circular Linked List";

} else {

cout<<endl<< "head address : " << head <<endl;

cout<< "Circular Linked List Values : " <<endl;


Node * temp = head;

do {

cout<< "(" << temp - > key << "," << temp - > data << "," << temp - >
next << ") --> ";

temp = temp - > next;

} while (temp != head);

};

int main() {

CircularLinkedListobj;

int option;

int key1, k1, data1;


do {

cout<< "\nWhat operation do you want to perform? Select Option


number. Enter 0 to exit." <<endl;

cout<< "1. appendNode()" <<endl;

cout<< "2. prependNode()" <<endl;

cout<< "3. insertNodeAfter()" <<endl;

cout<< "4. deleteNodeByKey()" <<endl;

cout<< "5. updateNodeByKey()" <<endl;

cout<< "6. print()" <<endl;

cout<< "7. Clear Screen" <<endl<<endl;

cin>> option;

Node * n1 = new Node();

//Node n1;

switch (option) {

case 0:

break;

case 1:
cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.appendNode(n1);

//cout<<n1.key<<" = "<<n1.data<<endl;

break;

case 2:

cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.prependNode(n1);

break;
case 3:

cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;

cin>> k1;

cout<< "Enter key & data of the New Node first: " <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.insertNodeAfter(k1, n1);

break;

case 4:

cout<< "Delete Node By Key Operation - \nEnter key of the Node to be


deleted: " <<endl;

cin>> k1;
obj.deleteNodeByKey(k1);

break;

case 5:

cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;

cin>> key1;

cin>> data1;

obj.updateNodeByKey(key1, data1);

break;

case 6:

obj.printList();

break;

case 7:

system("cls");

break;

default:
cout<< "Enter Proper Option number " <<endl;

} while (option != 0);

return 0;

Output:

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

1
Append node operation

Enter key and data of the node to be appended

5 8

Node appended at first head position

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Prepend node operation

Enter key and data of the node to be prepended

6 4

Node prepended
What operation do you want to perform? Select option nunber.Enter 0
to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Inseet node after operation

Enter key and existing node after which you want to insert this new
node

Enter key and data of the new node first:

7 7

Node inserted at the end

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()
2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Delete node by key operation

Enter key of the node to be deleted :

Node unlinked with key value:5

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()
7.clearscreen

Update node by key operation

Enter key and new node to be updated

5 6

Node data updated successfully

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Head address:0x656b30

Circular linked list values are :(6,6)à (7,7)à


What operation do you want to perform? Select option nunber.Enter 0
to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

0
​4) a) stack using array
#include<iostream>

using namespace std;

class stack

int stk[5];

int top;

public:

stack()

top=-1;

void push(int x)

if(top > 4)

cout<<"stack over flow";

return;
}

stk[++top]=x;

cout<<"inserted"<<x;

void pop()

if(top <0)

{ cout <<"stack under flow";

return;

cout<<"deleted" <<stk [top--];

void display()

if(top<0)

cout <<"stack empty";

return;

}
for(int i=top; i>=0; i--)

cout<<stk[i] <<" ";

};

int main()

int ch;

stack st;

while(1)

cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";

cin >> ch;

switch(ch)

case 1: cout <<"enter the element";

cin >> ch;

st.push(ch);

break;

case 2: st.pop(); break;


case 3: st.display();break;

case 4: exit(0);

return(0);

Output:-

1.push 2.pop 3.display 4.exit

Enter ur choice1

enter the element23

inserted23

inserted45

1.push 2.pop 3.display 4.exit

Enter ur choice3

45 78 23

1.push 2.pop 3.display 4.exit

Enter ur choice2

deleted45
1.push 2.pop 3.display 4.exit

Enter ur choice2

deleted78

1.push 2.pop 3.display 4.exit

Enter ur choice3

23

1.push 2.pop 3.display 4.exit

Enter ur choice4
4) a) stack using array

#include<iostream>

using namespace std;

class stack

int stk[5];

int top;

public:

stack()

top=-1;
}

void push(int x)

if(top > 4)

cout<<"stack over flow";

return;

stk[++top]=x;

cout<<"inserted"<<x;

void pop()

if(top <0)

{ cout <<"stack under flow";

return;

cout<<"deleted" <<stk [top--];

}
void display()

if(top<0)

cout <<"stack empty";

return;

for(int i=top; i>=0; i--)

cout<<stk[i] <<" ";

};

int main()

int ch;

stack st;

while(1)

cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";

cin >> ch;


switch(ch)

case 1: cout <<"enter the element";

cin >> ch;

st.push(ch);

break;

case 2: st.pop(); break;

case 3: st.display();break;

case 4: exit(0);

return(0);

Output:-

1.push 2.pop 3.display 4.exit

Enter ur choice1

enter the element23

inserted23
inserted45

1.push 2.pop 3.display 4.exit

Enter ur choice3

45 78 23

1.push 2.pop 3.display 4.exit

Enter ur choice2

deleted45

1.push 2.pop 3.display 4.exit

Enter ur choice2

deleted78

1.push 2.pop 3.display 4.exit

Enter ur choice3

23

1.push 2.pop 3.display 4.exit

Enter ur choice4
2) b) stack using linked list

#include <iostream>

using namespace std;

struct Node {

int data;

struct Node *next;

};

struct Node* top = NULL;

void push(int val) {

struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));

newnode->data = val;

newnode->next = top;

top = newnode;

void pop() {

if(top==NULL)

cout<<"Stack Underflow"<<endl;

else {
cout<<"The popped element is "<< top->data <<endl;

top = top->next;

void display() {

struct Node* ptr;

if(top==NULL)

cout<<"stack is empty";

else {

ptr = top;

cout<<"Stack elements are: ";

while (ptr != NULL) {

cout<< ptr->data <<" ";

ptr = ptr->next;

cout<<endl;

int main() {
int ch, val;

cout<<"1) Push in stack"<<endl;

cout<<"2) Pop from stack"<<endl;

cout<<"3) Display stack"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter choice: "<<endl;

cin>>ch;

switch(ch) {

case 1: {

cout<<"Enter value to be pushed:"<<endl;

cin>>val;

push(val);

break;

case 2: {

pop();

break;

}
case 3: {

display();

break;

case 4: {

cout<<"Exit"<<endl;

break;

default: {

cout<<"Invalid Choice"<<endl;

}while(ch!=4);

return 0;

Output:-

1) Push in stack
2) Pop from stack

3) Display stack

4) Exit

Enter choice:

Enter value to be pushed:

Enter choice:

Enter value to be pushed:

Enter choice:

Stack elements are: 4 2

Enter choice:

The popped element is 4

Enter choice:
2

The popped element is 2

Enter choice:

stack is empty

Enter choice:

Exit
12) Implementation of operations on AVL trees.
#include<iostream>

#include<cstdio>

#include<sstream>
#include<algorithm>
#define pow2(n) (1 << (n))
using namespace std;
struct avl {
int d;
struct avl *l;
struct avl *r;
}*r;
class avl_tree {
public:
int height(avl *);
int difference(avl *);
avl *rr_rotat(avl *);
avl *ll_rotat(avl *);
avl *lr_rotat(avl*);
avl *rl_rotat(avl *);
avl * balance(avl *);
avl * insert(avl*, int);
void show(avl*, int);
void inorder(avl *);
void preorder(avl *);
void postorder(avl*);
avl_tree() {
r = NULL;
}
};
int avl_tree::height(avl *t) {
int h = 0;
if (t != NULL) {
int l_height = height(t->l);
int r_height = height(t->r);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}
int avl_tree::difference(avl *t) {
int l_height = height(t->l);
int r_height = height(t->r);
int b_factor = l_height - r_height;
return b_factor;
}
avl *avl_tree::rr_rotat(avl *parent) {
avl *t;
t = parent->r;
parent->r = t->l;
t->l = parent;
cout<<"Right-Right Rotation";
return t;
}
avl *avl_tree::ll_rotat(avl *parent) {
avl *t;
t = parent->l;
parent->l = t->r;
t->r = parent;
cout<<"Left-Left Rotation";
return t;
}
avl *avl_tree::lr_rotat(avl *parent) {
avl *t;
t = parent->l;
parent->l = rr_rotat(t);
cout<<"Left-Right Rotation";
return ll_rotat(parent);
}
avl *avl_tree::rl_rotat(avl *parent) {
avl *t;
t = parent->r;
parent->r = ll_rotat(t);
cout<<"Right-Left Rotation";
return rr_rotat(parent);
}
avl *avl_tree::balance(avl *t) {
int bal_factor = difference(t);
if (bal_factor > 1) {
if (difference(t->l) > 0)
t = ll_rotat(t);
else
t = lr_rotat(t);
} else if (bal_factor < -1) {
if (difference(t->r) > 0)
t = rl_rotat(t);
else
t = rr_rotat(t);
}
return t;
}
avl *avl_tree::insert(avl *r, int v) {
if (r == NULL) {
r = new avl;
r->d = v;
r->l = NULL;
r->r = NULL;
return r;
} else if (v< r->d) {
r->l = insert(r->l, v);
r = balance(r);
} else if (v >= r->d) {
r->r = insert(r->r, v);
r = balance(r);
} return r;
}
void avl_tree::show(avl *p, int l) {
int i;
if (p != NULL) {
show(p->r, l+ 1);
cout<<" ";
if (p == r)
cout << "Root -> ";
for (i = 0; i < l&& p != r; i++)
cout << " ";
cout << p->d;
show(p->l, l + 1);
}
}
void avl_tree::inorder(avl *t) {
if (t == NULL)
return;
inorder(t->l);
cout << t->d << " ";
inorder(t->r);
}
void avl_tree::preorder(avl *t) {
if (t == NULL)
return;
cout << t->d << " ";
preorder(t->l);
preorder(t->r);
}
void avl_tree::postorder(avl *t) {
if (t == NULL)
return;
postorder(t ->l);
postorder(t ->r);
cout << t->d << " ";
}
int main() {
int c, i;
avl_tree avl;
while (1) {
cout<<"menu options"<<endl;
cout << "1.Insert Element into the tree" << endl;
cout << "2.show Balanced AVL Tree" << endl;
cout << "3.InOrder traversal" << endl;
cout << "4.PreOrder traversal" << endl;
cout << "5.PostOrder traversal" << endl;
cout << "6.Exit" << endl;
cout << "Enter your Choice: ";
cin >> c;
switch (c) {
case 1:
cout << "Enter value to be inserted: ";
cin >> i;
r = avl.insert(r, i);
break;
case 2:
if (r == NULL) {
cout << "Tree is Empty" << endl;
continue;
}
cout << "Balanced AVL Tree:" << endl;
avl.show(r, 1);
cout<<endl;
break;
case 3:
cout << "Inorder Traversal:" << endl;
avl.inorder(r);
cout << endl;
break;
case 4:
cout << "Preorder Traversal:" << endl;
avl.preorder(r);
cout << endl;
break;
case 5:
cout << "Postorder Traversal:" << endl;
avl.postorder(r);
cout << endl;
break;
case 6:
exit(1);
break;
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
Output:​-
menu options

1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 1

Enter value to be inserted: 10

1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 1

Enter value to be inserted: 20

1.Insert Element into the tree

2.show Balanced AVL Tree


3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 1

Enter value to be inserted: 30

Right-Right Rotation1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 1

Enter value to be inserted: 40

1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit
Enter your Choice: 1

Enter value to be inserted: 50

Right-Right Rotation1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 1

Enter value to be inserted: 60

Right-Right Rotation1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 1

Enter value to be inserted: 40

1.Insert Element into the tree

2.show Balanced AVL Tree


3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 2

Balanced AVL Tree:

60 50 40 Root -> 40 30 20 10

1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 3

Inorder Traversal:

10 20 30 40 40 50 60

1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal
5.PostOrder traversal

6.Exit

Enter your Choice: 4

Preorder Traversal:

40 20 10 30 50 40 60

1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit

Enter your Choice: 5

Postorder Traversal:

10 30 20 40 60 50 40

1.Insert Element into the tree

2.show Balanced AVL Tree

3.InOrder traversal

4.PreOrder traversal

5.PostOrder traversal

6.Exit
Enter your Choice: 6
1) Program for binary search

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int n,i,arr[50],search,first,last,middle;

cout<<"Enter total number of elements:";

cin>>n;

cout<<"Enter"<<n<<"number:";

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

cin>>arr[i];

cout<<"Enter a number to find:";

cin>>search;

first=0;
last=n-1;

middle=(first+last)/2;

while(first<=last)

if(arr[middle]<search)

first=middle+1;

else if(arr[middle]==search)

cout<<search<<"found at location"<<middle+1<<"\n";

break;

else

last=middle-1;

middle=(first+last)/2;

}
if(first>last)

cout<<"Not found!"<<search<<"is not present in the list.";

getch();

OUTPUT:-

Enter the total number of elements :5

Enter 5 number:

12 23 34 45 56

Enter a number to find:34

34 found at location 3
2.implementation of binary search tree
#include<iostream.h>

class BST

Struct node

int data;

node* left;

node* right;

};

node* root;

node*makeEmpty(node* t)

if(t==NULL)

return NULL;

makeEmpty(t->left);

makeEmpty(t->right);

delete t;

}
Return NULL;

node* insert(int x,node* t)

if(t==NULL)

t=new node;

t->data=x;

t->left=t->right=NULL;

else if(x<t->data)

t->left=insert(x,t->left);

else if(x>t->data)

t->right=insert(x,t->right);

return t;

node* findMin(node* t)

if(t==NULL)
return NULL;

else if(t->left==NULL)

return t;

else

return findMin(t->left);

node* findMax(node* t)

if(t==NULL)

return NULL;

else if(t->right==NULL)

return t;

else

return findMax(t->right);

node* remove(int x ,node* t)

node* temp;
if(t==NULL)

return NULL;

else if(x<t->data)

t->left=remove(x,t->left);

else if(x<t->data)

t->right=remove(x,t->right);

else if(t->left&&t->right)

temp=findMin(t->right);

t->data=temp->data;

t->right=remove(t->data,t->right);

else

temp=t;

if(t->left==NULL)

t=t->right;

else if(t->right==NULL)

t=t->left;
delete temp;

return t;

void inorder(node* t)

if(t==NULL)

return;

inorder(t->left);

cout<<t->data<<" ";

inorder(t->right);

node* find(node* t,int x)

if(t==NULL)

return NULL;

else if(x<t->data)

return find(t->left,x);

else if(x>t->data)
return find(t->right,x);

else

return t;

public:

BST()

root=NULL;

~BST()

root=makeEmpty(root);

Void insert(int x)

root=insert(x,root);

Void remove(int x)

{
root=remove(x,root);

void display()

inorder(root);

cout<<endl;

void search(int x)

root=find(root,x);

};

int main()

BST t;

t.insert(20);

t.insert(25);

t.insert(15);
t.insert(10);

t.insert(30);

t.display();

t.remove(20);

t.display();

t.remove(25);

t.display();

t.remove(30);

t.display();

return 0;

OUTPUT:

10 15 20 25 30

10 15 25 30

10 15 30

10 15
12) Implementation recursive and iterative traversals on binary tree

#include<iostream.h>

#include<conio.h>

#include<process.h>

struct tree_node

tree_node *left;

tree_node *right;

int data;

};

class bst

tree_node *root;

public:

bst()

root=NULL;

}
int isempty()

return(root==NULL);

void insert(int item);

void inordertrav();

void inorder(tree_node*);

void postordertrav();

void postorder(tree_node*);

void preordertrav();

void preorder(tree_node*);

};

void bst::insert(int item)

tree_node *p=new tree_node;

tree_node *parent;

p->data=item;

p->left=NULL;

p->right=NULL;
parent=NULL;

if(isempty())

root=p;

else

tree_node *ptr;

ptr=root;

while(ptr!=NULL)

parent=ptr;

if(item>ptr->data)

ptr=ptr->right;

else

ptr=ptr->left;

if(item<parent->data)

parent->left=p;

else

parent->right=p;
}

void bst::inordertrav()

inorder(root);

void bst::inorder(tree_node *ptr)

if(ptr!=NULL)

inorder(ptr->left);

cout<<" "<<ptr->data<<" ";

inorder(ptr->right);

void bst::postordertrav()

postorder(root);

}
void bst ::postorder(tree_node*ptr)

if(ptr!=NULL)

postorder(prt->left);

postorder(ptr->right);

cout<<" "<<ptr->data<<" ";

void bst::preordertrav()

preorder(root);

void bst::preorder(tree_node*ptr)

if(ptr!=NULL)

cout<<" "<<ptr->data<<" ";

preorder(ptr->left);
preorder(ptr->right);

void main()

bst b;

b.insert(52);

b.insert(25);

b.insert(50);

b.insert(15);

b.insert(40);

b.insert(45);

b.insert(20);

cout<<"inorder"<<endl;

b.inordertrav();

cout<<endl<<"postorder"<<endl;

b.postordertrav();

cout<<endl<<"preorder"<<endl;

b.preordertrav();
getch();

OUTPUT:-

Inorder

15 20 25 40 45 50 52

Postorder

20 15 45 40 50 25 52

Preorder

52 25 15 20 50 40 45
1(b)Doubly Linked List using C++

#include<iostream>

class Node {
public:

int key;

int data;

Node * next;

Node * previous;

Node() {

key = 0;

data = 0;

next = NULL;

previous = NULL;

Node(int k, int d) {

key = k;

data = d;

};

class DoublyLinkedList {
public:

Node * head;

DoublyLinkedList() {

head = NULL;

DoublyLinkedList(Node * n) {

head = n;

// 1. CHeck if node exists using key value

Node * nodeExists(int k) {

Node * temp = NULL;

Node * ptr = head;

while (ptr != NULL) {

if (ptr - > key == k) {


temp = ptr;

ptr = ptr - > next;

return temp;

// 2. Append a node to the list

void appendNode(Node * n) {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

if (head == NULL) {

head = n;

cout<< "Node Appended as Head Node" <<endl;

} else {
Node * ptr = head;

while (ptr - > next != NULL) {

ptr = ptr - > next;

ptr - > next = n;

n - > previous = ptr;

cout<< "Node Appended" <<endl;

// 3. Prepend Node - Attach a node at the start

void prependNode(Node * n) {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

if (head == NULL) {

head = n;
cout<< "Node Prepended as Head Node" <<endl;

} else {

head - > previous = n;

n - > next = head;

head = n;

cout<< "Node Prepended" <<endl;

// 4. Insert a Node after a particular node in the list

void insertNodeAfter(int k, Node * n) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value: " << k <<endl;

} else {

if (nodeExists(n - > key) != NULL) {


cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

Node * nextNode = ptr - > next;

// inserting at the end

if (nextNode == NULL) {

ptr - > next = n;

n - > previous = ptr;

cout<< "Node Inserted at the END" <<endl;

//inserting in between

else {

n - > next = nextNode;

nextNode - > previous = n;

n - > previous = ptr;

ptr - > next = n;

cout<< "Node Inserted in Between" <<endl;


}

// 5. Delete node by unique key. Basically De-Link not delete

void deleteNodeByKey(int k) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value: " << k <<endl;

} else {

if (head - > key == k) {

head = head - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;

} else {

Node * nextNode = ptr - > next;


Node * prevNode = ptr - > previous;

// deleting at the end

if (nextNode == NULL) {

prevNode - > next = NULL;

cout<< "Node Deleted at the END" <<endl;

//deleting in between

else {

prevNode - > next = nextNode;

nextNode - > previous = prevNode;

cout<< "Node Deleted in Between" <<endl;

}
// 6th update node

void updateNodeByKey(int k, int d) {

Node * ptr = nodeExists(k);

if (ptr != NULL) {

ptr - > data = d;

cout<< "Node Data Updated Successfully" <<endl;

} else {

cout<< "Node Doesn't exist with key value : " << k <<endl;

// 7th printing

void printList() {

if (head == NULL) {

cout<< "No Nodes in Doubly Linked List";

} else {

cout<<endl<< "Doubly Linked List Values : ";


Node * temp = head;

while (temp != NULL) {

cout<< "(" << temp - > key << "," << temp - > data << ") <--> ";

temp = temp - > next;

};

int main() {

DoublyLinkedListobj;

int option;

int key1, k1, data1;

do {
cout<< "\nWhat operation do you want to perform? Select Option
number. Enter 0 to exit." <<endl;

cout<< "1. appendNode()" <<endl;

cout<< "2. prependNode()" <<endl;

cout<< "3. insertNodeAfter()" <<endl;

cout<< "4. deleteNodeByKey()" <<endl;

cout<< "5. updateNodeByKey()" <<endl;

cout<< "6. print()" <<endl;

cout<< "7. Clear Screen" <<endl<<endl;

cin>> option;

Node * n1 = new Node();

//Node n1;

switch (option) {

case 0:

break;

case 1:
cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.appendNode(n1);

//cout<<n1.key<<" = "<<n1.data<<endl;

break;

case 2:

cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.prependNode(n1);

break;
case 3:

cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;

cin>> k1;

cout<< "Enter key & data of the New Node first: " <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.insertNodeAfter(k1, n1);

break;

case 4:

cout<< "Delete Node By Key Operation - \nEnter key of the Node to be


deleted: " <<endl;

cin>> k1;
obj.deleteNodeByKey(k1);

break;

case 5:

cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;

cin>> key1;

cin>> data1;

obj.updateNodeByKey(key1, data1);

break;

case 6:

obj.printList();

break;

case 7:

system("cls");

break;

default:
cout<< "Enter Proper Option number " <<endl;

} while (option != 0);

return 0;

Output​:

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

1
Append node operation

Enter key and data of the node to be appended

7 8

Node appended at head node

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Prepend node operation

Enter key and data of the node to be prepended

80

Node prepended
What operation do you want to perform? Select option nunber.Enter 0
to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Insert node after operation

Enter key of existing node after which you want to insert this new node

Enter key and data of new node first :

3 88

Node inserted at the end

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()
2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Delete node by key operation b

Enter key of the node to be deleted :

Node unlinked with key value : 8

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen
5

Update node by key operation

Enter key and new node to be updated

3 8

Node data updated successfully

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Doubly Linked List values are (7,8)à(3,8)à

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()
2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

0
9(a) C++ program to implement Selection sort using class

#include <iostream.h>

constant MAX = 10 ;

class array

private :

intarr[MAX] ;

int count ;

public :

array( ) ;

void add ( int item ) ;

void sort( ) ;
void display( ) ;

};

array :: array( )

count = 0 ;

for ( inti = 0 ; i< MAX ; i++ )

arr[i] = 0 ;

void array :: add ( int item )

if ( count < MAX )

arr[count] = item ;

count++ ;

else

cout<< "\nArray is full" <<endl ;

void array :: sort( )


{

int temp ;

for ( inti = 0 ; i<= count - 2 ; i++ )

for ( int j = i + 1 ; j <= count - 1 ; j++ )

if ( arr[i] >arr[j] )

temp = arr[i] ;

arr[i] = arr[j] ;

arr[j] = temp ;

void array :: display( )

for ( inti = 0 ; i< count ; i++ )

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


cout<<endl ;

void main( )

array a ;

a.add ( 25 ) ;

a.add ( 17 ) ;

a.add ( 31 ) ;

a.add ( 13 ) ;

a.add ( 2 ) ;

cout<< "\nSelection sort.\n" ;

cout<< "\nArray before sorting:" <<endl ;

a.display( ) ;

a.sort( ) ;

cout<< "\nArray after selection sorting:" <<endl ;

a.display( ) ;

}
Output:

array before sorting :

25 17 31 13 2

Array after selection sorting:

2 13 17 25 31
9(c)Merge sort program in c++#include <iostream.h>
const int MAX = 5 ;
class array
{
private :
int *a ;
int size ;
int count ;
public :
array( ) ;
array ( int sz ) ;
void add ( int num ) ;
void display( ) ;
void merge_sort(int low,int high);
void merge(int low,int mid,int high);
~array( ) ;
};
array :: array( )
{
count = size = 0 ;
a = NULL ;
}
array :: array( int sz )
{
count = 0 ;
size = sz ;
a = new int[sz] ;
}
void array :: add ( int num )
{
if ( count < size )
{
a[count] = num ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << a[i] << "\t" ;
cout << endl ;
}
void array :: merge_sort(int low,int high)
{

int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void array :: merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;

while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
array :: ~array( )
{
delete a ;
}

int main( )
{
array a ( MAX ) ;

a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
cout << "\nMerge sort.\n" ;

a.merge_sort (0,4) ;

cout << "\nArray after sorting: " << endl ;


a.display( ) ;
return 0;
}

Output:

Merge sort.

Array after sorting:


2 9 11 13 57
1(a) singly linked list program In c++

#include<iostream>

class Node {

public:

int key;

int data;

Node * next;

Node() {

key = 0;

data = 0;

next = NULL;

Node(int k, int d) {
key = k;

data = d;

};

class SinglyLinkedList {

public:

Node * head;

SinglyLinkedList() {

head = NULL;

SinglyLinkedList(Node * n) {

head = n;

// 1. CHeck if node exists using key value

Node * nodeExists(int k) {

Node * temp = NULL;


Node * ptr = head;

while (ptr != NULL) {

if (ptr - > key == k) {

temp = ptr;

ptr = ptr - > next;

return temp;

// 2. Append a node to the list

void appendNode(Node * n) {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

if (head == NULL) {
head = n;

cout<< "Node Appended" <<endl;

} else {

Node * ptr = head;

while (ptr - > next != NULL) {

ptr = ptr - > next;

ptr - > next = n;

cout<< "Node Appended" <<endl;

// 3. Prepend Node - Attach a node at the start

void prependNode(Node * n) {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {
n - > next = head;

head = n;

cout<< "Node Prepended" <<endl;

// 4. Insert a Node after a particular node in the list

void insertNodeAfter(int k, Node * n) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value: " << k <<endl;

} else {

if (nodeExists(n - > key) != NULL) {

cout<< "Node Already exists with key value : " << n - > key << ".
Append another node with different Key value" <<endl;

} else {

n - > next = ptr - > next;

ptr - > next = n;

cout<< "Node Inserted" <<endl;


}

// 5. Delete node by unique key

void deleteNodeByKey(int k) {

if (head == NULL) {

cout<< "Singly Linked List already Empty. Cant delete" <<endl;

} else if (head != NULL) {

if (head - > key == k) {

head = head - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;

} else {

Node * temp = NULL;

Node * prevptr = head;

Node * currentptr = head - > next;

while (currentptr != NULL) {

if (currentptr - > key == k) {

temp = currentptr;
currentptr = NULL;

} else {

prevptr = prevptr - > next;

currentptr = currentptr - > next;

if (temp != NULL) {

prevptr - > next = temp - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;

} else {

cout<< "Node Doesn't exist with key value : " << k <<endl;

// 6th update node

void updateNodeByKey(int k, int d) {


Node * ptr = nodeExists(k);

if (ptr != NULL) {

ptr - > data = d;

cout<< "Node Data Updated Successfully" <<endl;

} else {

cout<< "Node Doesn't exist with key value : " << k <<endl;

// 7th printing

void printList() {

if (head == NULL) {

cout<< "No Nodes in Singly Linked List";

} else {

cout<<endl<< "Singly Linked List Values : ";

Node * temp = head;

while (temp != NULL) {


cout<< "(" << temp - > key << "," << temp - > data << ") --> ";

temp = temp - > next;

};

int main() {

SinglyLinkedList s;

int option;

int key1, k1, data1;

do {

cout<< "\nWhat operation do you want to perform? Select Option


number. Enter 0 to exit." <<endl;

cout<< "1. appendNode()" <<endl;

cout<< "2. prependNode()" <<endl;


cout<< "3. insertNodeAfter()" <<endl;

cout<< "4. deleteNodeByKey()" <<endl;

cout<< "5. updateNodeByKey()" <<endl;

cout<< "6. print()" <<endl;

cout<< "7. Clear Screen" <<endl<<endl;

cin>> option;

Node * n1 = new Node();

//Node n1;

switch (option) {

case 0:

break;

case 1:

cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;


n1 - > data = data1;

s.appendNode(n1);

//cout<<n1.key<<" = "<<n1.data<<endl;

break;

case 2:

cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

s.prependNode(n1);

break;

case 3:

cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;

cin>> k1;
cout<< "Enter key & data of the New Node first: " <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

s.insertNodeAfter(k1, n1);

break;

case 4:

cout<< "Delete Node By Key Operation - \nEnter key of the Node to be


deleted: " <<endl;

cin>> k1;

s.deleteNodeByKey(k1);

break;

case 5:
cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;

cin>> key1;

cin>> data1;

s.updateNodeByKey(key1, data1);

break;

case 6:

s.printList();

break;

case 7:

system("cls");

break;

default:

cout<< "Enter Proper Option number " <<endl;

} while (option != 0);


return 0;

Output​:

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Append node operation

enter key and data of the node to be appended

Node appended
What operation do you want to perform? Select option nunber.Enter 0
to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Prepend node operation

Enter key and data of the node to be prepended

Node already exists with key value? Prepend another node with
different key value

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()
3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Prepend node operation

Enter key and data of the node to be prepended

Node prepended

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()
7.clearscreen

Insert node after operation

Enter key of existing node after which you want to insert this new node

Enter key and data of the new node first:

Node inserted

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

4
Delete node by key operation

Enter key of the node to be deleted

Node unlinked with key value: 7

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Update node by key operation

Enter key and new node to be updated

10

Node data updated successfully


What operation do you want to perform? Select option nunber.Enter 0
to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Singly linked list values are (9,7)à(4,5)à(5,10)à(1,9)à

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()
7.clearscreen

1(c) circular linked list program in c++

#include<iostream>

class Node {

public:

int key;

int data;

Node * next;

Node() {

key = 0;

data = 0;
next = NULL;

Node(int k, int d) {

key = k;

data = d;

};

class CircularLinkedList {

public:

Node * head;

CircularLinkedList() {

head = NULL;

// 1. CHeck if node exists using key value

Node * nodeExists(int k) {
Node * temp = NULL;

Node * ptr = head;

if (ptr == NULL) {

return temp;

} else {

do {

if (ptr - > key == k) {

temp = ptr;

ptr = ptr - > next;

} while (ptr != head);

return temp;

//return temp;

}
// 2. Append a node to the list

void appendNode(Node * new_node) {

if (nodeExists(new_node - > key) != NULL) {

cout<< "Node Already exists with key value : " <<

new_node - > key <<

". Append another node with different Key value" <<

endl;

} else {

if (head == NULL) {

head = new_node;

new_node - > next = head;

cout<< "Node Appended at first Head position" <<endl;

} else {

Node * ptr = head;

while (ptr - > next != head) {

ptr = ptr - > next;

ptr - > next = new_node;

new_node - > next = head;


cout<< "Node Appended" <<endl;

// 3. Prepend Node - Attach a node at the start

void prependNode(Node * new_node) {

if (nodeExists(new_node - > key) != NULL) {

cout<< "Node Already exists with key value : " <<

new_node - > key <<

". Append another node with different Key value" <<

endl;

} else {

if (head == NULL) {

head = new_node;

new_node - > next = head;

cout<< "Node Prepended at first Head position" <<endl;

} else {

Node * ptr = head;


while (ptr - > next != head) {

ptr = ptr - > next;

ptr - > next = new_node;

new_node - > next = head;

head = new_node;

cout<< "Node Prepended" <<endl;

// 4. Insert a Node after a particular node in the list

void insertNodeAfter(int k, Node * new_node) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value OF: " << k <<endl;

} else {
if (nodeExists(new_node - > key) != NULL) {

cout<< "Node Already exists with key value : " <<

new_node - > key <<

". Append another node with different Key value" <<

endl;

} else {

if (ptr - > next == head) {

new_node - > next = head;

ptr - > next = new_node;

cout<< "Node Inserted at the End" <<endl;

} else {

new_node - > next = ptr - > next;

ptr - > next = new_node;

cout<< "Node Inserted in between" <<endl;

}
// 5. Delete node by unique key

void deleteNodeByKey(int k) {

Node * ptr = nodeExists(k);

if (ptr == NULL) {

cout<< "No node exists with key value OF : " << k <<

endl;

} else {

if (ptr == head) {

if (head - > next == NULL) {

head = NULL;

cout<< "Head node Unlinked... List Empty";

} else {

Node * ptr1 = head;

while (ptr1 - > next != head) {

ptr1 = ptr1 - > next;

ptr1 - > next = head - > next;


head = head - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;

} else {

Node * temp = NULL;

Node * prevptr = head;

Node * currentptr = head - > next;

while (currentptr != NULL) {

if (currentptr - > key == k) {

temp = currentptr;

currentptr = NULL;

} else {

prevptr = prevptr - > next;

currentptr = currentptr - > next;

prevptr - > next = temp - > next;

cout<< "Node UNLINKED with keys value : " << k <<endl;


}

// 6th update node

void updateNodeByKey(int k, intnew_data) {

Node * ptr = nodeExists(k);

if (ptr != NULL) {

ptr - > data = new_data;

cout<< "Node Data Updated Successfully" <<endl;

} else {

cout<< "Node Doesn't exist with key value : " << k <<endl;

}
// 7th printing

void printList() {

if (head == NULL) {

cout<< "No Nodes in Circular Linked List";

} else {

cout<<endl<< "head address : " << head <<endl;

cout<< "Circular Linked List Values : " <<endl;

Node * temp = head;

do {

cout<< "(" << temp - > key << "," << temp - > data << "," << temp - >
next << ") --> ";

temp = temp - > next;

} while (temp != head);

}
};

int main() {

CircularLinkedListobj;

int option;

int key1, k1, data1;

do {

cout<< "\nWhat operation do you want to perform? Select Option


number. Enter 0 to exit." <<endl;

cout<< "1. appendNode()" <<endl;

cout<< "2. prependNode()" <<endl;

cout<< "3. insertNodeAfter()" <<endl;

cout<< "4. deleteNodeByKey()" <<endl;

cout<< "5. updateNodeByKey()" <<endl;

cout<< "6. print()" <<endl;

cout<< "7. Clear Screen" <<endl<<endl;

cin>> option;
Node * n1 = new Node();

//Node n1;

switch (option) {

case 0:

break;

case 1:

cout<< "Append Node Operation \nEnter key & data of the Node to be
Appended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.appendNode(n1);

//cout<<n1.key<<" = "<<n1.data<<endl;

break;

case 2:
cout<< "Prepend Node Operation \nEnter key & data of the Node to be
Prepended" <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;

obj.prependNode(n1);

break;

case 3:

cout<< "Insert Node After Operation \nEnter key of existing Node after
which you want to Insert this New node: " <<endl;

cin>> k1;

cout<< "Enter key & data of the New Node first: " <<endl;

cin>> key1;

cin>> data1;

n1 - > key = key1;

n1 - > data = data1;


obj.insertNodeAfter(k1, n1);

break;

case 4:

cout<< "Delete Node By Key Operation - \nEnter key of the Node to be


deleted: " <<endl;

cin>> k1;

obj.deleteNodeByKey(k1);

break;

case 5:

cout<< "Update Node By Key Operation - \nEnter key & NEW data to
be updated" <<endl;

cin>> key1;

cin>> data1;

obj.updateNodeByKey(key1, data1);

break;
case 6:

obj.printList();

break;

case 7:

system("cls");

break;

default:

cout<< "Enter Proper Option number " <<endl;

} while (option != 0);

return 0;

Output:

What operation do you want to perform? Select option nunber.Enter 0


to exit
1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Append node operation

Enter key and data of the node to be appended

5 8

Node appended at first head position

What operation do you want to perform? Select option number.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()
6.print()

7.clearscreen

Prepend node operation

Enter key and data of the node to be prepended

6 4

Node prepended

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Inseet node after operation


Enter key and existing node after which you want to insert this new
node

Enter key and data of the new node first:

7 7

Node inserted at the end

What operation do you want to perform? Select option nunber.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Delete node by key operation

Enter key of the node to be deleted :

5
Node unlinked with key value:5

What operation do you want to perform? Select option number.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Update node by key operation

Enter key and new node to be updated

5 6

Node data updated successfully

What operation do you want to perform? Select option number.Enter 0


to exit

1.appendnode()

2.prependnode()
3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

Head address:0x656b30

Circular linked list values are :(6,6)à (7,7)à

What operation do you want to perform? Select option number.Enter 0


to exit

1.appendnode()

2.prependnode()

3.insertnodeafter()

4.deletenodebykey()

5.updatenodebykey()

6.print()

7.clearscreen

0
9(d) Quick sort program in c++#include<iostream.h>

constant MAX = 10 ;
class array
{
private :

intarr[MAX] ;
int count ;
public :

array( ) ;
void add ( int item ) ;
int getcount( ) ;
static int split ( int *, int, int ) ;
void quicksort ( int lower, int upper ) ;
void display( ) ;
};
array :: array( )
{
count = 0 ;
for ( inti = 0 ; i< MAX ; i++ )
arr[i] = 0 ;
}
void array :: add ( int item )
{
if ( count < MAX )
{
arr[count] = item ;
count++ ;
}
else
cout<< "\nArray is full" <<endl ;
}
int array :: getcount( )
{
return count ;
}
void array :: quiksort ( int lower, int upper )
{
if ( upper > lower )
{
inti = split ( arr, lower, upper ) ;
quicksort ( lower, i - 1 ) ;
quicksort ( i + 1, upper ) ;
}
}
int array :: split ( int *a, int lower, int upper )
{
inti, p, q, t ;

p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] <i )
p++ ;
while ( a[q] >i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
void array :: display( )
{
for ( inti = 0 ; i< count ; i++ )
cout<<arr[i] << " " ;
cout<<endl ;
}
int main( )
{
array a ;
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3 ) ;
cout<< "\nQuik sort.\n" ;
cout<< "\nArray before sorting:" <<endl ;
a.display( ) ;
int c = a.getcount( ) ;
a.quicksort ( 0, c - 1 ) ;
cout<< "\nArray after quick sorting:" <<endl ;
a.display( ) ;
return 0;
}

Output :

Quik sort.

Array before sorting:


11 2 9 13 57 25 17 1 90 3

Array after quick sorting:


1 2 3 9 11 13 17 25 57 90
23)Implementation of traversal on graphs

23)a)Breadth first search.

#include <iostream>

#include <list>

#include <memory>

class Graph

int _V;

bool _directed;

std::unique_ptr< std::list<int> > adj;

public:

Graph(int V, bool directed);

void AddEdge(int v, int w);

void BreadthFirstSearch(int s);

};
Graph::Graph(int V, bool directed) : adj(new std::list<int>[V])

_V = V;

_directed = directed;

void Graph::AddEdge(int v, int w)

std::list<int>* adjacency = adj.get();

adjacency[v].push_back(w);

if (!_directed)

adjacency[w].push_back(v);

void Graph::BreadthFirstSearch(int s)
{

bool *visited = new bool[_V];

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

visited[i] = false;

// Create a queue for BFS

std::list<int> queue;

visited[s] = true;

queue.push_back(s);

// 'i' will be used to get all adjacent vertices of a vertex

std::list<int>::iterator i;

while(!queue.empty())

// Dequeue a vertex from queue and print it

s = queue.front();

std::cout << s << " ";


queue.pop_front();

// Get all adjacent vertices of the dequeued vertex s

// If a adjacent has not been visited, then mark it visited

// and enqueue it

for(i = (adj.get())[s].begin(); i != (adj.get())[s].end(); ++i)

if(!visited[*i])

visited[*i] = true;

queue.push_back(*i);

int main()

Graph g(7, true);


g.AddEdge(0, 1);

g.AddEdge(0, 2);

g.AddEdge(0, 3);

g.AddEdge(1, 0);

g.AddEdge(1, 5);

g.AddEdge(2, 5);

g.AddEdge(3, 0);

g.AddEdge(3, 4);

g.AddEdge(4, 6);

g.AddEdge(5, 1);

g.AddEdge(6, 5);

std::cout << "Breadth First Traversal from vertex 2:\n";

g.BreadthFirstSearch(2);

return 0;

Output:​-
Breadth First Traversal from vertex 2:
2510346

23)b)Depth first search

#include <iostream>

#include <list>

#include <memory>
class Graph

private:

int _V;

bool _directed;

std::unique_ptr< std::list<int> > adj;

void DFSUtil(int v, bool visited[]);

public:

Graph(int V, bool directed);

void AddEdge(int v, int w);

void DepthFirstSearch(int s);

};

Graph::Graph(int V, bool directed) : adj(new std::list<int>[V])

_V = V;

_directed = directed;

}
void Graph::AddEdge(int v, int w)

std::list<int>* adjacency = adj.get();

adjacency[v].push_back(w);

if (!_directed)

adjacency[w].push_back(v);

void Graph::DFSUtil(int v, bool visited[])

// Mark the current node as visited and print it

visited[v] = true;

std::cout << v << " ";

// Recur for all the vertices adjacent to this vertex


std::list<int>::iterator i;

for (i = (adj.get())[v].begin(); i != (adj.get())[v].end(); ++i)

if (!visited[*i])

DFSUtil(*i, visited);

// DFS traversal of the vertices reachable from v. It uses recursive


DFSUtil()

void Graph::DepthFirstSearch(int v)

// Mark all the vertices as not visited

std::unique_ptr<bool[]> visited(new bool[_V]);

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

visited[i] = false;

// Call the recursive helper function to print DFS traversal

DFSUtil(v, visited.get());

}
int main()

// Create a graph given in the above diagram

Graph g(7, true);

g.AddEdge(0, 1);

g.AddEdge(0, 2);

g.AddEdge(0, 3);

g.AddEdge(1, 0);

g.AddEdge(1, 5);

g.AddEdge(2, 5);

g.AddEdge(3, 0);

g.AddEdge(3, 4);

g.AddEdge(4, 6);

g.AddEdge(5, 1);

g.AddEdge(6, 5);

std::cout << "Depth First Traversal starting from vertex 2:\n";

g.DepthFirstSearch(2);
return 0;

Output:​-

Depth First Traversal starting from vertex 2:


2510346
4) a) stack using array
#include<iostream>
using namespace std;

class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout<<"stack over flow";
return;
}
stk[++top]=x;
cout<<"inserted"<<x;
}
void pop()
{
if(top <0)
{ cout <<"stack under flow";
return;
}
cout<<"deleted" <<stk [top--];
}
void display()
{
if(top<0)
{
cout <<"stack empty";
return;
}
for(int i=top; i>=0; i--)
cout<<stk[i] <<" ";
}
};
int main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return(0);
}

Output:-

1.push 2.pop 3.display 4.exit

Enter ur choice1

enter the element23

inserted23

inserted45

1.push 2.pop 3.display 4.exit

Enter ur choice3

45 78 23

1.push 2.pop 3.display 4.exit

Enter ur choice2

deleted45

1.push 2.pop 3.display 4.exit

Enter ur choice2
deleted78

1.push 2.pop 3.display 4.exit

Enter ur choice3

23

1.push 2.pop 3.display 4.exit

Enter ur choice4
4) b) stack using linked list

#include <iostream>

using namespace std;

struct Node {

int data;

struct Node *next;

};

struct Node* top = NULL;

void push(int val) {

struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));

newnode->data = val;

newnode->next = top;

top = newnode;

void pop() {

if(top==NULL)

cout<<"Stack Underflow"<<endl;

else {

cout<<"The popped element is "<< top->data <<endl;


top = top->next;

void display() {

struct Node* ptr;

if(top==NULL)

cout<<"stack is empty";

else {

ptr = top;

cout<<"Stack elements are: ";

while (ptr != NULL) {

cout<< ptr->data <<" ";

ptr = ptr->next;

cout<<endl;

int main() {

int ch, val;


cout<<"1) Push in stack"<<endl;

cout<<"2) Pop from stack"<<endl;

cout<<"3) Display stack"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter choice: "<<endl;

cin>>ch;

switch(ch) {

case 1: {

cout<<"Enter value to be pushed:"<<endl;

cin>>val;

push(val);

break;

case 2: {

pop();

break;

case 3: {
display();

break;

case 4: {

cout<<"Exit"<<endl;

break;

default: {

cout<<"Invalid Choice"<<endl;

}while(ch!=4);

return 0;

Output:-

1) Push in stack

2) Pop from stack


3) Display stack

4) Exit

Enter choice:

Enter value to be pushed:

Enter choice:

Enter value to be pushed:

Enter choice:

Stack elements are: 4 2

Enter choice:

The popped element is 4

Enter choice:

2
The popped element is 2

Enter choice:

stack is empty

Enter choice:

Exit

10) hashing program


#include<iostream.h>

#include<stdlib.h>

#include<string.h>

#include<stdio.h>

using namespace std;

constant T_S =200;

class HashTableEntry{

public:

int k;

int v;

HashTableEntry(int k,int v){

this->k= k;

this->v = v;

};

class HashMapTable{

private:

HashTableEntry**t;
public:

HashMapTable(){

t =new HashTableEntry*[T_S];

for(int i=0;i< T_S;i++){

t[i]= NULL;

int HashFunc(int k){

return k % T_S;

void Insert(int k,int v){

int h =HashFunc(k);

while(t[h]!= NULL && t[h]->k != k){

h =HashFunc(h +1);

if(t[h]!= NULL)

delete t[h];

t[h]=new HashTableEntry(k, v);

}
int SearchKey(int k){

int h =HashFunc(k);

while(t[h]!= NULL && t[h]->k != k){

h =HashFunc(h +1);

if(t[h]== NULL)

return-1;

else

return t[h]->v;

void Remove(int k){

int h =HashFunc(k);

while(t[h]!= NULL){

if(t[h]->k == k)

break;

h =HashFunc(h +1);

if(t[h]== NULL){

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


return;

}else{

delete t[h];

cout<<"Element Deleted"<<endl;

~HashMapTable(){

for(inti=0;i< T_S;i++){

if(t[i]!= NULL)

delete t[i];

delete[] t;

};

int main(){

HashMapTable hash;

int k, v;

int c;

while(1){
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>>c;

switch(c){

case1:

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

cin>>v;

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

cin>>k;

hash.Insert(k, v);

break;

case2:

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

cin>>k;

if(hash.SearchKey(k)==-1){

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


continue;

}else{

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

cout<<hash.SearchKey(k)<<endl;

break;

case3:

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

cin>>k;

hash.Remove(k);

break;

case4:

exit(1);

default:

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

return0;

}
Output

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: 1

Enter key at which element to be inserted: 1

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: 2

Enter key at which element to be inserted: 2

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: 4

Enter key at which element to be inserted: 5

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: 7

Enter key at which element to be inserted: 6

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: 7

No element found at key 7

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: 6

Element at key 6 : 7

1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 3

Enter key of the element to be deleted: 1

Element Deleted

1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 4


10)hashing program

#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
constant T_S =200;
class HashTableEntry{
public:
int k;
int v;
HashTableEntry(int k,int v){
this->k= k;
this->v = v;
}
};
class HashMapTable{
private:
HashTableEntry**t;
public:
HashMapTable(){
t =newHashTableEntry*[T_S];
for(inti=0;i< T_S;i++){
t[i]= NULL;
}
}
int HashFunc(int k){
return k % T_S;
}
void Insert(int k,int v){
int h =HashFunc(k);
while(t[h]!= NULL && t[h]->k != k){
h =HashFunc(h +1);
}
if(t[h]!= NULL)
delete t[h];
t[h]=newHashTableEntry(k, v);
}
int SearchKey(int k){
int h =HashFunc(k);
while(t[h]!= NULL && t[h]->k != k){
h =HashFunc(h +1);
}
if(t[h]== NULL)
return-1;
else
return t[h]->v;
}
voidRemove(int k){
int h =HashFunc(k);
while(t[h]!= NULL){
if(t[h]->k == k)
break;
h =HashFunc(h +1);
}
if(t[h]== NULL){
cout<<"No Element found at key "<<k<<endl;
return;
}else{
delete t[h];
}
cout<<"Element Deleted"<<endl;
}
~HashMapTable(){
for(inti=0;i< T_S;i++){
if(t[i]!= NULL)
delete t[i];
delete[] t;
}
}
};
int main(){
HashMapTable hash;
int k, v;
int c;
while(1){
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>>c;
switch(c){
case1:
cout<<"Enter element to be inserted: ";
cin>>v;
cout<<"Enter key at which element to be inserted: ";
cin>>k;
hash.Insert(k, v);
break;
case2:
cout<<"Enter key of the element to be searched: ";
cin>>k;
if(hash.SearchKey(k)==-1){
cout<<"No element found at key "<<k<<endl;
continue;
}else{
cout<<"Element at key "<<k<<" : ";
cout<<hash.SearchKey(k)<<endl;
}
break;
case3:
cout<<"Enter key of the element to be deleted: ";
cin>>k;
hash.Remove(k);
break;
case4:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return0;
}
Output
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: 1
Enter key at which element to be inserted: 1
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: 2
Enter key at which element to be inserted: 2
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: 4
Enter key at which element to be inserted: 5
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: 7
Enter key at which element to be inserted: 6
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: 7
No element found at key 7
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: 6
Element at key 6 : 7
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 3
Enter key of the element to be deleted: 1
Element Deleted
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 4
19)Write a program on implementation of heap sort
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
Const int MAX = 10 ;
class array
{
private :
intarr[MAX] ;
int count ;
public :
array( ) ;
void add ( intnum ) ;
void makeheap(int ) ;
void heapsort( ) ;
void display( ) ;
};
array :: array( )
{
count = 0 ;
for ( inti = 0 ; i< MAX ; i++ )
arr[MAX] = 0 ;
}
void array :: add ( intnum )
{
if ( count < MAX )
{
arr[count] = num ;
count++ ;
}
else
cout<< "\nArray is full" <<endl ;
}
void array :: makeheap(int c)
{

for ( inti = 1 ; i< c ; i++ )


{
intval = arr[i] ;
int s = i ;
int f = ( s - 1 ) / 2 ;
while ( s > 0 &&arr[f] <val )
{
arr[s] = arr[f] ;
s=f;
f=(s-1)/2;
}
arr[s] = val ;
}
}
void array :: heapsort( )
{
for ( inti = count - 1 ; i> 0 ; i-- )
{
intivalue = arr[i] ;
arr[i] = arr[0] ;
arr[0]=ivalue;
makeheap(i);

}
}
void array :: display( )
{
for ( inti = 0 ; i< count ; i++ )
cout<<arr[i] << "\t" ;
cout<<endl ;
}
void main( )
{
array a ;

a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3 ) ;
a.makeheap(10) ;
cout<< "\nHeap Sort.\n" ;
cout<< "\nBefore Sorting:\n" ;
a.display( ) ;
a.heapsort( ) ;
cout<< "\nAfter Sorting:\n" ;
a.display( ) ;
getch();
}

Output :

Heap sort.

Before sorting:

90 57 25 13 11 9 17 1 2 3

After sorting :

1 2 3 9 11 13 17 25 57 90
16) Write a program to implement shell sort.

//C++ program for shell sort

#include <iostream.h>

void shell_sort (int a[ ], int size )

int temp , gap ,i ,swap ;

gap = size /2 ;

do

do

swap =0;

for ( i=0 ; i< size-gap ; i ++)

if(a[i] > a[ i+ gap])


{

temp = a[i] ;

a[i] = a [i+ gap];

a[ i+ gap]= temp;

swap=1;

}}

}while ( swap ==1);

gap= gap/2 ;

}while (gap >0) ;

Intmain()

intarr[10],i,k;

cout<<"Enter 10 values\n";

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

cin>>arr[i];

//call of shell sort function

shell_sort(arr,10);

cout<<" \n Sorted Values \n";


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

cout<<endl<<arr[i];

return 0 ;

Output:

Enter 10 values

44

12

45

34

Sorted values

2
3

12

34

44

45
24) Implementation of B-tree

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

struct node{

int value;

node *left;

node *right;

};

class btree

public:

btree();

~btree();

void insert(int key);


node *search(int key);

void destroy_tree();

void inorder_print();

void postorder_print();

void preorder_print();

private:

void destroy_tree(node *leaf);

void insert(int key, node *leaf);

node *search(int key, node *leaf);

void inorder_print(node *leaf);

void postorder_print(node *leaf);

void preorder_print(node *leaf);

node *root;

};

btree::btree(){

root=NULL;

btree::~btree(){

destroy_tree();
}

void btree::destroy_tree(node *leaf){

if(leaf != NULL){

destroy_tree(leaf->left);

destroy_tree(leaf->right);

delete leaf;

void btree::insert(int key, node *leaf){

if(key < leaf->value){

if(leaf->left != NULL){

insert(key , leaf->left);

else

leaf->left = new node;

leaf->left->value = key;

leaf->left->left = NULL;

leaf->left->right = NULL;
}

else if(key >= leaf->value){

if(leaf->right != NULL){

insert(key, leaf->right);

else

leaf->right = new node;

leaf->right->value = key;

leaf->right->right = NULL;

leaf->right->left = NULL;

void btree::insert(int key){

if(root != NULL){

insert(key, root);

}else{
root = new node;

root->value = key;

root->left = NULL;

root->right = NULL;

node *btree::search(int key, node *leaf){

if(leaf != NULL){

if(key == leaf->value){

return leaf;

if(key < leaf->value){

return search(key, leaf->left);

}else{

return search(key, leaf->right);

}else{

return NULL;

}
}

node *btree::search(int key){

return search(key, root);

void btree::destroy_tree(){

destroy_tree(root);

void btree::inorder_print(){

inorder_print(root);

cout << "\n";

void btree::inorder_print(node *leaf){

if(leaf != NULL){

inorder_print(leaf->left);

cout << leaf->value << ",";

inorder_print(leaf->right);

void btree::postorder_print()
{

postorder_print(root);

cout << "\n";

void btree::postorder_print(node *leaf)

if(leaf != NULL){

inorder_print(leaf->left);

inorder_print(leaf->right);

cout << leaf->value << "," ;

void btree::preorder_print(){

preorder_print(root);

cout << "\n";

void btree::preorder_print(node *leaf){

if(leaf != NULL){

cout << leaf->value << ",";


inorder_print(leaf->left);

inorder_print(leaf->right);

void main(){

//btree tree;

clrscr();

btree *tree = new btree();

tree->insert(10);

tree->insert(6);

tree->insert(14);

tree->insert(5);

tree->insert(8);

tree->insert(11);

tree->insert(18);

tree->inorder_print();

tree->preorder_print();

tree->postorder_print();

delete tree;
getch();

Output:-

5,6,8,10,11,14,18,

10,5,6,8,11,14,18,

5,6,8,11,14,18,10,

You might also like