You are on page 1of 99

Week1

Write a C++ programs to implement the following using an array.


a) Stack ADT b) Queue ADT
/*PROGRAM TO IMPLEMENT STACKS ADT USING ARRAYS */
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
template<class t>
class list
{
public:
int size;
t *a;
virtual void push()=0;
virtual void pop()=0;
virtual void display()=0;
};
template<class t>
class stack : public list <t>
{
public:
int top;
int is_full();
int is_empty();
stack(int n);
void push()

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


{
t ele;
if(is_full())
{
cout<<"stack is full";
}
else
{
cout<<"\n enter the ele to insert";
cin>>ele;
top++;
a[top]=ele;
}
}
void pop()
{
if(is_empty())
{
cout<<"stack is empty";
}
else
{
cout<<"\n the poped ele is:"<<a[top];
top--;
}
}
void display()
{
2

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


int i;
if(is_empty())
{
cout<<"\nstack is empty";
}
else
{
cout<<"\n the ele's in array\n";
for(i=0;i<=top;i++)
{
cout<<a[i]<<"\t";
}

}
}
};
template<class t>
stack<t>::stack(int n)
{
size=n;
a= new t[size];
top=-1;
}
template<class t>
int stack<t>::is_full()
{
if(top==size-1)
return(1);
3

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


else
return 0;
}
template<class t>
int stack<t>:: is_empty()
{
if(top==-1)
return 1;
else
return 0;
}
void main()
{
clrscr();
int p;
cout<<"enter the size";
cin>>p;
stack <int> s(p);
int ch;
while(1)
{
cout<<"\n enter ur choice 1.push 2.pop 3.display 4.exit\n";
cin>>ch;
switch(ch)
{
case 1:s.push();
break;
case 2:s.pop();
4

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


break;
case 3:s.display();
break;
case 4:exit(1);
}
}
}
OUTPUT

/* PROGRAM TO IMPLEMENT QUEUE ADT USING ARRAYS*/


#include<iostream.h>
#include<conio.h>
#include<process.h>
template<class t>
class list
{
public:
int size;
t *a;
virtual void insert()=0;
virtual void del()=0;
virtual void display()=0;
};
template<class t>
5

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


class queue:public list <t>
{
public:
int is_full();
int is_empty();
int f,r;
t ele;
queue(int n)
{
size=n;
a=new t[size];
f=r=-1;
}
void insert()
{
if(is_full())
{
cout<<"\nqueue is full";
}
else
{
cout<<"enter the ele to insert";
cin>>ele;
if(f==-1)
f=0;
r++;
a[r]=ele;
}
6

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


}
void del()
{
if(is_empty())
{
cout<<"\nqueue is empty";
}
else
{
if(f==0&&r==0)
f=r=-1;
cout<<"the dleted ele is:"<<a[f];
f++;
}
}
void display()
{
int i;
if(is_empty())
{
cout<<"\nthere are no ele's to diplay";
}
else
{
cout<<"the ele's in arr:";
for(i=f;i<=r;i++)
{
cout<<a[i]<<"\t";
7

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


}
}
}
};
template<class t>
int queue<t>::is_full()
{
if(r==size-1)
return 1;
else
return 0;
}
template<class t>
int queue<t>::is_empty()
{
if(r==f-1||f==-1)
return 1;
else
return 0;
}
void main()
{
int ch,n;
cout<<"\nenter the size of arr";
cin>>n;
queue <int> q(n);
while(1)
{
8

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


cout<<"\nenter the choice 1.insert 2.delete 3.display 4.exit";
cin>>ch;
switch(ch)
{
case 1:q.insert();
break;
case 2:q.del();
break;
case 3:q.display();
break;
case 4:exit(1);
}
}
}
OUTPUT

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week 2
Write C++ programs to implement the following using a singly linked list.
a) Stack ADT b) Queue ADT
/* PROGRAM TO IMPLEMENT STACK ADT USING LINKED LIST*/
#include<iostream.h>
#include<process.h>
#include<conio.h>
template<class t>
class list
{
public:
struct node
{
t data;
node *next;
};
virtual void push()=0;
virtual void pop()=0;
virtual void display()=0;
};
template <class t>
class stack: public list<t>
{
public:
struct node *top;
stack()
{
top=NULL;
10

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


}
t ele;
int is_empty();
void push()
{
struct node *temp;
temp=new node;
cout<<"\nenter the ele to insert";
cin>>ele;
temp->data=ele;
temp->next=NULL;
if(top==NULL)
top=temp;
else
{
temp->next=top;
top=temp;
}
}
void pop()
{
t e;
if(is_empty())
{
cout<<"\nlist is empty";
}
else
{
11

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


e=top->data;
top=top->next;
cout<<"the poped ele is "<<e;
}
}
void display()
{
struct node *temp;
temp=new node;
if(is_empty())
{
cout<<"\n there r no lee's todisplay in list";
}
else
{
temp=top;
cout<<"\nthe ele's in list:";
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
}
};
template <class t>
int stack<t>::is_empty()
{
12

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


if(top==NULL)
return 1;
else
return 0;
}
void main()
{
int ch;
clrscr();
stack <int>s;
while(1)
{
cout<<"\nenter the choice 1.push 2.pop 3.display 4.exit";
cin>>ch;
switch(ch)
{
case 1: s.push();
break;
case 2:s.pop();
break;
case 3:s.display();
break;
case 4:exit(1);
}
}
}

13

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


OUTPUT

/PROGRAM TO IMPLEMENT QUEUE ADT USING LINKED LIST*/


#include<iostream.h>
#include<conio.h>
#include<process.h>
template <class t>
class list
{
public:
struct node
{
t data;
node *next;
};
virtual void insert()=0;
virtual void del()=0;
virtual void display()=0;
};
template<class t>
class queue:public list<t>
{
public:
struct node *front;

14

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


struct node *rear;
queue()
{
front=NULL;
rear=NULL;
}
t ele;
int is_empty();
void insert()
{
struct node *temp;
temp=new node;
cout<<"\nenter the ele to insert";
cin>>ele;
temp->data=ele;
temp->next=NULL;
if(front==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=rear->next;
}
}
void del()
{
t e;
if(is_empty())
15

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


{
cout<<"\nthe list is empty";
}
else
{

e=front->data;
if(front==rear)
front=rear=NULL;
front=front->next;
cout<<"\n the poped ele is:"<<e;
}
}
void display()
{
struct node *temp;
temp=new node;
if(is_empty())
{
cout<<"\nthere r no ele's in list" ;
}
else
{
temp=front;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
16

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


}
}
}
};
template<class t>
int queue<t>::is_empty()
{
if(front==NULL)
return 1;
else
return 0;
}
void main()
{
int ch;
clrscr();
queue <int> s;
while(1)
{
cout<<"\n enter ur choice 1.insert 2.delete 3.display 4.exit:" ;
cin>>ch;
switch(ch)
{
case 1:s.insert();
break;
case 2:s.del();
break;
case 3:s.display();
17

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


break;
case 4:exit(1);
}
}
}

OUTPUT

18

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week3
Write C++ programs to implement the deque (double ended queue) ADT using an array.
#include<iostream.h>
#include<conio.h>
template<class t>
class dequeue
{
int front1,rear1,front2,rear2,size;
t *a;
public:
dequeue();
void insertf();
void insertr();
void delf();
void delr();
void display();
};
template<class t>
dequeue<t>::dequeue()
{
front1=rear1=0;
cout<<"\n enter the size:";
cin>>size;
a=new t[size];
front2=rear2=size-1;
19

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

for(int i=0;i<size;i++)
a[i]=NULL;
}
template<class t>
void dequeue<t>::insertf()
{
if(rear1==size||rear1>rear2)
cout<<"\n queue is full";
else
{
cout<<"\n enter the no:";
cin>>a[rear1];
rear1++;
}
}
template<class t>
void dequeue<t>::insertr()
{
if(rear2<rear1||rear2<0)
cout<<"\n queue is full";
else
{
cout<<"\n enter the no:";
cin>>a[rear2];
rear2--;
}
20

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
template<class t>
void dequeue<t>::delf()
{
if(front1==size)
cout<<"\n queue is empty";
else
{
int no;
cout<<a[front1];
a[front1]=NULL;
front1++;
}
}
template<class t>
void dequeue<t>::delr()
{
if(front2<0)
cout<<"\n queue is empty";
else
{
cout<<a[front2];
a[front2]=NULL;
front2--;
}
21

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
template<class t>
void dequeue<t>::display()
{
if(size==0)
cout<<"\n queue is empty";
else
{
for(int i=0;i<size;i++)
{
cout<<a[i]<<" ";
}
}
}
void main()
{
clrscr();
dequeue<int> a;
int choice;
do
{
cout<<"\n 0-exit,1-insert front,2-insert rear,3-del front,4-del rear,5-display";
cin>>choice;
switch(choice)
{
case 1:a.insertf();
break;
22

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

case 2: a.insertr();
break;
case 3: a.delf();
break;
case 4: a.delr();
break;
case 5: a.display();
break;
}
}while(choice!=0);
}
Output

/*Deque ADT using double linked list*/


#include<iostream.h>
#include<conio.h>
template<class t>
class dequeue
{
Private: struct node
{
t data;
node *prev;
23

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

node * next;
};
public: struct node *head,*tail,*p;
int count;
dequeue()
{
head=tail=NULL;
count=0;
}
void insertf();
void insertr();
void delf();
void delr();
void display();
int isempty()
{
if(count==0)
return(1);
else
return(0);
}
};
template<class t>
void dequeue<t>::insertf()
{
p=new node;
cout<<enter the ele;
24

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

cin>>ele;
p->data=ele;
if(isempty())
{
p->next=p->prev=NULL;
head=tail=p;
}
else
{
p->next=head;
head->prev=p;
p->prev=NULL;
head=p;
}
count++;
}
template<class t>
void dequeue<t>::insertr()
{
p=new node;
cout<<enter the ele;
cin>>ele;
p->data=ele;
if(isempty())
{
p->next=p->prev=NULL;
head=tail=p;
25

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
else
{
p->prev=tail;
tail->next=p;
p->next=NULL;
tail=p;
}
count++;
}
template<class t>
void dequeue<t>::delf()
{
if(isempty())
cout<<"\n queue list is empty";
else
{
p=head;
cout<<deleted node is<<p->data;
head=p->next;
head->prev=NULL;
delete(p);
}
count--;
}
26

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

template<class t>
void dequeue<t>::delr()
{
if(isempty())
cout<<"\n queue list is empty";
else
{
p=tail;
cout<<deleted ele is<<p->data;
tail=p->prev;
tail->next=NULL;
delete(p);
}
count--;
}
template<class t>
void dequeue<t>::display()
{
if(isempty())
cout<<"\n queue is empty";
else
{
p=head;
while(p!=NULL)
{
cout<<p->data;
27

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

p=p->next;
}
}
}
void main()
{
clrscr();
dequeue<int> a;
int choice;
do
{
cout<<"\n 0-exit,1-insert front,2-insert rear,3-del front,4-del rear,5-display";
cin>>choice;
switch(choice)
{
case 1:a.insertf();
break;
case 2: a.insertr();
break;
case 3: a.delf();
break;
case 4: a.delr();
break;
case 5: a.display();
break;
}
}while(choice!=0);
28

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
Output

29

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week 4
Write a C++ program to perform the following operations:
a) Insert an element into a binary search tree.
b) Delete an element from a binary search tree.
c) Search for a key element in a binary search tree.
#include<iostream.h>
#include<conio.h>
#include<process.h>
enum bool{false,true};
template <class T>
class Node
{
friend BST<T>;
private:
T data;
Node<T> *lchild,*rchild;
};
template <class T>
class BST
{
public: BST()
{ root=NULL;}
~BST();
void destroy(Node<T> *p);
bool search(const T &k);
void insert(const T &e);
void del(const T &k);
void display();
void preorder(Node<T> *p);
private:
Node<T> *root;
};
template<class T>
BST<T>::~BST()
{
destroy(root);
}
template<class T>
void BST<T>::destroy(Node<T> *p)
{
30

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

if(p)
{
destroy(p->lchild);
delete p;
destroy(p->rchild);
}
}
template<class T>
bool BST<T>::search(const T &k)
{
Node<T> *p=root;
while(p)
{
if(k<p->data)
p=p->lchild;
else if(k>p->data)
p=p->rchild;
else return true;
}
return false;
}
template<class T>
void BST<T>::insert(const T &e)
{
Node<T> *p=new Node<T>;
Node<T> *temp1=root,*temp2;
p->data=e;
p->lchild=p->rchild=NULL;
if(root==NULL)
root=p;
else
{
while(temp1)
{
temp2=temp1;
if(e<temp1->data)
temp1=temp1->lchild;
else if(e>temp1->data)
temp1=temp1->rchild;
else{
cout<<"\n element is already there \n";
return;
}
}
if(e>temp2->data)
31

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

temp2->rchild=p;
else temp2->lchild=p;
}
}
template<class T>
void BST<T>::del(const T &k)
{
Node<T> *p=root;
Node<T> *temp=root,*temp2,*s;
while(p)
{
if(k<p->data)
{
temp=p;
p=p->lchild;
}
else if(k>p->data)
{
temp=p;
p=p->rchild;
}
else
{
cout<<p->data<<"is deleted\n";
temp2=p;
if(p->lchild)
{
s=p->lchild;
while(s->rchild)
{
temp2=s;
s=s->rchild;
}
if(temp2!=p)
temp2->rchild=s->lchild;
else
temp2->lchild=s->lchild;
}
else if(p->rchild)
{
s=p->rchild;
while(s->lchild)
{
temp2=s;
s=s->lchild;
32

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
if(temp2!=p)
temp2->lchild=s->rchild;
else
temp2->rchild=s->rchild;
}
else
{
s=p;
if(p->data>temp->data)
temp->lchild=NULL;
else temp->lchild=NULL;
if(p==root)
root=NULL;
}
p->data=s->data;
p=NULL;
delete s;
return;
}
}
cout<<"\n element not found";
return;
}
template<class T>
void BST<T>::display()
{
if(root)
preorder(root);
else cout<<"there are no elements in bst\n";
}
template<class T>
void BST<T>::preorder(Node<T> *p)
{
if(p)
{
cout<<p->data<<"\t";
preorder(p->lchild);
preorder(p->rchild);
}
}
void main()
{
33

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

BST<int> q;
int i,ch,x;
char t;
clrscr();
do
{
cout<<"\n1.insert 2.search 3.delete 4.display 5.exit";
cout<<"\n enter the choice";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n enter the element to be inserted:";
cin>>x;
q.insert(x);
break;
case 2: cout<<"\n enter the element to be search";
cin>>x;
if(q.search(x)) cout<<x<<"is found";
else
cout<<"element not found";
break;
case 3: cout<<"\nenter the element to be deleted";
cin>>x;
q.del(x);
break;
case 4: q.display();
break;
case 5: exit(0);
default: cout<<"\n entered wrong choice";
}
cout<<"\n do u want to continue{y/n} :";
cin>>t;
}while(t!='n' &&t!='N');
}
Output

34

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week5
Write C++ programs that use non-recursive functions to traverse the given binary tree in
a) Preorder b) inorder and c) postorder.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class binarynode
{
public:
int data;
binarynode *left;
binarynode *right;
};
class binsrctree
{
private:
binarynode *root;
void inorder(binarynode *);
void preorder(binarynode *);
void postorder(binarynode *);
public:
binsrctree()
{
root=NULL;
}
35

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

void insert(int );
void print_inorder();
void print_preorder();
void print_postorder();
};
class stack
{
int top;
binarynode *stackel[20];
public:
stack()
{
top=-1;
}
void push(binarynode *);
binarynode* pop();
int empty()
{
if(top==-1)
return(1);

return(0);
}
};
void stack::push(binarynode *node)
36

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

{
stackel[++top]=node;
}
binarynode *stack::pop()
{
return(stackel[top--]);
}
class stack_int
{
int top;
int stack_int[20];
public:
stack_int()
{
top=-1;
}
void push(int flag);
int pop();
int empty_int()
{
if(top==-1)
return(1);

return(0);
}
37

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

};

void stack_int::push(int flag)


{
stack_int[++top]=flag;
}
int stack_int::pop()
{
return(stack_int[top--]);
}
/*---------------------------------------------------------------------*/
/* FUNCTION TO INSERT A NODE IN THE TREE */
/*---------------------------------------------------------------------*/
void binsrctree::insert(int val)
{
binarynode *temp,*prev,*curr;
temp=new binarynode;
temp->data=val;
temp->left=temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
38

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

curr=root;
while(curr!=NULL)
{
prev=curr;
if(temp->data<curr->data)
curr=curr->left;
else
curr=curr->right;
}
if(temp->data<prev->data)
prev->left=temp;
else
prev->right=temp;
}
}

/*--------------------------------------------------*/
/*INORDER NON RECURSIVE TRAVERSAL*/
/*--------------------------------------------------*/
void binsrctree::inorder(binarynode *root)
{
stack stk;
binarynode *temp;
if(root!=NULL)
{
39

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

temp=root;
do
{
while(temp!=NULL)
{
stk.push(temp);
temp=temp->left;
}/*end while*/
if(!stk.empty())
{
temp=stk.pop();
cout<<temp->data<<"

";

temp=temp->right;
}/*end if*/
else
break;
}while(1); /*end do while*/
}/* end if */
else
cout<<"
Empty tree";

} /*end function */
/*--------------------------------------------------*/
/*PREORDER NON RECURSIVE TRAVERSAL */
40

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

/*---------------------------------------------------*/
void binsrctree::preorder(binarynode *root)
{
stack stk;
binarynode *temp=root;
stk.push(temp);
while(!stk.empty())
{
temp=stk.pop();
if(temp!=NULL)
{
cout<<temp->data<<"

";

stk.push(temp->right);
stk.push(temp->left);
}
}
}
/*--------------------------------------------------*/
/*POSTORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::postorder(binarynode *root)
{
stack stk;
stack_int stk1;
int flag;
41

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

binarynode *temp=root;
do
{
if(temp!=NULL)
{
stk.push(temp);
stk1.push(1);
temp=temp->left;
}
else
{
if(stk.empty())
break;
temp=stk.pop();
flag=stk1.pop();
if(flag==2)
{
cout<<temp->data;
temp=NULL;
} /*end if */
else
{
stk.push(temp);
stk1.push(2);
temp=temp->right;
42

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

} /* end else */
} /* end if */
}while(1);/*end do while*/
}/*end function*/
/*--------------------------------------------------*/
/*FUNCTION TO PRINT INORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_inorder()
{
cout<<" ";
inorder(root);
cout<<" ";
}
/*--------------------------------------------------*/
/*FUNCTION TO PRINT PREORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_preorder()
{
cout<<" ";
preorder(root);
cout<<" ";
}
/*--------------------------------------------------*/
/*FUNCTION TO PRINT POSTORDER NON RECURSIVE TRAVERSAL */
/*---------------------------------------------------*/
void binsrctree::print_postorder()
43

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

{
cout<<" ";
postorder(root);
cout<<" ";
}
/*--------------------------------------------------*/
/* MAIN FUNCTION */
/*---------------------------------------------------*/
void main()
{
binsrctree BST;
int ch,element;
clrscr();
do
{
cout<<"1. Insert a node in binary tree";
cout<<"2. Inorder traversal";
cout<<"3.preorder traversal";
cout<<"4. postorder traversal";
cout<<"5. Exit \nEnter your choice";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the element you wnat to insert";
cin>>element;
44

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

BST.insert(element);
break;
case 2: cout<<"Inorder traversal";
BST.print_inorder();
break;
case 3: cout<<preorder traversal";
BST.print_preorder();
break;
case 4: cout<<"postorder traversal";
BST.print_postorder();
break;
case 5:exit(1);
}
}while(ch!=5);
}
OUTPUT

45

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week 6
Write C++ programs for the implementation of bfs and dfs for a given graph.
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
void create(); // For creating a graph
void dfs(); // For Deapth First Search(DFS) Traversal.
void bfs(); // For Breadth First Search(BFS) Traversal.
struct node // Structure for elements in the graph
{
int data,status;
struct node *next;
struct link *adj;
};
struct link // Structure for adjacency list
{
struct node *next;
struct link *adj;
};
struct node *start, *p, *q;
struct link *l, *k;
int main()
{
int choice;
clrscr();
create();
do
46

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

{
cout<<"-----------------------";
cout<<"1: DFS 2: BSF 3: Exit \nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: dfs();
break;
case 2: bfs();
break;
case 3: exit(0);
break;
default: cout<<"Incorrect choice! \nRe-enter your choice.";
getch();
}
}while(choice!=3);
return 0;
}
void create() // Creating a graph
{
int dat,flag=0;
start=NULL;
cout<<"
Enter the nodes in the graph(0 to end): ";
while(1)
{
cin>>dat;
47

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

if(dat==0)
break;
p=new node;
p->data=dat;
p->status=0;
p->next=NULL;
p->adj=NULL;
if(flag==0)
{
start=p;
q=p;
flag++;
}
else
{
q->next=p;
q=p;
}
}
p=start;
while(p!=NULL)
{
cout<<"Enter the links to "<<p->data<<" (0 to end) : ";
flag=0;
while(1)
{
cin>>dat;
48

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

if(dat==0)
break;
k=new link;
k->adj=NULL;
if(flag==0)
{
p->adj=k;
l=k;
flag++;
}
else
{
l->adj=k;
l=k;
}
q=start;
while(q!=NULL)
{
if(q->data==dat)
k->next=q;
q=q->next;
}
}
p=p->next;
}
cout<<"-------------------------";
return;
49

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
// Deapth First Search(DFS) Traversal.
void bfs()
{
int qu[20],i=1,j=0;
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
qu[0]=p->data;
p->status=1;
while(1)
{
if(qu[j]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==qu[j])
break;
p=p->next;
}
k=p->adj;
while(k!=NULL)
50

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

{
q=k->next;
if(q->status==0)
{
qu[i]=q->data;
q->status=1;
qu[i+1]=0;
i++;
}
k=k->adj;
}
j++;
}
j=0;
cout<<"Breadth First Search Results";
cout<<"--------------------------";
while(qu[j]!=0)
{
cout<<qu[j]<<" ";
j++;
}
getch();
return;
}

51

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

// Breadth First Search(BFS) Traversal.


void dfs()
{
int stack[25],top=1;
cout<<"Deapth First Search Results";
cout<<"---------------------------";
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
stack[0]=0;
stack[top]=p->data;
p->status=1;
while(1)
{
if(stack[top]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==stack[top])
break;
p=p->next;
}
52

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

cout<<stack[top]<<" ";
top--;
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
top++;
stack[top]=q->data;
q->status=1;
}
k=k->adj;
}
}
getch();
return;
}
OUTPUT

53

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week 7
Write C++ programs for implementing the following sorting methods:
a) Merge sort b) Heap sort
/* PROGRAM TO IMPLEMENT MERGE SORT*/
#include<iostream.h>
#include<conio.h>
template<class t>
class sort
{
public:
int n;
t *a,*b;
void merge(int,int,int);
void mergesort(int,int);
void display();
};
template<class t>
void sort<t>::mergesort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
54

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


}
template<class t>
void sort<t>::merge(int low,int mid,int high)
{
int i,j,k,h;
i=low;
j=low;
k=mid+1;
h=high;
while(j<=mid&&k<=h)
{
if(a[j]<a[k])
{
b[i]=a[j];
++j;
}
else
{
b[i]=a[k];
k++;
}
i++;
}
if(j>mid)
{
while(k<=h)
{
b[i]=a[k];
55

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


i++;
k++;
}
}
else
{
while(j<=mid)
{
b[i]=a[j];
i++;
j++;
}
}
for(i=low;i<=h;i++)
a[i]=b[i];
}
template<class t>
void sort<t>::display()
{
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void main()
{
sort <int> s;
clrscr();
cout<<"\nenter the size of arrray: " ;
cin>>s.n;
56

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


cout<<"\nenter the array elements: ";
for(int i=0;i<s.n;i++)
cin>>s.a[i];
cout<<"\n the elements before sorting\n";
s.display();
s.mergesort(0,s.n-1);
cout<<"\nthe elements after sorting\n" ;
s.display();
getch();
}
OUTPUT

/* PROGRAM TO IMPLEMENT HEAP SORT*/


#include<iostream.h>
#include<conio.h>
template<class t>
class heap
{
57

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


public:
t *a;
int i,j,k,n;
void sort(int);
void heapify(int);
void adjust(int,int);
void display();
};
template<class t>
void heap<t>::adjust(int i,int n)
{
int j=2*i;
t item=a[i];
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
++j;
if(item>=a[j])
break;
a[j/2]=a[j];
j=2*j;
}
a[j/2]=item;
}
template<class t>
void heap<t>::heapify(int n)
{
for(i=n/2;i>=1;i--)
58

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


adjust(i,n);
}
template<class t>
void heap<t>::sort(int n)
{
heapify(n);
for(int i=n;i>1;i--)
{
int temp=a[i];
a[i]=a[1];
a[1]=temp;
adjust(1,i-1);
}
}
template<class t>
void heap<t>::display()
{
for(int i=1;i<=n;i++)
cout<<a[i]<<"\t";
}
void main()
{
clrscr();
heap<int> h;
cout<<"\nenter the size of array: ";
cin>>h.n;
cout<<"\nenter the array elements :";
for(int i=1;i<=h.n;i++)
59

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual


cin>>h.a[i];
cout<<"\nthe elements before sorting\n";
h.display();
h.sort(h.n);
cout<<"\nthe elemnts after sorting\n";
h.display();
getch();
}
OUTPUT

60

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week 8
Write a C++ program to perform the following operations
a) Insertion into a B-tree b) Deletion from a B-tree
#include<iostream.h>
#include<conio.h>
#include<process.h>
class B_Tree
{
int i,n[];
public:
void search(int,int);
void create(T);
void splitChild(int,int,int);
void insert(int);
void insertNonFull(int);
void delete(int,int);
}
// B-Tree-Search(x, k)
void B_Tree::search(int x,int k)
{
i <- 1
while i <= n[x] and k > keyi[x]
do i <- i + 1
if i <= n[x] and k = keyi[x]
then return (x, i)
if leaf[x]
then return NIL
else Disk-Read(ci[x])
return B-Tree-Search(ci[x], k)
}

61

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

// B-Tree-Create(T)
void B_Tree::create(T)
{
x <- Allocate-Node()
leaf[x] <- TRUE
n[x] <- 0
Disk-Write(x)
root[T] <- x
}
// B-Tree-Split-Child(x, i, y)
void B_Tree::splitChild(int x,int i,int y)
{
z <- Allocate-Node()
leaf[z] <- leaf[y]
n[z] <- t - 1
for j <- 1 to t - 1
do keyj[z] <- keyj+t[y]
if not leaf[y]
then for j <- 1 to t
do cj[z] <- cj+t[y]
n[y] <- t - 1
for j <- n[x] + 1 downto i + 1
do cj+1[x] <- cj[x]
ci+1 <- z
for j <- n[x] downto i
do keyj+1[x] <- keyj[x]
keyi[x] <- keyt[y]
n[x] <- n[x] + 1
Disk-Write(y)
Disk-Write(z)
Disk-Write(x)
}
// B-Tree-Insert(T, k)
void B_Tree::insert(T,int k)
{
r <- root[T]
if n[r] = 2t - 1
then s <- Allocate-Node()
root[T] <- s
leaf[s] <- FALSE
n[s] <- 0
c1 <- r
B-Tree-Split-Child(s, 1, r)
B-Tree-Insert-Nonfull(s, k)
62

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

else B-Tree-Insert-Nonfull(r, k)
}
// B-Tree-Insert-Nonfull(x, k)
void B_Tree::insertNonFull(int x,int k)
{
i <- n[x]
if leaf[x]
then while i >= 1 and k < keyi[x]
do keyi+1[x] <- keyi[x]
i <- i - 1
keyi+1[x] <- k
n[x] <- n[x] + 1
Disk-Write(x)
else while i >= and k < keyi[x]
do i <- i - 1
i <- i + 1
Disk-Read(ci[x])
if n[ci[x]] = 2t - 1
then B-Tree-Split-Child(x, i, ci[x])
if k > keyi[x]
then i <- i + 1
B-Tree-Insert-Nonfull(ci[x], k)
}
// B-Tree-Delete(x,k)
void B_Tree::delete(int x,int k)
{
If leaf[x]!=NULL && key[x]<-k
then leaf[x]=NULL;
else cout<<element not exists;
}
void main()
{
int ch,n;
B_Tree b;
do
{
63

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

cout<<1.Create 2.Insert 3.Insertnonfull 4.Search 5.SplitChild 6.Delete


7.Exit;
cout<<Enter your choice:;
cin>>ch;
switch(ch)
{
case 1: cout<<enter the element to create the B-Tree:;
cin>>n;
b.create(x,n);
break;
case 2: cout<<enter the element to be inserted;
cin>>n;
b.insert(T,n);
break;
case 3: cout<<enter the element when the tree is nonfull;
cin>>n;
b.insertNonFull(x,n);
break;
case 4: cout<<enter the element to be searched:;
cin>>n;
b.search(x,n);
case 5: b.splitChild(x,i,n);
break;
case 6: cout<<enter the element to be deleted;
cin>>n;
64

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

b.delete(x,n);
break;
case 7: exit(0);

}
}while(ch!=7);
getch();
}
Output

WEEK 9
Write a C++ program to perform the following operations
a) Insertion into an AVL-tree b) Deletion from an AVL-tree
#include<iostream>
#include<stdlib.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define NULL 0
class AVL;
class AVLNODE
{
friend class AVL;
private:
int data;
AVLNODE *left,*right;
int bf;
};
class AVL
{
private:
AVLNODE *root;
public:
AVLNODE *loc,*par;
AVL()
65

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

{
root=NULL;
}
int insert(int);
void displayitem();
void display(AVLNODE *);
void removeitem(int);
void remove1(AVLNODE *,AVLNODE *,int);
void remove2(AVLNODE *,AVLNODE *,int);
void search(int x);
void search1(AVLNODE *,int);
};
int AVL::insert(int x)
{
AVLNODE *a,*b,*c,*f,*p,*q,*y,*clchild,*crchild;
int found,unbalanced;
int d;
if(!root) //special case empty tree
{
y=new AVLNODE;
y->data=x;
root=y;
root->bf=0;
root->left=root->right=NULL;
return TRUE;
}
//phase 1:locate insertion point for x.a keeps track of the most
// recent node with balance factor +/-1,and f is the parent of a
// q follows p through the tree.
f=NULL;
a=p=root;
q=NULL;
found=FALSE;
while(p&&!found)
{
//search for insertion point for x
if(p->bf)
{
a=p;
f=q;
}
if(x<p->data) //take left branch
{
q=p;
p=p->left;
}
else if(x>p->data)
{
q=p;
66

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

p=p->right;
}
else
{
y=p;
found=TRUE;
}
}
//end while
//phase 2:insert and rebalance.x is not in the tree and
// may be inserted as the appropriate child of q.
if(!found)
{
y = new AVLNODE;
y->data=x;
y->left=y->right=NULL;
y->bf=0;
if(x<q->data) //insert as left child
q->left=y;
else
q->right=y; //insert as right child
//adjust balance factors of nodes on path from a to q
//note that by the definition of a,all nodes on this
//path must have balance factors of 0 and so will change
//to +/- d=+1 implies that x is inserted in the left
// subtree of a d=-1 implies
//to that x inserted in the right subtree of a.

if(x>a->data)
{
p=a->right;
b=p;
d=-1;
}
else
{
p=a->left;
b=p;
d=1;
}
while(p!=y)
if(x>p->data)
//height of right increases by 1
{
p->bf=-1;
p=p->right;
}
67

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

else
{

//height of left increases by 1


p->bf=1;
p=p->left;

}
//is tree unbalanced
unbalanced=TRUE;
if(!(a->bf)||!(a->bf+d))
{
//tree still balanced
a->bf+=d;
unbalanced=FALSE;
}
if(unbalanced) //tree unbalanced,determine rotation type
{
if(d==1)
{
//left imbalance
if(b->bf==1)
//rotation type LL
{
a->left=b->right;
b->right=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->left=c->right;
c->left=b;
c->right=a;

switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
68

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

} //end of LR
}
//end of left imbalance
else //right imbalance
{
if(b->bf==-1) //rotation type RR
{
a->right=b->left;
b->left=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->right=c->left;
c->right=b;
c->left=a;
switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
}
//subtree with root b has been rebalanced and is the new subtree
if(!f)
root=b;
else if(a==f->left)
f->left=b;
else if(a==f->right)
f->right=b;
} //end of if unbalanced
return TRUE;
}
//end of if(!found)
return FALSE;
69

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

//end of AVL INSERTION

void AVL::displayitem()
{
display(root);
}
void AVL::display(AVLNODE *temp)
{
if(temp==NULL)
return;
cout<<temp->data<<" ";
display(temp->left);
display(temp->right);
}
void AVL::removeitem(int x)
{
search(x);
if(loc==NULL)
{
cout<<"\nitem is not in tree";
return;
}
if(loc->right!=NULL&&loc->left!=NULL)
remove1(loc,par,x);
else
remove2(loc,par,x);
}
void AVL::remove1(AVLNODE *l,AVLNODE *p,int x)
{
AVLNODE *ptr,*save,*suc,*psuc;
ptr=l->right;
save=l;
while(ptr->left!=NULL)
{
save=ptr;
ptr=ptr->left;
}
suc=ptr;
psuc=save;
remove2(suc,psuc,x);
if(p!=NULL)
if(l==p->left)
p->left=suc;
else
p->right=suc;
else
70

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

root=l;
suc->left=l->left;
suc->right=l->right;
return;
}
void AVL::remove2(AVLNODE *s,AVLNODE *p,int x)
{
AVLNODE *child;
if(s->left==NULL && s->right==NULL)
child=NULL;
else if(s->left!=NULL)
child=s->left;
else
child=s->right;
if(p!=NULL)
if(s==p->left)
p->left=child;
else
p->right=child;
else
root=child;
}
void AVL::search(int x)
{
search1(root,x);
}
void AVL::search1(AVLNODE *temp,int x)
{
AVLNODE *ptr,*save;
int flag;
if(temp==NULL)
{
cout<<"\nthe tree is empty";
return;
}
if(temp->data==x)
{
cout<<"\nthe item is root and is found";
par=NULL;
loc=temp;
par->left=NULL;
par->right=NULL;
return;
}
if( x < temp->data)
{
71

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

ptr=temp->left;
save=temp;
}
else
{
ptr=temp->right;
save=temp;
}
while(ptr!=NULL)
{
if(x==ptr->data)
{
flag=1;
cout<<"\nitemfound";
loc=ptr;
par=save;
}
if(x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(flag!=1)
{
cout<<"item is not there in tree";
loc=NULL;
par=NULL;
cout<<loc;
cout<<par;
}
}
main()
{
AVL a;
int x,y,c;
char ch;
do
{
cout<<"\n1.insert";
cout<<"\n2.display";
cout<<"\n3.delete";
cout<<"\n4.search";
cout<<"\n5.exit";
cout<<"\nEnter u r choice to perform on AVL tree";
cin>>c;
72

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

switch(c)
{
case 1:cout<<"\nEnter an element to insert into tree";
cin>>x;
a.insert(x);
break;
case 2:a.displayitem(); break;
case 3:cout<<"\nEnter an item to deletion";
cin>>y;
a.removeitem(y);
break;
case 4:cout<<"\nEnter an element to search";
cin>>c;
a.search(c);
break;
case 5:exit(0);
break;
default :cout<<"\nInvalid option try again";
}
cout<<"\ndo u want to continue";
cin>>ch;
}
while(ch=='y'||ch=='Y');
}

OUTPUT

73

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week 10
Write a C++ program to implement Kruskals algorithm to generate a minimum cost spanning
tree.
#include<iostream.h>
#include<conio.h>
#define MAX 20
struct edge
{
int weight,u,v;
};
class minspan
{
int **a,*parent;
edge *e;
int n,ne;
public:
minspan(int n1);
void edgecost();
void edgesort();
int find(int);
void union1(int,int);
void kruskal();
void display();
};

minspan::minspan(int n1)
{
n=n1;
a=new int *[n+1];
for(int i=1;i<=n;i++)
a[i]=new int[n+1];
cout<<"enter weight matrix";
for(i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
e=new edge[n*n];
parent=new int[n];
for(i=1;i<=n;i++)
parent[i]=0;
ne=0;
}

74

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

void minspan::edgecost()
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(a[i][j]!=1000)
{
e[ne].weight=a[i][j];
e[ne].u=i;
e[ne].v=j;
ne++;
}
}
}
void minspan::edgesort()
{
struct edge temp;
int i,j;
for(i=1;i<ne;i++)
for(j=1;j<ne-i;j++)
{
if(e[j].weight>e[j+1].weight)
{
temp=e[j];
e[j]=e[j+1];
e[j+1]=temp;
}
}
}
int minspan::find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
void minspan::union1(int i,int j)
{
parent[j]=i;
}
void minspan::kruskal()
{
int a,b,c,i,j,k,index,mincost,p;
edge temp;
int t[MAX][2];
int f=ne-1;
75

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

i=0;
index=1;
mincost=0;
p=0;
edgesort();
while(i<n-1 && f)
{
temp=e[index];
a=temp.weight;
b=temp.u;
c=temp.v;
j=find(b);
k=find(c);
if(j!=k)
{
i++;
t[i][1]=b;
t[i][2]=c;
mincost += a;
p++;
union1(j,k);
}
index++;
f--;
}
if(i!=n-1)
cout<<"no minimum spanning tree"<<endl;
else
{
cout<<"minimum cost:"<<mincost<<endl;
for(i=1;i<=p;i++)
cout<<t[i][1]<<"\t"<<t[i][2]<<endl;
}
}
void minspan::display()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<a[i][j]<<"\t";
cout<<endl;
}
}
void main()
{
76

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

int n;
clrscr();
cout<<"enter no of vertices"<<endl;
cin>>n;
minspan m(n);
m.display();
m.edgecost();
m.kruskal();
getch();
}
OUTPUT:

77

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week 11
Write a C++ program to implement Prims algorithm to generate a minimum cost spanning tree.

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#define max 6
#define INFINITY 1000
int edge[max][2];
class prim
{
int cost[max][max];
int n;
int mincost;
public:

prim(int);
void getdata();
void display();
void find();

};
prim::prim(int n1)
{
n=n1;
mincost=0;
}
void prim::getdata()
{
78

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

cout<<"enter cost adjacency matric\n";


cout<<"enter 99 to indicate no edge";
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>cost[i][j];
}
}
void prim::display()
{
for(int i=1;i<=n-1;i++)
mincost=mincost+cost[edge[i][1]][edge[i][2]];
cout<<"the solution vector to MWst="<<mincost;
if(mincost>=1000)
{
cout<<"there is no minimum spanning tree";
exit(1);
}
cout<<"edges of the minimum spanning tree";
for(i=1;i<=n-1;i++)
cout<<"<"<<edge[i][1]<<edge[i][2]<<">";
}
void prim::find()
{
int closest[max];
int lowcost[max],min;
79

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

int i,j,k;
for(i=2;i<=n;i++)
{
lowcost[i]=cost[1][i];
closest[i]=1;
}
for(i=2;i<=n;i++)
{
min=lowcost[2];
k=2;
for(j=3;j<=n;j++)
if(lowcost[j]<min)
{
min=lowcost[j];
k=j;
}
edge[i-1][1]=closest[k];
edge[i-1][2]=k;
lowcost[k]=INFINITY;
for(j=2;j<=n;j++)
if(cost[k][j]<lowcost[j])
if(lowcost[j]<INFINITY)
{
lowcost[j]=cost[k][j];
closest[j]=k;
}
}
80

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
void main()
{
int n;
clrscr();
cout<<"enter no of vertices:";
cin>>n;
prim p(n);
p.getdata();
p.find();
p.display();
getch();
}
OUTPUT

81

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Week 12
Write a C++ program to implement all the functions of a dictionary (ADT) using hashing.
#include<iostream.h>
#include<conio.h>
#include<process.h>
#ifndef _HASHTABLE_H
#define _HASHTABLE_H
#include "type.h"
#include "list.h"
#include "listiter.h"
#include <string.h>
#define TABLE_SIZE 33
class Hashtable
{
public:
Hashtable();
bool insert (Type *);
void view();
Type* find (char*);
bool remove(char *);
private:
List* table[TABLE_SIZE];
int hash (char*); //hash function
};
#endif //_HASHTABLE_H

#include "hashtable.h"
#define DEBUG 1
Hashtable::Hashtable()
{
for(int i = 0; i < TABLE_SIZE; i++)
table[i] = 0;
}
//Hash Function: sum of the ASCII codes of the characters % TABLE_SIZE
int Hashtable::hash (char *l)
{
82

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

int long sum = 0;


int len = strlen(l);
for(int i = 0; i < len; i++)
sum += l[i];
return sum % TABLE_SIZE;
}
bool Hashtable::insert (Type* new_item)
{
int index;
List* list;
Node* p;
index = hash(new_item->get_key());
if(DEBUG)
cout<<"\nThe index given by a hashfunction is "<<index;
if(table[index] == 0){
table[index] = new List(*new_item);
return true;
}
else {
list = table[index];
p = list->finditem(*new_item);
if(p == NULL)
list->insert(*new_item);
else
p->setinfo(*new_item);
return true;
}
return false; //should never get here
}
void Hashtable::view()
{
for(int i = 0; i < TABLE_SIZE; i++) {
if(table[i] != 0 ){
List list = *table[i];
83

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

ListIter iter(list);
while(!iter.done()) {
if(strcmp(iter.value().get_key(), "*") != 0)
cout << "Index: " << i << iter.value();
iter.next();
}
}
}
}
Type* Hashtable::find (char* key)
{
int index, new_index;
Type *item;
List *list;
Node *p;
item = new Type(key);
new_index = index = hash(key);
if (table[index] == 0)
return 0;
list = table[index];
p = list->finditem(*item);
if(p == NULL)
return NULL;
else {
*item = p->getinfo();
return item;
}
}
bool Hashtable::remove(char *key)
{
int index, new_index;
List list;
new_index = index = hash(key);
if (table[index] == 0)
84

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

return false;
list = *table[index];
ListIter iter(list);
while(!iter.done()) {
Node *node = iter.lookup();
Type value = node->getinfo();
if (strcmp(value.get_key(), key) == 0) {
value.set_key("*");
node->setinfo(value);
return true;
}
iter.next();
}
return false;
}
void main()
{
Hashtable *table;
table = new Hashtable();
int flag = 1;
while(flag)
{
int choice;
cout << "\n 1. Insert 2. Delete 3. Search 4. View 5. Exit ;
cout << "\n Please enter your choice: ";
cin >> choice;
cin.get();
if(choice == 5)
flag = 0;
else if(choice == 1)
{
char last_name[20];
char first_name[20];
char key[11];
85

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

double gpa;
Student *st;
bool result;
cout << "\nEnter the key" << endl;
cin >> key;
cin.get ();
cout << "\nEnter last name" << endl;
cin >> last_name;
cin.get();
cout << "\nEnter first name" << endl;
cin >> first_name;
cin.get ();
cout << "\nEnter GPA" << endl;
cin >> gpa;
cin.get();
st = new Type(last_name, first_name, gpa, key);
result = table->insert(st);
if(result == true)
cout<<endl<<"The record was successfully inserted" << endl;
else
cout << endl << "The record was not inserted" << endl;
}
else if(choice == 2)
{
char key[20];
bool result;
cout << "\nEnter the key" << endl;
cin >> key;
cin.get ();
result = table->remove(key);
if(result == true)
cout<<"\nThe record was successfully deleted from the table";
else
cout << "The record was not found." << endl;
}
else if(choice == 3)
{
86

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

char key[20];
Student *s;
cout << "\nEnter the key" << endl;
cin >> key;
cin.get ();
s = table->find(key);
if(s)
cout << "Found record:" << *s;
else
cout << endl << "The record was not found" << endl;
}
else if(choice == 4)
{
table->view();
}
else
cout << "Invalid choice" << endl;
};
}
Output

Write a C Program to implement Stack using an array


#include<stdio.h>
#define MAX 5
int top = -1;
int stack_arr[MAX];

main()
{
int choice;
while(1)
87

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
push()
{
int pushed_item;
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack : ");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top] = pushed_item;
}
}/*End of push()*/
pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
88

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

{
printf("Popped element is : %d\n",stack_arr[top]);
top=top-1;
}
}/*End of pop()*/
display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of display()*/

Output

89

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Write a C program to implement Queue using array


# include
# define MAX 5
int queue_arr[MAX];
int rear = -1;
int front = -1;
main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert();
break;
case 2 : del();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
90

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

insert()
{
int added_item;
if (rear==MAX-1)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
}
}/*End of insert()*/
del()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
}
}/*End of del() */
display()
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
for(i=front;i<= rear;i++)
91

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

printf("%d ",queue_arr[i]);
printf("\n");
}
}/*End of display() */

Output

Write a program to implement addition of two numbers in C++


#include<iostream.h>
#include<conio.h>
Class add
{
public: int a, b,c;
sum()
{
c=a+b;
}
get()
{
cout<<enter the values of a & b;
cin>>a>>b;
}
put()
{
Cout<<sum is<<c;
92

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
};
Void main()
{
add a;
a.get();
a.sum();
a.put();
getch();
}

Output:

Write a c++ program to find biggest of two numbers


Class large
{
public: int a,b;
get()
{
cout<<enter the values of a & b;
cin>>a>>b;
}
Void largest();
};
Void large::largest()
{
if(a>b)
cout<<largest no is <<a;
93

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

else
cout<<largest no is<<b;
}
Void main()
{
large l;
l.get();
l.largest();
}

Output:
Write a C++ program to implement operator overloading
#include<iostream.h>
class space
{
private:
int x;
int y;
int z;
public:
void getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void display()
{
cout<<<"";
cout<<<"";
cout<<<"\n";
}
void operator-()
{
x=-x;
94

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

y=-y;
z=-z;
}
};
void main()
{
clrscr();
space s;
s.getdata(10,-20,30);
cout<<"s :";
s.display();
-s;
cout<<"s :";
s.display();
getch();
}
Output

95

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

Write a C++ program to implement single inheritance


class base
{
int a;
public:
int b;
void getdata(void);
void putdata(void);
int get_value(void);
void put_value(void);
};
class derived: public base //Public derivation
{
int c;
public:
void sum(void);
void display(void);
};
void base::getdata(void)
{
cout<<"Enter a and b";
cin>>a>>b;

96

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

}
void base::putdata(void)
{
cout<<"a="<<a<<endl<<"b="<<b<<endl;
}
int base::get_value(void)
{
return (a);
}
void base::put_value(void)
{
cout<<"Base Class Private Member a="<<a<<endl;
}
void derived::sum(void)
{
c=b+get_value();
}
void derived::display(void)
{
cout<<"\n In Derived Class\n";
cout<<"a="<<get_value()<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
int main()
{
derived dobj1;
dobj1.getdata();
dobj1.putdata();
dobj1.put_value();
97

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

dobj1.sum();
dobj1.display();
dobj1.b=15;
dobj1.sum();
dobj1.display();
return(0);
}

Output

Write a C++ program to implement virtual functions


class Window
{
public:
virtual void Create() // virtual function
{
cout <<"Base class Window";
}
};
class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button ";
}
};
void main()
{
Window *x, *y;
x = new Window();
x->Create();
y = new CommandButton();
98

--------------------------------------------------------------------------------------------------DS Through C++ Lab Manual

y->Create();
}

Output

99