Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 66

DATA STRUCTURES

USING C++

Write a program for stack operation using arrays.

#include<iostream.h>
#include<conio.h>
int top=0;
int i;
class abc
{
int stack[5],item;
public:
void push();
void pop();
void dis();
};
void abc::push()
{
if(top==5)
{
cout<<"Stack is overflow";
}
else
{
cout<<"Enter item\n";
cin>>item;
top++;
stack[top]=item;
dis();
}
}
void abc::pop()
{
if(top==0)
cout<<"stack is underflow";
else
{
item=stack[top];
cout<<"deleted elemented is "<<item;
top--;
dis();
}
}
DATA STRUCTURES
USING C++

void abc::dis()
{
cout<<"elements in the stack are...";
for(i=0;i<=top;i++)
cout<<stack[i];
}
void main()
{
abc a;
clrscr();
int ch;
do
{
cout<<"\n 1.push()";
cout<<"\n 2.pop()";
cout<<"\n 3.exit()";
cout<<"Enter choice";
cin>>ch;
switch(ch)
{
case 1:
a.push();
break;
case 2:

a.pop();
}
}while(ch!=3);
}
DATA STRUCTURES
USING C++

OUTPUT:

1.push()
2.pop()
3.exit()
Enter choice 1
Enter item 1
Elements in the stack are…1
Enter choice 1
Enter item 2
Element in the stack are …1 2
Enter choice 3
DATA STRUCTURES
USING C++

Write a C++ program for queue operation using arrays.

#include<iostream.h>
#include<conio.h>
int front,rear,size=4;
int i;
class abc
{
int queue[5],item;
public:
abc()
{
Front=rear=-1;
void insert();
void del();
void dis();
};
void abc::insert()
{
if(rear>=size)
{
cout<<"Queue is overflow";
}
else
{
rear=rear+1;
cout<<"Enter item\n";
cin>>item;
queue[rear]=item;
dis();
}
}
void abc::del()
{
if(front==rear)
cout<<"Queue is underflow";
else
{
front=front+1;
item=queue[front];
cout<<"deleted elemented is "<<item;
DATA STRUCTURES
USING C++

}
}

void abc::dis()
{
cout<<"elements in the queue are..."<<endl;
for(i=front;i<=rear;i++)
cout<<queue[i];
}
void main()
{
abc a;
clrscr();
int ch;
do
{
cout<<"\n 1.insert()";
cout<<"\n 2.del()";
cout<<"\n 3.exit()";
cout<<"Enter choice";
cin>>ch;
switch(ch)
{
case 1:
a.insert();
break;
case 2:
a.del();
}
}while(ch!=3);
}
DATA STRUCTURES
USING C++

Output:

1.insert()
2.del()
3.exit()
Enter choice 1
Enter item 1
Elements in the Queue are…
1
Enter choice 2
Enter item 2
Elements in the Queue are…
12
Enter choice 3
DATA STRUCTURES
USING C++

Write a C++ program for Stack operation using Linked list.

#include<iostream.h>
#include<conio.h>
class node
{
public:
char d;
class node *ptr;
};
node *top,*p,*start,*bottom;
char ch;
class abc
{
public:
void create();
void dis();
void push();
void pop();
};
void abc::create()
{
start=new node;
cout<<”Enter data”;
cin>>start->d;
start->ptr=NULL;
bottom=start;
p=new node;
cin>>p->d;
p->ptr=start;
start=p;
cin>>ch;
while(ch!=’*’)
{
p=new node;
p->d=ch;
p->ptr=start;
start=p;
DATA STRUCTURES
USING C++

cin>>ch;
}
top=start;
dis();
}

void abc::dis()
{
p=top;
cout<<”top”;
while(p!=NULL)
{
cout<<”->”<<p->d;
p=p->ptr;
}
cout<<”->bottom”;
}
void abc::push()
{
p=new node;
cout<<”Enter data”;
cin>>p->d;
p-ptr=top;
top=p;
dis();
}
void abc::pop()
{
p=top;
top=top->ptr;
delete(p);
dis();
}
void main()
{
abc a;
clrscr();
int ch;
do
{
cout<<”\n 1.create()”;
cout<<”\n 2.push()”;
cout<<”\n 3.pop()”;
DATA STRUCTURES
USING C++

cout<<”\n 4.exit()”;
cout<<”Enter choice”;
cin>>ch;

switch(ch)
{
case 1:
a.create();
break;
case 2:
a.push();
break;
case 3:
a.pop();
}
}while(ch!=4);
}
DATA STRUCTURES
USING C++

Output:

1.create()
2.push()
3.pop()
4.exit()
Enter choice 1
Enter data
a
s
a
n
a
m
top->m->a->n->a->s->a->bottom
Enter choice 2
Enter data p
top->p->m->a->n->a->s->a->bottom
DATA STRUCTURES
USING C++

Write a C++ program for Queue operation using Linked list.

# include <iostream.h>
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
int ch;
struct node
{
int data;
node *link;
};

class queue
{
node *front, * rear;
public:
queue()
{
front=rear= NULL;
}
void insert(int);
void delet();
void disp();
};

void queue :: insert(int e)


{
node *p;
p= new node();
DATA STRUCTURES
USING C++

p->data=e;
p->link=NULL;
if(rear==NULL)
front=rear=p;
else
rear->link = p;
rear=p;
}

void queue :: delet()


{
node *p;
if (front==NULL)
{
cout<<"Underflow";
return;
}
p=front;
front=front->link;
if(front==NULL)
rear=NULL;
cout<<p->data;
delete p;
return;
}
void queue :: disp()
{
node *p;
if (front==NULL)
{
cout<<"the list is empty";
return;
}
p=front;
while (p!=NULL)
{
cout<<p->data<<"\n";
p=p->link;
}
}
void main()
DATA STRUCTURES
USING C++

{
int ch;
int ele;
clrscr();
queue s;
cout<<"1-INSERT\n 2- DELETE\n 3-DISPLAY \n 4- EXIT";
do
{
cout<<"\n Enter the choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n enter an element";
cin>>ele;
s.insert(ele);
break;
case 2: s.delet();
break;
case 3: s.disp();
break;
case 4: exit(0);
}
}while(ch<4);
getch();
}

Output:

1 – INSERT
2 – DELETE
3 – DISPLAY
4 – EXIT
Enter the choice :1
Enter an element : 12
Enter the choice :1
Enter an element : 13
Enter the choice :3
12
13
Enter the choice :2
12
DATA STRUCTURES
USING C++

Enter the choice :3


13

write a C++ program for double linked list

#include<iostream.h>
#include<conio.h>
class node
{
public:
class node *rptr;
int d;
class node *lptr;
};
node *start,*p,*pre,*last;
int ch;
class abc
{
public:
void create();
void dis();
void ib();
void ie();
void db();
void de();
};
void abc::create()
{
start=new node;
cout<<”Enter data:”;
cin>>start->d;
start->lptr=NULL;
start->rptr=NULL;

p=new node;
DATA STRUCTURES
USING C++

cin>>p->d;
p->lptr=start;
p->rptr=NULL;
start->rptr==p;
cin>>ch;
while(ch!=999)
{
pre=p;
p=new node;
p->d=ch;
p->rptr=NULL;
pre->rptr=p;
pre->lptr=p;
cin>>ch;
}
last=p;
dis();
}
void abc::dis()
{
p=start;
cout<<”start”;
while(p!=NULL)
{
cout<<”<->”<<p->d;
p=p->rptr;
}
cout<<”<->last”;
}
void abc::ib()
{
p=new node;
cout<<”Enter data”;
cin>>p->d;
p->lptr=NULL;
p->rptr=start;
start->lptr=p;
start=p;
dis();
}

void abc::ie()
{
p=new node;
DATA STRUCTURES
USING C++

cout<<”Enter data”;
cin>>p->d;
p->rptr=NULL;
p->lptr=last;
last->rptr=p;
last=p;
dis();
}
void abc::db()
{
p=start;
start=start->ptr;
delete(p);
dis();
}
void abc::de()
{
if(start==NULL)
{
cout<<”empty”;
else
{
p=start;
while(p->rptr!=NULL)
{
pre=p;
p=p->rptr;
}
delete(p);
pre->rptr=NULL;
}
dis(); }
void main()
{
abc a;
clrscr();
int ch;
do
{
cout<<”\n 1.create()”;
cout<<”\n 2.ib()”;
cout<<”\n 3.ie()”;
cout<<”\n 4.db()”;
cout<<”\n 5.de()”;
DATA STRUCTURES
USING C++

cout<<”\n 6.exit()”;
cout<<”Enter choice”;
cin>>ch;
switch(ch)
{
case 1: a.create();
break;
case 2: a.ib();
break;
case 3: a.ie();
break;
case 4: a.db();
break;
case 5:
a.de();
}
}while(ch!=6);
}

Output:

1.create()
2.ib()
3.ie()
4.db()
5.de()
6.exit()
Enter choice 1
1 2 3 999
Start<->1<->2<->3<->last
DATA STRUCTURES
USING C++

Write a C++ program for Binary Search tree.

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

struct node
{
int no;
struct node *left;
struct node *right;
};

class bst
{
private:
node *p,*root,*prev;
public:
bst();
void insert(int);
int search(int);
void del(int);
};

bst :: bst()
{
prev=NULL;
DATA STRUCTURES
USING C++

p=NULL;
root=NULL;
}

int bst :: search(int k)


{
int found=0;
if(root==NULL)
{
cout<<"\n"<<"The tree has no elements"<<endl;
return(found);
}
else
{
p=root;

while(p!=NULL)
{
if(p->no==k)
{
found=1;
break;
}
else if(k<p->no)
{
if(p->left!=NULL)
{
prev=p;
p=p->left;
}
else
break;
}
else
{
if(p->right!=NULL)
{
prev=p;
p=p->right;
}
else
break;
}
DATA STRUCTURES
USING C++

}
}
return(found);
}

void bst :: del(int x)


{
int h;
node *temp;
h=search(x);
if(h!=1)
cout<<endl<<"There is no such element to be deleted";
else
{
cout<<endl<<"The element deleted from the tree is: "<<p->no<<endl;
if(p->left==NULL && p->right==NULL)
{

if(p==root)
delete root;
else
{
if(prev->left==p)
prev->left=NULL;
else
prev->right=NULL;
delete p;
}
}
else if(p->left!=NULL && p->right==NULL)
{
prev->left=p->left;
delete p;
}
else if(p->left==NULL && p->right!=NULL)
{
prev->right=p->right;
delete p;
}
else
{
temp=p->left;
if((temp->left==NULL) && (temp->right==NULL))
{
DATA STRUCTURES
USING C++

if(prev->left==p)
{
prev->left=temp;
temp->left=NULL;
temp->right=p->right;
}
else
{
prev->right=temp;
temp->left=NULL;
temp->right=p->right;
}
delete p;
}
else
{
while(temp->right!=NULL)
{
if(temp->no<temp->right->no)
temp=temp->right;
}
if(prev->left==p)
{
prev->left=temp;
temp->left=p->left;
temp->right=p->right;
}
else
{
prev->right=temp;
temp->left=p->left;
temp->right=p->right;
}
delete p;
}}
}}

void bst :: insert(int n)


{
if(root==NULL)
{
p=new node;
p->no=n;
p->left=NULL;
DATA STRUCTURES
USING C++

p->right=NULL;
root=p;
}
else
{
node *temp;
temp=new node;
temp->no=n;
temp->left=NULL;
temp->right=NULL;
search(n);
if(p->no==n)
{
cout<<endl<<"There is already an element in tree";
return;
}
else if(n<p->no)
p->left=temp;
else
p->right=temp;
}}

void main()
{
int ch;
clrscr();
bst t;
do
{
cout<<"\n Menu";
cout<<"\n 1.search";
cout<<"\n 2.insert";
cout<<"\n 3.delete";
cout<<"\n 4.exit";
cout<<"\n Enter your choice [1..4]: ";
cin>>ch;
switch(ch)
{
case 1:
int k,p;
cout<<"\n Enter the element to be searched: ";
cin>>k;
p=t.search(k);
if(p==1)
DATA STRUCTURES
USING C++

cout<<endl<<"\n The element is found"<<endl;


else
cout<<endl<<"\n The element not found"<<endl;
break;
case 2:
int l;
cout<<endl<<"\n Enter the element to be inserted: ";
cin>>l;
t.insert(l);
break;
case 3:
int x;
cout<<endl<<"\n Enter the element to be deleted: ";
cin>>x;
t.del(x);
break;
case 4:
exit(1);
break;
}
}while(ch!=4);
getch();
}

OUTPUT

Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 2

Enter the element to be inserted: 10

Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 2

Enter the element to be inserted: 20


DATA STRUCTURES
USING C++

Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 2

Enter the element to be inserted: 30

Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 2

Enter the element to be inserted: 40

Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 1

Enter the element to be searched: 10

The element is found


Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 1

Enter the element to be searched: 20

The element is found

Menu
1.search
2.insert
3.delete
DATA STRUCTURES
USING C++

4.exit
Enter your choice [1..4]: 3

Enter the element to be deleted: 20

The element deleted from the tree is: 20

Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 1
Enter the element to be searched: 20

The element not found

Menu
1.search
2.insert
3.delete
4.exit
Enter your choice [1..4]: 4

PRE-ORDER TRAVERSAL USING NON RECURSIVE FUNCTION

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

class node
{
public:
class node *left;
class node *right;
int data;
};

class tree: public node


{
public:
int stk[50],top;
node *root;
tree()
DATA STRUCTURES
USING C++

{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;

node *search(node *temp,int ch)


{
if(root== NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right== NULL)
return temp;

if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);

} }
DATA STRUCTURES
USING C++

void display(node *temp)


{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data <<" ";
display(temp->right);
}
void preorder( node *root)
{
node *p,*q;
p=root;
q=NULL;
top=0;

while(p!=NULL)
{
cout <<p->data << " ";
if(p->right!=NULL)
{
stk[top]=p->right->data;
top++;
}
p=p->left;

if(p==NULL && top>0)


{
p=pop(root);
}}
}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
DATA STRUCTURES
USING C++

}
};
void main()
{
tree t1;
int ch,n,i;
while(1)
{
cout <<"\n 1.INSERT \n 2.DISPLAY \n 3.PREORDER TRAVERSE \n
4.EXIT";
cout<<"\n Enter your choice [1..4]: ";
cin >> ch;
switch(ch)
{
case 1: cout <<" Enter no of elements to insert:";
cout<<" \n Enter the elements: \t";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.preorder(t1.root); break;
case 4: exit(1);
}
}}

Output:

1.INSERT
2.DISPLAY
3.PREORDER TRAVERSE
4.EXIT
Enter your choice [1..4]: 1
Enter no of elements to insert:
Enter the elements: 7
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY
3.PREORDER TRAVERSE
DATA STRUCTURES
USING C++

4.EXIT

Enter your choice [1..4]: 2


2 5 11 21 24 36 44

1.INSERT
2.DISPLAY
3.PREORDER TRAVERSE
4.EXIT

Enter your choice [1..4]: 3


5 2 24 11 21 36 44

1.INSERT
2.DISPLAY
3.PREORDER TRAVERSE
4.EXIT
Enter your choice [1..4]: 4

IN-ORDER TRAVERSAL USING NON RECURSIVE FUNCTION

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

class node
{
public:
class node *left;
class node *right;
int data;
};

class tree: public node


{
DATA STRUCTURES
USING C++

public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;

node *search(node *temp,int ch)


{
if(root== NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right== NULL)
return temp;

if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
DATA STRUCTURES
USING C++

{ if(temp->right==NULL) return temp;


search(temp->right,ch);

} }

void display(node *temp)


{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data;
display(temp->right);
}
void inorder( node *root)
{
node *p;
p=root;
top=0;
do
{
while(p!=NULL)
{
stk[top]=p->data;
top++;
p=p->left;
}
if(top>0)
{
p=pop(root);
cout << p->data;
p=p->right;
}
}while(top!=0 || p!=NULL);
}

node * pop(node *p)


{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
DATA STRUCTURES
USING C++

}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};

void main()
{
tree t1;
int ch,n,i;
while(1)
{
cout<<"\n 1.INSERT \n 2.DISPLAY \n 3.INORDER
TRAVERSE \n 4.EXIT";
cout<<"\n Enter your choice [1..4]: ";
cin >> ch;
switch(ch)
{
case 1: cout <<"\n Enter no of elements to insert: ";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.inorder(t1.root); break;
case 4: exit(1);
}
}
}

OUTPUT

1.INSERT
2.DISPLAY
3.INORDER TRAVERSE
4.EXIT
Enter your choice [1..4]: 1
Enter no of elements to insert: 7
5 24 36 11 44 2 21
1.INSERT
DATA STRUCTURES
USING C++

2.DISPLAY
3.INORDER TRAVERSE
4.EXIT

Enter your choice [1..4]: 2


2 5 11 21 24 36 44
1.INSERT
2.DISPLAY
3.INORDER TRAVERSE
4.EXIT

Enter your choice [1..4]: 3


2 5 11 21 24 36 44
1.INSERT
2.DISPLAY
3.INORDER TRAVERSE
4.EXIT

Enter your choice [1..4]: 4

POST-ORDER TRAVERSAL USING NON RECURSIVE FUNCTION

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

class node
{
public:
class node *left;
class node *right;
int data;
DATA STRUCTURES
USING C++

};

class tree: public node


{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;

node *search(node *temp,int ch)


{
if(root== NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right== NULL)
return temp;
DATA STRUCTURES
USING C++

if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);

} }
void display(node *temp)
{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data << " ";
display(temp->right);
}
void postorder( node *root)
{
node *p;
p=root;
top=0;

while(1)
{
while(p!=NULL)
{
stk[top]=p->data;
top++;
if(p->right!=NULL)
stk[top++]=-p->right->data;
p=p->left;
}
while(stk[top-1] > 0 || top==0)
{
if(top==0) return;
cout << stk[top-1] <<" ";
p=pop(root);
}
if(stk[top-1]<0)
{
stk[top-1]=-stk[top-1];
p=pop(root);
} }
}
DATA STRUCTURES
USING C++

node * pop(node *p)


{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}};
void main()
{
tree t1;
int ch,n,i;
clrscr();
while(1)
{
cout<<"\n 1.INSERT \n 2.DISPLAY \n 3.POSTORDER TRAVERSE \n
4.EXIT \n";
cout<<"\n Enter your choice [1..4]: ";
cin >> ch;
switch(ch)
{
case 1: cout <<"Enter no of elements to insert:";
cout<<"\n Enter the elements: ";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.postorder(t1.root); break;
case 4: exit(1);
}
}}
OUTPUT

1.INSERT
2.DISPLAY
DATA STRUCTURES
USING C++

3.POSTORDER TRAVERSE
4.EXIT

Enter your choice [1..4]: 1

Enter no of elements to insert:


Enter the elements: 7

5 24 36 11 44 2 21

1.INSERT
2.DISPLAY
3.POSTORDER TRAVERSE
4.EXIT

Enter your choice [1..4]: 2


2 5 11 21 24 36 44

1.INSERT
2.DISPLAY
3.POSTORDER TRAVERSE
4.EXIT

Enter your choice [1..4]: 3


2 21 11 44 36 24 5

1.INSERT
2.DISPLAY
3.POSTORDER TRAVERSE
4.EXIT

Enter your choice [1..4]: 4

Write a C++ program for BFS

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];

void main()
{
DATA STRUCTURES
USING C++

clrscr();
int m;
cout <<"\n Enter no of Vertices: ";
cin >> n;
cout <<"\n Enter no of Edges: ";
cin >> m;
cout <<"\n EDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}

cout <<"\n Enter Initial Vertex: ";


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

Output:

Enter no of Vertices: 9
Enter no of Edges: 9
EDGES
12
23
15
DATA STRUCTURES
USING C++

47
78
89
26
14
57
Enter Initial Vertex: 1
Visitied vertices
12 4 5 3 6 7 8 9

Write a C++ program for DFS

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
DATA STRUCTURES
USING C++

void main()
{
int m;
clrscr();
cout <<"\n Enter no of Vertices: ";
cin >> n;
cout <<"\n Enter no of Edges: ";
cin >> m;
cout <<"\n EDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}

cout <<"\n Enter Initial Vertex: ";


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

Output:

Enter no of Vertices: 9
Enter no of Edges: 9
EDGES
12
DATA STRUCTURES
USING C++

23
15
14
47
78
89
26
57
Enter Initial Vertex: 1
ORDER OF VISITED VERTICES
123647895

Write a C++ program for Merge Sort

#include<iostream.h>
#include<conio.h>
DATA STRUCTURES
USING C++

void mergesort(int *,int,int);


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

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

void main()
{
clrscr();
cout <<"\n Enter no of Elements: ";
cin >> n;
cout <<"\n Enter the Elements: ";
for(i=0;i<n;i++)
cin >> a[i];
mergesort(a,0,n-1);
cout <<"\n Numbers after Sorting are... \n";
for(i=0;i<n;i++)
cout << a[i] << " ";
getch();
}

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


{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,j);
}
}
void merge(int a[],int low,int mid ,int high)
{
int h,i,j,k;
h=low;
i=low;
j=mid+1;

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


{
if(a[h]<=a[j])
b[i]=a[h++];
else
DATA STRUCTURES
USING C++

b[i]=a[j++];
i++;
}
if( h > mid)
for(k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=h;k<=mid;k++)
b[i++]=a[k];
cout <<"\n";
for(k=low;k<=high;k++)
{
a[k]=b[k];
cout << a[k] <<" ";
}

Output:
DATA STRUCTURES
USING C++

Enter no of Elements: 8
Enter the Elements: 12 5 61 60 50 1 70 81
5 12
60 61
5 12 60 61
1 50
70 81
1 50 70 81
1 5 12 50 60 61 70 81
Numbers after Sorting are...
1 5 12 50 60 61 70 81

Write a C++ program for HeapSort


DATA STRUCTURES
USING C++

#include <iostream.h>
#include <conio.h>
int heapSize;
//int heapsize=0;

void print(int a[],int nn)


{
for (int i = 0; i < nn; i++) {
cout << a[i] << "-";
}
cout << endl;
}

int parent(int i)
{
if(i==1)
return 0;
if(i%2==0)
return ( (i / 2)-1);
else
return ( (i / 2));
}

int left(int i)
{
return (2 * i) + 1;
}

int right(int i)
{
return (2 * i) + 2;
}

void heapify(int a[], int i, int nn)


{
int l = left(i), great;
int r = right(i);
if ( (a[l] > a[i]) && (l < nn))
{
great = l;
}
DATA STRUCTURES
USING C++

else
{
great = i;
}
if ( (a[r] > a[great]) && (r < nn))
{
great = r;
}
if (great != i)
{
int temp = a[i];
a[i] = a[great];
a[great] = temp;
heapify(a, great, nn);
}
}

void BuildMaxHeap(int a[],int nn)


{
for (int i = (nn - 1) / 2; i >= 0; i--)
{
heapify(a, i, nn);
print(a, nn);
}
}

void HeapSort(int a[],int nn)


{
BuildMaxHeap(a,nn);
for (int i = nn; i > 0; i--)
{
int temp = a[0];
a[0] = a[nn - 1];
a[nn - 1] = temp;
nn = nn - 1;
heapify(a, 0, nn);
}

}
DATA STRUCTURES
USING C++

void main()
{

clrscr();
int n=0;
int arr[10];
cout<<"\n Enter size of input list\n";
cin>>n;
heapSize=n;
for(int i=0;i<n;i++)
{
cout<<"Enter"<<i+1<<" st/nd/rd/th element\n";
cin>>arr[i];
}
for(int j=n+1;i<=10;i++)
arr[j]=0;
cout<<"Heapsort...........\n";

HeapSort(arr,n);
print(arr,n);
getch();
}
DATA STRUCTURES
USING C++

Output:

Enter size of input list


5
Enter 1 st/nd/rd/th element
56
Enter 2 st/nd/rd/th element
34
Enter 3 st/nd/rd/th element
12
Enter 4 st/nd/rd/th element
98
Enter 5 st/nd/rd/th element
76

Heapsort...........
56-34-12-98-76-
56-98-12-34-76-
98-76-12-34-56-
12-34-56-76-98-
DATA STRUCTURES
USING C++

AVL Tree Program

#include <string.h>
#include <iostream.h>

#include "avl_tree.h"

// An "environment" of variables and (string) values.


class env
{
private:

struct node
{
// Child pointers.
node *gt, *lt;
char *name;
char *value;
};

// Abstractor class for avl_tree template.


struct abstr
{
typedef node *handle;

typedef const char *key;

typedef unsigned size;

static handle get_less(handle h, bool access) { return(h->lt); }


static void set_less(handle h, handle lh) { h->lt = lh; }
static handle get_greater(handle h, bool access) { return(h->gt); }
static void set_greater(handle h, handle gh) { h->gt = gh; }

static int get_balance_factor(handle h)


{ return((signed char) (h->name[0])); }
static void set_balance_factor(handle h, int bf) { h->name[0] = bf; }
static int compare_key_node(key k, handle h)
{ return(strcmp(k, (h->name) + 1)); }
static int compare_node_node(handle h1, handle h2)
{ return(strcmp((h1->name) + 1, (h2->name) + 1)); }
DATA STRUCTURES
USING C++

static handle null(void) { return(0); }

// Nodes are not stored on secondary storage, so this should


static bool read_error(void) { return(false); }
};

typedef abstract_container::avl_tree<abstr> tree_t;


tree_t tree;
public:

void set(const char *name, const char *value)


{
node *n = tree.search(name);

if (!n)
{
n = new node;
n->name = new char [strlen(name) + 2];
strcpy(n->name + 1, name);
tree.insert(n);
}
else
// Delete current value.
delete [] n->value;

if (value)
{
if (strlen(value) == 0)
value = 0;
}

if (value)
{
n->value = new char [strlen(value) + 1];
strcpy(n->value, value);
}
else
{
tree.remove(name);
delete [] n->name;
delete n;
}
}
DATA STRUCTURES
USING C++

const char *get(const char *name)


{
node *n = tree.search(name);

return(n ? n->value : "");


}

void dump(void)
{
tree_t::iter it;
node *n;

it.start_iter_least(tree);

for (n = *it; n; it++, n = *it)


cout<<n->name+1<<n->value;

// Clear environment.
void clear(void)
{
tree_t::iter it;
node *n;

it.start_iter_least(tree);

for (n = *it; n; n = *it)


{
it++;

// Release node n's resources.


delete [] n->name;
delete [] n->value;
delete n;
}

tree.purge();
}
};

int main(void)
{
env e;
DATA STRUCTURES
USING C++

e.set("The", "The value");


e.set("quick", "quick value");
e.set("brown", "brown value");
e.set("fox", "fox value");
e.set("jumped", "jumped value");
e.set("over", "over value");
e.set("the", "the value");
e.set("lazy", "lazy value");
e.set("dog", "dog value");

e.set("DOG", "DOG value");


e.set("DOG", 0);

cout<<”The Value of dog is \n \n “<<e.get(“dog”);


cout<<”DUMP”;
e.dump();

e.clear();
e.dump();

return(0);
}
DATA STRUCTURES
USING C++

Write a C++ program to implement all the functions of a dictionary (ADT) using
hashing.

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

struct EnrollRec {
long key;
int course[9];
int link;
};

#define HEADER (797)


#define MAXADDR (1199)

int fd;

int getcommand(void);
void docommand(int);
void newrec(void);
void show(void);
void add();

int hash(long key)


{
return key % 797;
}

void display(struct EnrollRec record)


{ int i;
printf("Key: %ld\nCourses:", record.key);
for(i = 0; i< 9; i++)
{
if(record.course[i] != 0)
printf(" %d ", record.course[i]);
}
DATA STRUCTURES
USING C++

printf("\n");
}

int readrecord(int recordnumber, struct EnrollRec *buffer)


{
lseek(fd,recordnumber*sizeof(struct EnrollRec), SEEK_SET);
return read(fd, buffer, sizeof(struct EnrollRec));
}

int writerecord(int recordnumber, struct EnrollRec * buffer)


{
lseek(fd,recordnumber*sizeof(struct EnrollRec), SEEK_SET);

return write(fd, buffer, sizeof(struct EnrollRec));

int getnode()
{
struct EnrollRec header, firstrecord;
int available;
readrecord(HEADER, &header);
if(header.link == 0)
return 0;
available = header.link;
readrecord(available, &firstrecord);
header.link = firstrecord.link;
header.course[8]--;
writerecord(HEADER, &header);
return available;
}

void returnnode(int whichnode)


{
struct EnrollRec freenode = { -1, {0,0,0,0,0,0,0,0,0}, 0 }; /* Clean. */
struct EnrollRec header;
readrecord(HEADER, &header);
freenode.link = header.link;
writerecord(whichnode, &freenode);
header.link = whichnode;
header.course[8]++;
DATA STRUCTURES
USING C++

writerecord(HEADER, &header);
}

void main(int argc, char * argv[])


{

if(argc < 2)
{
fprintf(stderr, "Filename argument required.\n");
exit(-1);
}
fd = open(argv[1], O_RDWR | O_BINARY);
if(fd < 0)
{
perror(argv[1]);
exit(-1);
}
while(1)
docommand(getcommand());
}

int getcommand()
{
int command;
while( command = getchar(), isspace(command) );
while(getchar() != '\n');
return command;
}

void docommand(int command)


{
switch (command)
{
case 'n':
newrec(); break;
case 's':
show(); break;
case 'a':
add(); break;
case 'x':
close(fd); exit(0); break;
default:
fprintf(stderr, "Unknown command %c\n", command);
DATA STRUCTURES
USING C++

long getkey()
{
long key;
is entered. */
printf("Enter the key: ");
while(scanf("%ld", &key) < 1 || key < 0 || key > 999999999)
{
/* Throw away invalid line. */
while(getchar() != '\n') ;
printf("Invalid key - try again: ");
}
return key;
}

int getcourse()
{
int coursenumber;
printf("Enter the course number: ");
while(scanf("%d", &coursenumber) < 1 || coursenumber < 1 || coursenumber > 9999)
{
while(getchar() != '\n') ;
printf("Invalid course number - try again: ");
}
return coursenumber;
}

int search(long key)


{
int address;
struct EnrollRec thisrecord;
address = hash(key);
while(1)
{
readrecord(address, &thisrecord);

if(thisrecord.key == key)
return address;
address = thisrecord.link;
if(address == 0)
DATA STRUCTURES
USING C++

return -1;
}
}

void newrec()
{
long key = getkey();
int found, home, address;
struct EnrollRec whatthe;
struct EnrollRec homerecord, foundrecord, newrecord = {0, {0,0,0,0,0,0,0,0,0}, 0};

found = search(key);
if(found >= 0)
{
readrecord(found, &foundrecord);
printf("Record found in file:\n");
display(foundrecord);
return;
}
newrecord.key = key;
home = hash(key);
readrecord(home, &homerecord);
/* If it is, put the new record there. */
if(homerecord.key == -1)
{
writerecord(home, &newrecord);
printf("Record written to address %d\n", home);
}
else
{
address = getnode();
if(address == 0)
{
printf("Out of free space in file.\n");
return;
}
newrecord.link = homerecord.link;
homerecord.link = address;

writerecord(address, &newrecord);
writerecord(home, &homerecord);
printf("Record written to address %d\n",address);
DATA STRUCTURES
USING C++

void show()
{
long key = getkey();
struct EnrollRec therecord;
int address = search(key);
if(address < 0)
printf("KEY NOT FOUND\n");

else
{
readrecord(address, &therecord);
display(therecord);
}
}

void add()
{
struct EnrollRec therecord;
int address, i, full, coursenumber;
long key = getkey();
address = search(key);
if(address == -1)
{
printf("KEY NOT FOUND: %ld\n", key);
return;
}
readrecord(address, &therecord);
full = 1;
for(i = 0; i < 9; i++)
{
if(therecord.course[i] == 0)
{
full = 0;
DATA STRUCTURES
USING C++

break;
}
}

if(full)
{
printf("CANNOT ADD ANY COURSES\n");
return;
}
coursenumber = getcourse();
for(i = 0; i < 9; i++)
{
if(therecord.course[i] == coursenumber)
{
printf("ALREADY ENROLLED IN THAT COURSE: %d\n", coursenumber);
return;
}
}

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


{
if(therecord.course[i] == 0)
{
therecord.course[i] = coursenumber;
break;
}
}
writerecord(address, &therecord);
printf("Altered record:\n");
display(therecord);
}
DATA STRUCTURES
USING C++

Write a C++ program fro implementing Knuth-Morris- Pratt pattern matching


algorithm.

#include "match.h"
#include "lineb.h"
#include <stream.h>

int csense = 'a' - 'A'; // assume case insensitive

inline char casify(char c)


{
if (c >= 'A' && c <= 'Z') return c + csense;
else return c;
}

inline int eolgetc(char & v, istream & i)


{
i.get(v);
return (!i.eof() && (v == '\n' || v == '\014'));
}

inline void reset(line_buffer & lb) { lb.set_buffer(0); }


void add(line_buffer & lb, char c) { lb.replace(lb.textlen(), &c, 1, 0); }

// Complain about inappropriate args

extern "C" void exit(int);


DATA STRUCTURES
USING C++

const char * progname = "lookup";

void usage()
{
cerr << "usage: " << progname << "[-cr] word < file\n";
exit (1);
}

int rest = 0; // nonzero means print whole file after match


int first_para = 1; // zero when a para has already been output

void
process(istream & in, string_match & target)
{
line_buffer para;
int was_eol = 1;
target.reset();

while (!in.eof()) { // looping over chars in file


char inchar;
if (eolgetc(inchar, in)) { // end of line?
target.reset(); // match does not extend over line
if (was_eol) reset(para); // two newlines, para break
else {
was_eol = 1; // one newline, remember the next
add(para,'\n'); // but assume internal to para for now
}
continue; // back to top of loop
}
if (in.eof()) return;
was_eol = 0;

if (!target.match(casify(inchar))) add(para, inchar);


else {

if (first_para) first_para = 0;
else cout.put('\n');

cout << para.text(); // first send text before match


cout.put(inchar); // then end of match itself
DATA STRUCTURES
USING C++

while (!in.eof()) { // then previously unread part


if (eolgetc (inchar, in) && !rest) {
cout.put('\n'); // maybe start of para break, copy
if (eolgetc (inchar, in)) break; // and test again
}
cout.put(inchar); // send char after nl or non-nl char
}
reset(para); // now at paragraph break
was_eol = 1; // flush any further newlines
} // end code for matched target
} // end loop over chars in line
}

// The main program.

main (int argc, char ** argv)


{

if (argc == 0) usage();
progname = argv[0];
while (argc > 2 && argv[1][0] == '-') {
argc--;
const char * cp = *++argv;
while (*++cp != '\0') switch (*cp) {
case 'c':
csense = 0;
break;

case 'r':
rest = 1;
break;

default:
usage();
}
}

if (argc != 2) usage();

for (char * cp = argv[1]; *cp != '\0'; cp++) *cp = casify(*cp);


DATA STRUCTURES
USING C++

string_match target(argv[1]);

// Process standard input.


process(cin, target);
}

Write a C++ program for implementing Boyer-Moore Patten matching algorithm

void preBmBc(char *x, int m, int bmBc[]) {


int i;

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


bmBc[i] = m;
for (i = 0; i < m - 1; ++i)
bmBc[x[i]] = m - i - 1;
}

void suffixes(char *x, int m, int *suff) {


int f, g, i;

suff[m - 1] = m;
g = m - 1;
for (i = m - 2; i >= 0; --i) {
if (i > g && suff[i + m - 1 - f] < i - g)
suff[i] = suff[i + m - 1 - f];
else {
if (i < g)
g = i;
f = i;
while (g >= 0 && x[g] == x[g + m - 1 - f])
--g;
suff[i] = f - g;
}
}
}
DATA STRUCTURES
USING C++

void preBmGs(char *x, int m, int bmGs[]) {


int i, j, suff[XSIZE];

suffixes(x, m, suff);

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


bmGs[i] = m;
j = 0;
for (i = m - 1; i >= 0; --i)
if (suff[i] == i + 1)
for (; j < m - 1 - i; ++j)
if (bmGs[j] == m)
bmGs[j] = m - 1 - i;
for (i = 0; i <= m - 2; ++i)
bmGs[m - 1 - suff[i]] = m - 1 - i;
}

void BM(char *x, int m, char *y, int n) {


int i, j, bmGs[XSIZE], bmBc[ASIZE];

/* Preprocessing */
preBmGs(x, m, bmGs);
preBmBc(x, m, bmBc);

/* Searching */
j = 0;
while (j <= n - m) {
for (i = m - 1; i >= 0 && x[i] == y[i + j]; --i);
if (i < 0) {
OUTPUT(j);
j += bmGs[0];
}
else
j += MAX(bmGs[i], bmBc[y[i + j]] - m + 1 + i);
}
}
DATA STRUCTURES
USING C++

INDEX

S.No Contents Pg.No

01 Stack operation using arrays.

02 Queue operation using arrays


03 Stack operation using Linked list
04 Queue operation using Linked list
05 Double linked list
06 Binary Search tree.
07 Pre-Order Traversal

08 In-Order Traversal

09 Post-Order Traversal

10 Breadth First Search

11 Depth First Search


DATA STRUCTURES
USING C++

12 Merge Sort

13 Heap Sort
14 AVL Tree
15 To implement all the functions of a dictionary (ADT)
using hashing
16 Knuth-Morris- Pratt pattern matching algorithm
17 Boyer-Moore Patten matching algorithm

You might also like