You are on page 1of 73

1.

Write a program to implement singly linked list as an ADT that supports the following
operations: i. Insert an element x at the beginning of the singly linked list ii. Insert an
element x at i th position in the singly linked list iii. Remove an element from the beginning
of the doubly linked list iv. Remove an element from i th position in the singly linked list. vi.
Search for an element x in the singly linked list and return its pointer.

Code:

#include<iostream>

using namespace std;

class snode

public:

int info;

snode *next;

snode()

next = 0;

snode(int x, snode *n = 0)

info = x;

next = n;

};

class slist

snode *head, *tail;

public:

slist()

{
head = tail = 0;

void addtohead(int);

void addtotail(int);

int deletefromhead();

int deletefromtail();

void display();

int isempty();

bool isinlist(int);

void deletenode(int);

int count();

void insertnode(int, int);

void deletefromi(int);

~slist();

};

int slist :: isempty()

if (head == 0)

return 1;

else

return 0;

void slist :: addtohead(int x)

snode *p = new snode(x);

if (isempty())

head = tail = p;

else

{
p->next = head;

head = p;

void slist :: addtotail(int x)

snode *p = new snode(x);

if (isempty())

head = tail = p;

else

tail->next = p;

tail = p;

int slist :: deletefromhead()

int x = head->info;

if (head == tail)

delete head;

head = tail = 0;

else

snode *p = head;

head = head->next;

delete p;

}
return x;

int slist :: deletefromtail()

int x = tail->info;

if (head == tail)

delete tail;

head = tail = 0;

else

snode *p = head;

while(p->next != tail)

p = p->next;

delete tail;

tail = p;

tail->next = 0;

return x;

void slist :: display()

snode *p = head;

while(p!=0)

cout<<p->info<<"->";

p = p->next;

}
}

bool slist :: isinlist(int x)

snode *p = head;

bool flag = false;

while(p!=0)

if (x==p->info)

flag = true;

break;

else

p = p->next;

return flag;

int slist :: count()

int count = 0;

snode *p = head;

while(p!=0)

count += 1;

p = p->next;

return count;

}
void slist :: deletenode(int e)

if (head == tail && head->info == e)

delete head;

head = tail = 0;

else

if (head->info == e)

snode *p = head;

head = head->next;

delete p;

else

snode *prev = head;

snode *p = head->next;

while(p!=0 && p->info != e)

prev = p;

p = p->next;

if (p!=0)

prev->next = p->next;

if (p == tail)

tail = prev;

delete p;

}
else

cout<<"Element not present";

void slist :: insertnode(int x, int n)

int c;

c = count();

if (n == 1)

addtohead(x);

else if (n==c+1)

addtotail(x);

else

snode *p = new snode(x);

snode *temp = head;

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

if (temp!=0)

temp = temp->next;

if (temp!=0)

p->next = temp->next;

temp->next = p;

else

cout<<"Invalid index!";
}

void slist :: deletefromi(int x)

snode *prev = head;

snode *temp = prev->next;

int i = 1;

int z;

int y = count();

if (x==1)

z = deletefromhead();

cout<<"Deleted element: "<<z<<endl;

else if (x==y)

z = deletefromtail();

cout<<"Deleted element: "<<z<<endl;

else

while(i<x-1 && temp!=0)

temp = temp->next;

prev = prev->next;

i++;

if (temp!=0 && temp->next != 0)


{

int x = temp->info;

prev->next = temp->next;

delete temp;

cout<<"Deleted element is: "<<x<<endl;

else if (temp!=0)

int x = temp->info;

prev->next = 0;

delete temp;

cout<<"Deleted element is: "<<x<<endl;

else

cout<<"Invalid index!\n";

slist :: ~slist()

snode *temp = head;

while(head != 0)

head = head->next;

delete temp;

temp = head;

}
int main()

char choice;

slist s;

bool b;

int ch, x, n, a;

do

cout<<"\n****\n";

cout<<" MENU \n";

cout<<"1.Add to head\n";

cout<<"2.Add to tail\n";

cout<<"3.Delete from head\n";

cout<<"4.Delete from tail\n";

cout<<"5.Display\n";

cout<<"6.Isempty\n";

cout<<"7.Isinlist\n";

cout<<"8.Delete node\n";

cout<<"9.Count\n";

cout<<"10.Insert a node\n";

cout<<"11.Delete node from i position";

cout<<"\n****\n\n";

cout<<"Enter your choice: ";

cin>>ch;

if (ch == 1 || ch==2 || ch==7 || ch==8 || ch==10)

cout<<"Enter the value: ";

cin>>x;

if (ch==10 || ch==11)

{
cout<<"Enter the position: ";

cin>>n;

switch(ch)

case 1 : s.addtohead(x);

cout<<"The list is: ";

s.display();

cout<<endl;

break;

case 2 : s.addtotail(x);

cout<<"The list is: ";

s.display();

cout<<endl;

break;

case 3 : if (s.isempty())

cout<<"The list is empty";

else

a = s.deletefromhead();

cout<<"Deleted element: "<<a<<endl;

cout<<"The list is: ";

s.display();

cout<<endl;

break;

case 4 : if (s.isempty())

cout<<"The list is empty";

else

a = s.deletefromtail();
cout<<"Deleted element: "<<a<<endl;

cout<<"The list is: ";

s.display();

cout<<endl;

break;

case 5 : if (s.isempty())

cout<<"The list is empty\n";

else

s.display();

cout<<endl;

break;

case 6 : a = s.isempty();

if (a==0)

cout<<"The list is not empty";

else

cout<<"The list is empty";

cout<<endl;

break;

case 7 : if (s.isempty())

cout<<"The list is empty";

else

b = s.isinlist(x);

if (b==1)

cout<<x<<" is present in the list";

else

cout<<x<<" is not present in the list";

}
cout<<endl;

break;

case 8 : s.deletenode(x);

cout<<endl;

break;

case 9 : a = s.count();

cout<<"Number of elements in the list: "<<a<<endl;

break;

case 10 : s.insertnode(x, n);

cout<<endl;

break;

case 11 : if (s.isempty())

cout<<"The list is empty";

else

s.deletefromi(n);

cout<<"The list is: ";

s.display();

cout<<"\nDo you want to continue?(Y/N): ";

cin>>choice;

} while(choice == 'y' || choice == 'Y');

Output:

/**

****

MENU

1.Add to head
2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 1

Enter the value: 3

The list is: 3->

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position


****

Enter your choice: 1

Enter the value: 4

The list is: 4->3->

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 1

Enter the value: 7

The list is: 7->4->3->

Do you want to continue?(Y/N): Y

****

MENU
1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 2

Enter the value: 8

The list is: 7->4->3->8->

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node
11.Delete node from i position

****

Enter your choice: 2

Enter the value: 6

The list is: 7->4->3->8->6->

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 2

Enter the value: 5

The list is: 7->4->3->8->6->5->

Do you want to continue?(Y/N): Y

****
MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 3

Deleted element: 7

The list is: 4->3->8->6->5->

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count
10.Insert a node

11.Delete node from i position

****

Enter your choice: 4

Deleted element: 5

The list is: 4->3->8->6->

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 6

The list is not empty

Do you want to continue?(Y/N): Y

****
MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 7

Enter the value: 5

5 is not present in the list

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count
10.Insert a node

11.Delete node from i position

****

Enter your choice: 8

Enter the value: 4

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 10

Enter the value: 3

Enter the position: 2

Do you want to continue?(Y/N): Y


****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist

8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 11

Enter the position: 6

Invalid index!

The list is: 3->3->8->6->

Do you want to continue?(Y/N): Y

****

MENU

1.Add to head

2.Add to tail

3.Delete from head

4.Delete from tail

5.Display

6.Isempty

7.Isinlist
8.Delete node

9.Count

10.Insert a node

11.Delete node from i position

****

Enter your choice: 5

3->3->8->6->

Do you want to continue?(Y/N): N

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

Process exited after 151.7 seconds with return value 0

Press any key to continue . . .

*/

2. Write a program to implement doubly linked list as an ADT that supports the following
operations: i. Insert an element x at the beginning of the doubly linked list ii. Insert an
element x at the end of the doubly linked list iii. Remove an element from the beginning of
the doubly linked list iv. Remove an element from the end of the doubly linked list.
code:
#include<iostream>
//#include<std bool.h>
using namespace std;
class dnode{
public:
int info;
dnode *next;
dnode *prev;
dnode(){

prev=next=0;
}
dnode(int x,dnode* n=0,dnode *p=0)
{info=x;
next=n;
prev=p;
}
};

class dlist:public dnode{


dnode*head,*tail;
public:
dlist()
{head=tail=0;
}
void addtohead(int);
void addtotail(int);
int deletefromhead();
int deletefromtail();
void display();
int isempty();
bool isinlist(int);
void deletenode(int);
void length();
~dlist();

};
int dlist::isempty(){
if(head==0){
return 1;
}
else{
return 0;
}
}
void dlist::addtohead(int x){
dnode *p=new dnode(x);
if(isempty()){
head=tail=p;
}
else{
p->next=head;
head->prev=p;
head=p;
}
}
void dlist::addtotail(int x){
dnode *p=new dnode(x);
if(isempty())
{head=tail=p;
}
else{
tail->next=p;
p->prev=tail;
tail=p;
}
}
int dlist::deletefromhead(){
int x=head->info;
if(head==tail){
delete head;
head=tail=0;
}
else{
dnode *p=head;
head=head->next;
head->prev=0;
delete p;
}
return x;
}
int dlist::deletefromtail(){
int x=tail->info;
if(head==tail)
{
delete tail;
head=tail=0;
}
else{
dnode *p=tail;
tail=tail->prev;
tail->next=0;
delete p;
}
return x;

}
void dlist::display(){
dnode *p=head;
while(p!=0){
cout<<p->info<<" -->";
p=p->next;
}
}
bool dlist::isinlist(int x){
dnode *p=head;
while(p!=0){
if(p->info==x){
return true;
}
else{
p=p->next;
}
}
if(p==0){
return false;
}
}
void dlist::length(){
dnode *p=head;
int count;
while(p!=0)
{
count++;
p=p->next;

}
cout<<"The length of the given linked list is"<< count<< " "<<" "<<endl;
}
void dlist::deletenode(int e)
{
if(head==tail && head->info==e)
{
delete head;
head=tail=0;
}
else
{
if(head->info==e)
{
dnode *p=head;
head=head->next;
delete p;
}
else
{
dnode *prev=head;
dnode *p=head->next;
if(p!=0 && p->info!=e)
{
prev=p;
p=p->next;
}
if(p!=0)
{
prev->next=p->next;
if(p==tail)
{
tail=prev;
}
delete p;
}
else
{
cout<<"Elemenet not found "<<endl;
}
}
}
}
dlist::~dlist()
{
dnode *temp=head;
while(head!=0)
{
head=head->next;
delete temp;
temp=head;
}
}
int main()
{
dlist d1;
char t;
do
{
cout<<endl<<"Enter y or Y for continue the program
";
cin>>t;
cout<<"~~~~~~~~~~"<<endl;
cout<<"MENU"<<endl;
cout<<"1.add to head"<<endl;
cout<<"2.add to tail"<<endl;
cout<<"3.delete from head"<<endl;
cout<<"4.delete from tail"<<endl;
cout<<"5.isempty"<<endl;
cout<<"6.display"<<endl;
cout<<"7.isinlist"<<endl;
cout<<"8.length"<<endl;
cout<<"9.delete node"<<endl;
cout<<"10.destructor"<<endl;
cout<<"~~~~~~~~~~"<<endl;
int s;
cout<<"Enter your choice: ";
cin>>s;
if(s==1)
{
int r;
cout<<"Enter the element ";
cin>>r;
d1.addtohead(r);
cout<<"After adding to the head "<<endl;
d1.display();

}
else if(s==2)
{
int k;
cout<<"Enter the element ";
cin>>k;
d1.addtotail(k);
cout<<"After adding to the head "<<endl;
d1.display();
}
else if(s==3)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else{
int c;
c=d1.deletefromhead();
cout<<c<<endl;
cout<<"After deleting from the head
"<<endl;
d1.display();
}
}
else if(s==4)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else{
int d;
d=d1.deletefromhead();
cout<<d<<endl;
cout<<"After deleting from the tail
"<<endl;
d1.display();
}
}
else if(s==5)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
cout<<"list is not empty"<<endl;
cout<<"List"<<endl;
d1.display();
}
}
else if(s==6)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
d1.display();
}
}
else if(s==7)
{

if(d1.isempty()==0)
{
int u;
cout<<"Enter thr element: ";
cin>>u;
if(d1.isinlist(u)==true)
{
cout<<"Element found"<<endl;
}
else
cout<<"Element not found"<<endl;
}
else
cout<<"List is empty"<<endl;

}
else if(s==8)
{
d1.length();
}
else if(s==9)
{
if(d1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
int f;
cout<<"Enter the info for deleting: ";
cin>>f;
d1.deletenode(f);
}
}
else
{
if(d1.isempty()==1)
{
cout<<"List is empty"<<endl;
}
else
d1.~dlist();
}

}
while(t=='Y'||t=='y');
if(t!='Y' || t!='y')
{

cout<<"Thanks"<<endl;
}
return 0;
}
/*
Output:

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 1
Enter the element 3
After adding to the head
3 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 1
Enter the element 5
After adding to the head
5 -->3 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 6
After adding to the head
5 -->3 -->6 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 4
After adding to the head
5 -->3 -->6 -->4 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 3
After adding to the head
5 -->3 -->6 -->4 -->3 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 1
Enter the element 8
After adding to the head
8 -->5 -->3 -->6 -->4 -->3 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 3
8
After deleting from the head
5 -->3 -->6 -->4 -->3 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 4
5
After deleting from the tail
3 -->6 -->4 -->3 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 5
list is not empty
List
3 -->6 -->4 -->3 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 7
Enter thr element: 4
Element found

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 8
The length of the given linked list is24

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 9
Enter the info for deleting: 3

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 10

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 6
list is empty

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 1
Enter the element 4
After adding to the head
4 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 6
After adding to the head
4 -->6 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 6
After adding to the head
4 -->6 -->6 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 8
After adding to the head
4 -->6 -->6 -->8 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 6
4 -->6 -->6 -->8 -->
Enter y or Y for continue the program N
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 4
After adding to the head
4 -->6 -->6 -->8 -->4 -->Thanks

--------------------------------
Process exited after 219.9 seconds with return value 0
Press any key to continue . . .
*/

3. Write a program to implement circular linked list as an ADT which supports the following
operations: i. Insert an element x in the list ii. Remove an element from the list iii. Search for
an element x in the list and return its pointer
Code:

#include<iostream>
//#include<std bool.h>
using namespace std;
class cnode{
public:
int info;
cnode *next;
cnode *prev;
cnode()
{

prev=next=0;
}
cnode(int x,cnode* n=0,cnode *p=0)
{info=x;
next=n;

}
};

class clist:public cnode{


cnode*head,*tail;
public:
clist()
{head=tail=0;
}
void addtohead(int);
void addtotail(int);
int deletefromhead();
int deletefromtail();
void display();
int isempty();
bool isinlist(int);
void deletenode(int);
void length();
~clist();

};
int clist::isempty(){
if(head==0){
return 1;
}
else{
return 0;
}
}
void clist::addtohead(int x){
cnode *p=new cnode(x);
if(isempty()){
head=tail=p;
}
else{
p->next=head;
head->prev=p;
head=p;
}
}
void clist::addtotail(int x){
cnode *p=new cnode(x);
if(isempty())
{head=tail=p;
}
else{
tail->next=p;
p->prev=tail;
tail=p;
}
}
int clist::deletefromhead(){
int x=head->info;
if(head==tail){
delete head;
head=tail=0;
}
else{
cnode *p=head;
head=head->next;
head->prev=0;
delete p;
}
return x;
}
int clist::deletefromtail(){
int x=tail->info;
if(head==tail)
{
delete tail;
head=tail=0;
}
else{
cnode *p=tail;
tail=tail->prev;
tail->next=0;
delete p;
}
return x;

}
void clist::display(){
cnode *p=head;
while(p!=0){
cout<<p->info<<" -->";
p=p->next;
}
}
bool clist::isinlist(int x){
cnode *p=head;
while(p!=0){
if(p->info==x){
return true;
}
else{
p=p->next;
}
}
if(p==0){
return false;
}
}
void clist::length(){
cnode *p=head;
int count;
while(p!=0)
{
count++;
p=p->next;

}
cout<<"The length of the given linked list is"<< count<< " "<<" "<<endl;
}
void clist::deletenode(int e)
{
if(head==tail && head->info==e)
{
delete head;
head=tail=0;
}
else
{
if(head->info==e)
{
cnode *p=head;
head=head->next;
delete p;
}
else
{
cnode *prev=head;
cnode *p=head->next;
if(p!=0 && p->info!=e)
{
prev=p;
p=p->next;
}
if(p!=0)
{
prev->next=p->next;
if(p==tail)
{
tail=prev;
}
delete p;
}
else
{
cout<<"Elemenet not found "<<endl;
}
}
}
}
clist::~clist()
{
cnode *temp=head;
while(head!=0)
{
head=head->next;
delete temp;
temp=head;
}
}
int main()
{
clist c1;
char t;
do
{
cout<<endl<<"Enter y or Y for continue the program ";
cin>>t;
cout<<"~~~~~~~~~~"<<endl;
cout<<"MENU"<<endl;
cout<<"1.add to head"<<endl;
cout<<"2.add to tail"<<endl;
cout<<"3.delete from head"<<endl;
cout<<"4.delete from tail"<<endl;
cout<<"5.isempty"<<endl;
cout<<"6.display"<<endl;
cout<<"7.isinlist"<<endl;
cout<<"8.length"<<endl;
cout<<"9.delete node"<<endl;
cout<<"10.destructor"<<endl;
cout<<"~~~~~~~~~~"<<endl;
int s;
cout<<"Enter your choice: ";
cin>>s;
if(s==1)
{
int r;
cout<<"Enter the element ";
cin>>r;
c1.addtohead(r);
cout<<"After adding to the head "<<endl;
c1.display();

}
else if(s==2)
{
int k;
cout<<"Enter the element ";
cin>>k;
c1.addtotail(k);
cout<<"After adding to the head "<<endl;
c1.display();
}
else if(s==3)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else{
int c;
c=c1.deletefromhead();
cout<<c<<endl;
cout<<"After deleting from the head "<<endl;
c1.display();
}
}
else if(s==4)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else{
int d;
d=c1.deletefromhead();
cout<<d<<endl;
cout<<"After deleting from the tail "<<endl;
c1.display();
}
}
else if(s==5)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
cout<<"list is not empty"<<endl;
cout<<"List"<<endl;
c1.display();
}
}
else if(s==6)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
c1.display();
}
}
else if(s==7)
{

if(c1.isempty()==0)
{
int u;
cout<<"Enter thr element: ";
cin>>u;
if(c1.isinlist(u)==true)
{
cout<<"Element found"<<endl;
}
else
cout<<"Element not found"<<endl;
}
else
cout<<"List is empty"<<endl;

}
else if(s==8)
{
c1.length();
}
else if(s==9)
{
if(c1.isempty()==1)
{
cout<<"list is empty"<<endl;
}
else
{
int f;
cout<<"Enter the info for deleting: ";
cin>>f;
c1.deletenode(f);
}
}
else
{
if(c1.isempty()==1)
{
cout<<"List is empty"<<endl;
}
else
c1.~clist();
}

}
while(t=='Y'||t=='y');
if(t!='Y' || t!='y')
{

cout<<"Thanks"<<endl;
}
return 0;
}
Output:

/*

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 1
Enter the element 3
After adding to the head
3 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 5
After adding to the head
3 -->5 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 8
After adding to the head
3 -->5 -->8 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 6
After adding to the head
3 -->5 -->8 -->6 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 5
After adding to the head
3 -->5 -->8 -->6 -->5 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 7
Enter thr element: 8
Element found

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 3
3
After deleting from the head
5 -->8 -->6 -->5 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 4
5
After deleting from the tail
8 -->6 -->5 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 5
list is not empty
List
8 -->6 -->5 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 8
The length of the given linked list is23

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 9
Enter the info for deleting: 3

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 6
8 -->6 -->
Enter y or Y for continue the program Y
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 10

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 6
list is empty

Enter y or Y for continue the program Y


~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 1
Enter the element 3
After adding to the head
3 -->
Enter y or Y for continue the program 2
~~~~~~~~~~
MENU
1.add to head
2.add to tail
3.delete from head
4.delete from tail
5.isempty
6.display
7.isinlist
8.length
9.delete node
10.destructor
~~~~~~~~~~
Enter your choice: 2
Enter the element 2
After adding to the head
3 -->2 -->Thanks

--------------------------------
Process exited after 170.6 seconds with return value 0
Press any key to continue . . .
*/

4. Implement Stack as an ADT and use it to evaluate a prefix/postfix expression.


Code:
#include<iostream>
#include<math.h>
using namespace std;
class stack1{
private:
int a[10];
int top;
int size;
public:
stack1(){
top=-1;
size=10;
}
stack1(int n){
size=n;
top=-1;
}
void push(int);
int pop();
void display();
int topel();
int calculate_Postfix(string);
};

void stack1::display(){
for(int i=top;i>=0;i--){
cout<<a[i]<<" ";
}
}

void stack1::push(int x){


if(top>=size-1) {
cout << "Stack OverFlow" << endl;
}
else{
top++;
a[top] = x;
}

int stack1::pop(){

int x=a[top];
top--;
return x;
}

int stack1::topel(){
int x=a[top];
return x;
}

int calculate_Postfix(string post_exp){


stack1 s;
int a,b;
int len=post_exp.length();
for(int i=0;i<len;i++){
if(post_exp[i]>=48 && post_exp[i]<=57){
s.push(post_exp[i]-'0');
}
else{
a=s.topel();
s.pop();
b=s.topel();
s.pop();
}
switch (post_exp[i])
{
case '+': // addition
s.push(b + a);
break;
case '-': // subtraction
s.push(b - a);
break;
case '*': // multiplication
s.push(b * a);
break;
case '/': // division
s.push(b / a);
break;
}
}
return s.topel();
}

int main(){
string post_exp ;
cout<<"enter the postfix expression:";
getline(cin,post_exp);
int result=calculate_Postfix(post_exp);
cout<<endl;
cout<<"The answer after calculating the postfix expression is : ";
cout<<result;
return 0;
}

Output:

/*
enter the postfix expression:598+58*+9-*

The answer after calculating the postfix expression is : 240


--------------------------------
Process exited after 23.82 seconds with return value 0
Press any key to continue . . .
*/

5. Implement Queue as an ADT.


Code:
#include <iostream>
using namespace std;

int cqueue[5];
int front = -1, rear = -1, n=5;

void insertCQ(int val) {


if ((front == 0 && rear == n-1) || (front == rear+1)) {
cout<<"Queue Overflow \n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;

if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {

int ch, val;


cout<<"1)Enqueue\n";
cout<<"2)Dequeue\n";
cout<<"3)isfull\n";
cout<<"4)isempty\n";
cout<<"5)Display\n";
cout<<"6)Exit\n";
do {
cout<<"Enter choice : "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Input for insertion: "<<endl;
cin>>val;
insertCQ(val);
break;

case 2:
deleteCQ();
break;

case 3:
displayCQ();
break;

case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}

/*
1)Enqueue
2)Dequeue
3)isfull
4)isempty
5)Display
6)Exit
Enter choice :
1
Input for insertion:
3
Enter choice :
1
Input for insertion:
5
Enter choice :
1
Input for insertion:
6
Enter choice :
1
Input for insertion:
8
Enter choice :
1
Input for insertion:
7
Enter choice :
1
Input for insertion:
8
Queue Overflow
Enter choice :
2
Element deleted from queue is : 3
Enter choice :
2
Element deleted from queue is : 5
Enter choice :
3
Queue elements are :
687
Enter choice :
4
Exit

--------------------------------
Process exited after 44.94 seconds with return value 0
Press any key to continue . . .
*/

6. Write a program to implement Binary Search Tree as an ADT which supports the following
operations: i. Insert an element x ii. Delete an element x iii. Search for an element x in the
BST iv. Display the elements of the BST in preorder, inorder, and postorder traversal .
Code:
#include <iostream>
#include <algorithm>
using namespace std;

class bstnode {
public:
int info;
bstnode *left;
bstnode *right;

bstnode() {
info = 0;
left = right = 0;
}

bstnode(int x, bstnode *l = 0, bstnode *r = 0) {


info = x;
left = l;
right = r;
}
};

class bst {
bstnode *root;

public:
bst();
void insert(int);
void preorder();
void preorder(bstnode*);
void inorder();
void inorder(bstnode*);
void postorder();
void postorder(bstnode*);
void deletenode(bstnode*&, int);
void deleteNode(int);
int height();
int height(bstnode*);
bool search(int);
};

bst::bst() {
root = 0;
}

void bst::insert(int key) {


if (root == 0) {
root = new bstnode(key);
return;
}

bstnode* current = root;


bstnode* parent = 0;

while (current != 0) {
parent = current;
if (key < current->info)
current = current->left;
else
current = current->right;
}

if (key < parent->info)


parent->left = new bstnode(key);
else
parent->right = new bstnode(key);
}

bool bst::search(int key) {


bstnode* temp = root;

while (temp != 0) {
if (key == temp->info)
return true;
else if (key < temp->info)
temp = temp->left;
else
temp = temp->right;
}

return false;
}

void bst::inorder() {
inorder(root);
cout << endl;
}

void bst::inorder(bstnode* node) {


if (node != 0) {
inorder(node->left);
cout << node->info << " ";
inorder(node->right);
}
}

void bst::preorder() {
preorder(root);
cout << endl;
}

void bst::preorder(bstnode* node) {


if (node != 0) {
cout << node->info << " ";
preorder(node->left);
preorder(node->right);
}
}

void bst::postorder() {
postorder(root);
cout << endl;
}

void bst::postorder(bstnode* node) {


if (node != 0) {
postorder(node->left);
postorder(node->right);
cout << node->info << " ";
}
}

void bst::deletenode(bstnode*& node, int key) {


if (node == 0)
return;

if (key < node->info)


deletenode(node->left, key);
else if (key > node->info)
deletenode(node->right, key);
else {
if (node->left == 0) {
bstnode* temp = node->right;
delete node;
node = temp;
} else if (node->right == 0) {
bstnode* temp = node->left;
delete node;
node = temp;
} else {
bstnode* temp = node->right;
while (temp->left != 0)
temp = temp->left;
node->info = temp->info;
deletenode(node->right, temp->info);
}
}
}

void bst::deleteNode(int key) {


deletenode(root, key);
}

int bst::height() {
return height(root);
}

int bst::height(bstnode* node) {


if (node == 0)
return -1;
else {
int leftHeight = height(node->left);
int rightHeight = height(node->right);
return 1 + max(leftHeight, rightHeight);
}
}

int main() {
bst tree;
int choice, key;

do {
cout << "\nBinary Search Tree Operations" << endl;
cout << "1. Insert a node" << endl;
cout << "2. Search for a key" << endl;
cout << "3. Inorder traversal" << endl;
cout << "4. Preorder traversal" << endl;
cout << "5. Postorder traversal" << endl;
cout << "6. Delete a node" << endl;
cout << "7. Calculate the height of the tree" << endl;
cout << "8. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter key to insert: ";
cin >> key;
tree.insert(key);
break;
case 2:
cout << "Enter key to search: ";
cin >> key;
if (tree.search(key)) {
cout << "Key " << key << " is present in the BST." << endl;
} else {
cout << "Key " << key << " is not present in the BST." << endl;
}
break;
case 3:
cout << "Inorder traversal: ";
tree.inorder();
break;
case 4:
cout << "Preorder traversal: ";
tree.preorder();
break;
case 5:
cout << "Postorder traversal: ";
tree.postorder();
break;
case 6:
cout << "Enter key to delete: ";
cin >> key;
tree.deleteNode(key);
break;
case 7:
cout << "Height of the BST: " << tree.height() << endl;
break;
case 8:
cout << "Exiting the program..." << endl;
break;
default:
cout << "Invalid choice! Please enter a valid option." << endl;
break;
}
} while (choice != 8);

return 0;
}

Output:

/*

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 1
Enter key to insert: 2

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 1
Enter key to insert: 4

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 1
Enter key to insert: 6

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 1
Enter key to insert: 4

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 2
Enter key to search: 5
Key 5 is not present in the BST.

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 3
Inorder traversal: 2 4 4 6

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 4
Preorder traversal: 2 4 6 4

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 5
Postorder traversal: 4 6 4 2

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 6
Enter key to delete: 4

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 7
Height of the BST: 2

Binary Search Tree Operations


1. Insert a node
2. Search for a key
3. Inorder traversal
4. Preorder traversal
5. Postorder traversal
6. Delete a node
7. Calculate the height of the tree
8. Exit
Enter your choice: 8
Exiting the program...

--------------------------------
Process exited after 83.4 seconds with return value 0
Press any key to continue . . .
*/

7. Write a program to implement insert and search operation in AVL trees.


Code:
#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 << "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;
}

You might also like