Вы находитесь на странице: 1из 47

Contents

EXPT . NO 01 02 DATE NAME OF THE EXPERIMENT PAGE . NO SIGNATURE

IMPLEMENTATION OF STACK (USING ARRAY) IMPLEMENTATION OF QUEUE (USING ARRAY) IMPLEMENTATION OF STACK (USING POINTER) IMPLEMENTATION OF QUEUE (USING POINTER) IMPLEMENTATION OF SINGLY LINKED LIST IMPLEMENTATION OF DOUBLY LINKED LIST BINARY SEARCH TREE

03

04

05

06

07

08

HEAP SORT

09

QUICK SORT

10

BINARY TREE TRAVERSAL

11

MERGE SORT

IMPLEMENTATION OF STACK

(USING ARRAY)
#include<iostream.h> #include<stdlib.h> #include<conio.h> #define MAX 50 class stack { private: int sarr[50]; int top; public: void display(); stack():top(-1) { } void push(int i) { if(top==MAX-1) { cout<<"Stack is full"<<endl; getch(); return; } top++; sarr[top]=i; } int pop() { int d; if(top==-1) { cout<<"Stack is empty"<<endl; getch(); return NULL; } else { d=sarr[top]; top--; } return d; } }; void stack::display() { if(top<0) cout<<"stack is empty";

else { for(int i=top;i>=0;i--) cout<<sarr[i]<<"\n"; } } void main() { stack s1; int ch,item; do { clrscr(); cout<<"1.Push Operation"<<endl; cout<<"2.Pop Operation"<<endl; cout<<"3.Dispaly"<<endl; cout<<"4.Exit"<<endl<<endl; cout<<"Enter your choice(1-4):"; cin>>ch; switch(ch) { case 1: cout<<"Enter the value:"; cin>>item; s1.push(item); getch(); break; case 2: item=s1.pop(); cout<<"Poped the item:"<<item<<endl; s1.display(); getch(); break; case 3: s1.display(); getch(); break; case 4: cout<<"Bye..."; getch(); break; } }while(ch!=4); }

OUTPUT:
1.Push Operation 2.Pop Operation 3.Dispaly 4.Exit Enter your choice(1-4):1 Enter the value:1 Enter your choice(1-4):1 Enter the value:2 Enter your choice(1-4):1 Enter the value:3 Enter your choice(1-4):3 3 2 1 Enter your choice(1-4):2 Poped the item:3 2 1 Enter your choice(1-4):4 Bye...

IMPLEMENTATION OF QUEUE

(USNIG ARRAY)

#include<iostream.h> #include<conio.h> #define MAX 50 class queue { private: int qarr[MAX]; int front,rear; public: queue() : front(-1),rear(-1) { } void display(); void addqueue(int d) { if(rear==MAX-1) { cout<<"queue is full"<<endl; getch(); return; } rear++; qarr[rear]=d; if(front==-1) front=0; } int delqueue() { int d; if(front==-1) { cout <<"queue is empty" <<endl; getch(); return NULL; } else { d=qarr[front]; if(front==rear)

{
front=-1; rear=-1; }

else front++; } return d; } }; void queue::display() { if(front==-1) cout <<" Element is empty"; else { cout <<" Content of queue"; for(int i=front;i<=rear;i++) cout <<qarr [i] <<"->"; } } void main() { queue Q1; int ch,item; do { clrscr(); cout <<"1.Add queue"<<endl; cout <<"2.Delete queue"<<endl; cout <<"3.Display"<<endl; cout <<"4.Quit"<<endl<<endl; cout <<"Enter your choice(1-4)"; cin >> ch; switch(ch) { case 1: cout<<"Enter the value"; cin>>item; Q1.addqueue(item); Q1.display(); getch(); break; case 2: item=Q1.delqueue(); cout<<"Delete the item:" <<item<<endl; Q1.display(); getch(); break; case 3: Q1.display();

getch(); break; case 4: cout <<"Bye....."; getch(); break; } }while(ch!=4); }

OUTPUT:
1.Add queue 2.Delete queue 3.Display 4.Quit Enter your choice(1-4):1 Enter the value:10 Content of queue 10-> Enter your choice(1-4):1 Enter the value:20 Content of queue 10->20-> Enter your choice(1-4):1 Enter the value:30 Content of queue 10->20->30-> Enter your choice(1-4):3 Content of queue 10->20->30-> Enter your choice(1-4):2 Delete the item:10 Content of queue 20->30-> Enter your choice(1-4):4 Bye.....

IMPLEMENTATION OF STACK
(USING POINTER)

#include<iostream.h> #include<conio.h> struct node { int data; node*link; }; class stack { private: node*top; public: void display(); stack():top(NULL) { } void push(int item) { node*t; t=new node; if(t==NULL) cout<<"Stack is full"<<endl; else t->data=item; t->link=top; top=t; } int pop() { if(top!=NULL) { node*t; int item; t=top; item=t->data; top=t->link; delete t; return item; } else { cout<<"stack is empty"<<endl;

return NULL; } } ~stack(); }; void stack::display() { node*t=top; if(t==NULL) cout<<"Stack is empty"; else { cout<<"Element in stack are"<<endl; while(t!=NULL) { cout<<t->data<<"\n"; t=t->link; } } } stack::~stack() { if(top==NULL) return; node*t; while(top!=NULL) { t=top; top=top->link; delete t; } } void main() { stack S1; int ch,item; do { clrscr(); cout<<"1.Push operation"<<endl; cout<<"2.Pop operation"<<endl; cout<<"3.Display"<<endl; cout<<"4.Exit"<<endl; cout<<"Enter your choice(1-4):"; cin>>ch; switch(ch) { case 1: cout<<"Enter the values:"<<endl;

cin>>item; S1.push(item); S1.display(); getch(); break; case 2: item=S1.pop(); cout<<"Deleted the item:"<<item<<endl; S1.display(); getch(); break; case 3: S1.display(); getch(); break; case 4: cout<<bye; getch(); break; } }while(ch!=4); }

OUTPUT:
1.Push operation 2.Pop operation 3.Display 4.Exit Enter your choice(1-4):1 Enter the values: 60 Element in stack are 60 Enter your choice(1-4):1 Enter the values: 70 Element in stack are 70 60 Enter your choice(1-4):1 Enter the values: 80 Element in stack are 80 70 60 Enter your choice(1-4):3 Element in stack are 80 70 60 Enter your choice(1-4):2 Deleted the item:80 Element in stack are 70 60 Enter your choice(1-4):4 bye...

IMPLEMENTATION OF QUEUE
(USING POINTER)

#include<iostream.h> #include<conio.h> struct node { int data; node*link; }; class queue { private: node*front,*rear; public: void display(); queue():front(NULL),rear(NULL) { } void addqueue(int item) { node*t; t=new node; if(t==NULL) cout<<"queue is full"<<endl; else t->data=item; t->link=NULL; if(front==NULL) { rear=front=t; return; } rear->link=t; rear=rear->link; } int delqueue() { if(front!=NULL) { node*t; int item; t=front; item=t->data; front=t->link;

delete t; return item; } else { cout<<"queue is empty"<<endl; return NULL; } } ~queue(); }; void queue::display() { node*t; t=front; if(t==NULL) cout<<"queue is empty"; else { cout<<"Elements in the queue are:\n"; while(t!=NULL) { cout<<t->data<<"\t"; t=t->link; } } } queue::~queue() { if(front==NULL) return; node*t; while(front!=NULL) { t=front; front=front->link; delete t; } } void main() { queue q1; int ch,item; char str[]="bye!bye!!bye!!!"; do { clrscr(); cout<<"1.Add an element in the queue"<<endl; cout<<"2.Delete an element in the queue"<<endl;

cout<<"3.Display"<<endl; cout<<"4.Exit"<<endl; cout<<"Enter your choice(1-4):"; cin>>ch; switch(ch) { case 1: cout<<"Adding values to the queue"<<endl; cout<<"Enter the value you want to insert:"; cin>>item; q1.addqueue(item); q1.display(); getch(); break; case 2: cout<<"Deleting values from the queue"<<endl; item=q1.delqueue(); cout<<"Deleted the item:"<<item<<endl; q1.display(); getch(); break; case 3: q1.display(); getch(); break; case 4: cout<<Bye; getch(); break; } }while(ch!=4); }

OUTPUT:
1.Add an element in the queue 2.Delete an element in the queue 3.Display 4.Exit Enter your choice(1-4):1 Adding values to the queue Enter the value you want to insert:60 Elements in the queue are: 60 Enter your choice(1-4):1 Adding values to the queue Enter the value you want to insert:70 Elements in the queue are: 60 70 Enter your choice(1-4):1 Adding values to the queue Enter the value you want to insert:90 Elements in the queue are: 60 70 90 Enter your choice(1-4):3 Elements in the queue are: 60 70 90 Enter your choice(1-4):2 Deleting values from the queue Deleted the item:60 Elements in the queue are: 70 90 Enter your choice(1-4):4 Bye

IMPLEMENTATION OF SINGLY LINKED LIST


#include<iostream.h> #include<conio.h> #include<dos.h> #include<string.h> class SList { private: struct node { node*link; int data; }*p; public: SList():p(NULL) { } ~SList(); void AddBeg(int); void AddEnd(int); void AddAfter(int,int); void SListShow(); void Delete(int); int SListCount(); void Display(); }; void SList::AddEnd(int n) { node*q,*t; if(p==NULL) { p=new node; p->link=NULL; p->data=n; } else { q=p; while(q->link!=NULL) q=q->link; t=new node; t->data=n; t->link=NULL; q->link=t; } }

void SList::AddBeg(int n) { node*q; q=new node; q->link=p; q->data=n; p=q; } void SList::AddAfter(int c,int n) { node*q,*t; int i; for(i=0,q=p;i<c;i++) { q=q->link; if(q==NULL) { cout<<"there are less than"<<c<<"Element"<<endl; return; } } t=new node; t->data=n; t->link=q->link; q->link=t; } void SList::Delete(int n) { node*q,*r; q=p; if(p->data==n) { p=q->link; delete q; return; } r=q; while(q!=NULL) { if(q->data==n) { r->link=q->link; delete q; return; } r=q; q=q->link; } cout<<"Element"<<"not in the list"<<endl;

} void SList::SListShow() { node*q; for(q=p;q!=NULL;q=q->link) cout<<q->data<<endl; } int SList::SListCount() { node*q; int c=0; for(q=p;q!=NULL;q=q->link) c++; return c; } SList::~SList() { node*q; if(p==NULL) return; while(p!=NULL) { q=q->link; delete p; p=q; } } void SList::Display() { if(p==NULL) cout<<"\nlist is empty \n"; else { cout<<"\n Element in the linked list \n"; node*q=p; while(q!=NULL) { cout<<q->data<<"--->"; q=q->link; } } }

void main() { SList L1;

int ch,item,after; char str1[40]="bye!bye!!bye!!!"; do { clrscr(); cout<<"1.Add at Begining"<<endl; cout<<"2.Add at End"<<endl; cout<<"3.Add at After"<<endl; cout<<"4.show list"<<endl; cout<<"5.count list"<<endl; cout<<"6.Delete node"<<endl; cout<<"7.Display"<<endl; cout<<"8.Quit"<<endl<<endl; cout<<"Enter your choice(1-8):"; cin>>ch; switch(ch) { case 1: cout<<"Enter the node value"; cin>>item; L1.AddBeg(item); L1.SListShow(); L1.Display(); getch(); break; case 2: cout<<"Enter the node value"; cin>>item; L1.AddEnd(item); L1.SListShow(); L1.Display(); getch(); break; case 3: cout<<"Enter the node value"; cin>>item; cout<<"Enter after which node..."; cin>>after; L1.AddAfter(after-1,item); L1.SListShow(); L1.Display(); getch(); break; case 4: L1.SListShow();

L1.Display(); getch(); break; case 5: item=L1.SListCount(); cout<<"total nodes in the list:"<<item<<endl; L1.Display(); getch(); break; case 6: cout<<"Enter the node value to be deleted:"; cin>>item; L1.Delete(item); L1.SListShow(); L1.Display(); getch(); break; case 7: L1.Display(); getch(); break; case 8: cout<<Bye; getch(); break; } cout<<"\nPress any key to continue...."; getch(); }while(ch!=8); }

OUTPUT:
1.Add at Begining 2.Add at End 3.Add at After 4.show list 5.count list 6.Delete node 7.Display 8.Quit Enter your choice(1-8):1 Enter the node value:1 1 Element in the linked list 1---> Press any key to continue.... Enter your choice(1-8):1 Enter the node value:2 2 1 Element in the linked list 2--->1---> Press any key to continue.... Enter your choice(1-8):2 Enter the node value:3 2 1 3 Element in the linked list 2--->1--->3---> Press any key to continue.... Enter your choice(1-8):3 Enter the node value:5 Enter after which node2 2 1 5 3 Element in the linked list

2--->1--->5--->3---> Press any key to continue.... Enter your choice(1-8):4 2 1 5 3 Element in the linked list 2--->1--->5--->3---> Press any key to continue.... Enter your choice(1-8):5 total nodes in the list:4 Element in the linked list 2--->1--->5--->3---> Press any key to continue.... Enter your choice(1-8):6 Enter the node value to be deleted:5 2 1 3 Element in the linked list 2--->1--->3---> Press any key to continue... Enter your choice(1-8):7 Element in the linked list 2--->1--->3---> Press any key to continue.... Enter your choice(1-8):8 Bye Press any key to continue....

IMPLEMENTATION OF DOUBLY LINKED LIST


#include<iostream.h> #include<conio.h> #include<process.h> class doubly { struct node { int a; node*blink; node*flink; }*first,*t,*t1,*t2; public: doubly() { int c; first=NULL; cout<<"\t\t\t **DOUBLY LINKED LIST** \n"; cout<<"\n Enter the Element for list(Press -1 to stop)"; cin>>c; while(c!=-1) { t=new node; t->a=c; t->blink=t->flink=NULL; if(first==NULL) { t->flink=NULL; t->blink=NULL; first=t; t1=t; } else { t1->flink=t; t->blink=t1; t->flink=NULL; t1=t; } cin>>c; } disp(); } void disp() { if(first==NULL)

cout<<"\n List is Empty"; t2=first; while(t2!=NULL) { cout<<t2->a<<"\t"; t2=t2->flink; } } void insert() { int c,p; cout<<"\n Enter the Element to be inserted:"; cin>>c; cout<<"\n Enter the Position to be inserted:"; cin>>p; if(first==NULL||p==1) { t=new node; t->a=c; t->flink=first; t->blink=NULL; first=t; } else { t=new node; t->a=c; t2=first; int q=1; while((q+1)!=p && t2->flink!=NULL) { t2=t2->flink; q++; } t->flink=(t2->flink); t->blink=t2; (t2->flink)->blink=t; t2->flink=t; if(t2->flink==NULL) { t->flink=NULL; t2->flink=t; t->blink; t2=t; } } } void del() {

int c; if(first==NULL) { cout<<"\n Nothing to delete"; return; } cout<<"\n Enter the element to be deleted:"; cin>>c; if(first->a==c) { first->flink->blink=NULL; first=first->flink; disp(); return; } else { t2=first->flink; while(t2!=NULL) { if(t2->a==c) { if(t2->flink==NULL) { t2->blink->flink=NULL; } else { t2->blink->flink=t2->flink; t2->flink->blink=t2->blink; } disp(); return; } t2=t2->flink; } cout<<"\n Element is not found \n"; disp(); } } }; void main() { clrscr(); doubly dll; int ch; do

{ cout<<"\n\n 1.Insert 2.Delete 3.Display 4.Exit \n"; cout<<"Enter your choice(1-4):"; cin>>ch; switch(ch) { case 1: dll.insert(); getch(); break; case 2: dll.del(); getch(); break; case 3: dll.disp(); getch(); break; case 4: exit(0); } }while(ch>=1 && ch<=4); }

OUTPUT:
**DOUBLY LINKED LIST**
Enter the Element for list(Press -1 to stop) 10 20 30 -1 10 20 30

1.Insert 2.Delete 3.Display 4.Exit Enter your choice(1-4):1 Enter the Element to be inserted:25 Enter the Position to be inserted:3 Enter your choice(1-4):3 10 20 25 30 Enter your choice(1-4):2 Enter the element to be deleted:25 10 20 30 Enter your choice(1-4):4

BINARY SEARCH TREE

#include<iostream.h> #include<conio.h> const int MAX=10; class array { private: int arr[MAX]; int count; public: array(); void add(int item); void search(int item); }; array::array() { count=0; for(int i=0;i<=MAX;i++) arr[i]=0; } void array::add(int item) { if(count<MAX) { arr[count]=item; count++; } else cout<<"\n Array is full"<<endl; } void array::search(int num) { int mid,lower=0,upper=count-1,flag=1; for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2) { if(arr[mid]==num) { cout<<"\n The number is at position " << mid+1 << " in the array"; flag=0; break; } if(arr[mid]>num) upper=mid-1; else lower=mid+1;

} if(flag) cout<<"\n Elements is not present in the array"; } void main() { array a; int i,j,x; int num; clrscr(); cout<<"\n Enter no. of elements:"; cin>>x; for(i=0;i<x;i++) { cout<<"\n Enter the elements:"; cin>>j; a.add(j); } cout<<"\n Enter number to search:"; cin>>num; a.search(num); getch(); }

OUTPUT:
Enter no. of elements:5 Enter the elements:5 Enter the elements:6 Enter the elements:9 Enter the elements:8 Enter the elements:5 Enter number to search:9 The number is at position 3 in the array

HEAP SORT
#include<iostream.h> #include<conio.h> #include<stdlib.h> int a[20],i,j,n,temp; void sort(int k) { while(k>1) { for(i=2;i<=k;i++) { j=i; do{ if(a[j]>a[j/2]) { temp=a[j]; a[j]=a[j/2]; a[j/2]=temp; j=j/2; } else j=1; } while(j!=1); } cout<<"\n"; for(i=1;i<=k;i++) cout<<" "<<a[i]; cout<<"\n\n"; temp=a[1]; a[1]=a[k]; a[k]=temp; k=k-1; }; } void main() { clrscr(); cout<<"\n\t\t\t ***HEAP SORT***"; cout<<"\n\n Enter the number of elements:"; cin>>n; cout<<"\n\n Enter the elements to be sorted:"; for(i=1;i<=n;i++) cin>>a[i]; cout<<"\n\n The heap after each step: \n"; sort(n);

cout<<"\n\n\n Sorted list:"; for(i=1;i<=n;i++) cout<<" "<<a[i]; getch(); }

OUTPUT:
***HEAP SORT***
Enter the number of elements:7 Enter the elements to be sorted:52 55 63 98 12 10 45 The heap after each step: 98 63 55 52 12 10 45 63 52 55 45 12 10 55 45 52 10 12 52 12 45 10 45 10 12 12 10

Sorted list: 10 12 45 52 55 63 98

QUICK SORT
#include<iostream.h> #include<conio.h> #include<stdlib.h> int n,i,j,k; void print(int x[]) { for(i=0;i<n;i++) cout<<"\t"<<x[i]; } void quick(int x[],int l,int u) { int t; i=l; j=u; k=x[i]; if(l>=u) return; else { while(i<j) { while(x[j]>k) j--; if(i<j) { t=x[i]; x[i]=x[j]; x[j]=t; cout<<"\n"; print(x); getch(); i=i+l; } } } quick(x,l,u-1); quick(x,l+1,u); } void main() { clrscr(); cout<<"\t\t ***QUICK SORT***"; cout<<"\n Enter no of elements:"; cin>>n;

int x[20]; cout<<"\n Enter the elements to be sorted:"; for(i=0;i<n;i++) cin>>x[i]; cout<<"\n Quick after each step \n"; quick(x,0,n-1); cout<<"\n\n Sorted Array are:"; print(x); getch(); }

OUTPUT:
***QUICK SORT***
Enter no of elements:5 Enter the elements to be sorted:89 65 90 85 88 Quick after each step 88 85 65 65 65 65 65 85 85 85 90 90 90 88 88 85 88 88 90 89 65 89 89 89 89 90 85 88 89 90

Sorted Array are:

BINARY TREE TRAVERSAL


#include<iostream.h> #include<conio.h> class btree { private: struct btnode { btnode*leftchild; btnode*rightchild; int data; }*root; public: btree(); void buildtree(int num); static void insert(btnode**sr,int num); void traverse(); static void inorder(btnode*sr); static void preorder(btnode*sr); static void postorder(btnode*sr); static void del(btnode*sr); ~btree(); }; btree::btree() { root=NULL; } void btree::buildtree(int num) { insert(&root,num); } void btree::insert(btnode**sr,int num) { if(*sr==NULL) { *sr=new btnode; (*sr)->leftchild=NULL; (*sr)->rightchild=NULL; (*sr)->data=num; return; } else { if(num<(*sr)->data) insert(&((*sr)->leftchild),num); else

insert(&((*sr)->rightchild),num); } return; } void btree::traverse() { cout<<"\n In order traversal:"; inorder(root); cout<<"\n Preorder traversal:"; preorder(root); cout<<"\n Postorder traversal:"; postorder(root); } void btree::inorder(btnode*sr) { if(sr!=NULL) { inorder(sr->leftchild); cout<<"\t"<<sr->data; inorder(sr->rightchild); } else return; } void btree::preorder(btnode*sr) { if(sr!=NULL) { cout<<"\t"<<sr->data; preorder(sr->leftchild); preorder(sr->rightchild); } else return; } void btree::postorder(btnode*sr) { if(sr!=NULL) { postorder(sr->leftchild); postorder(sr->rightchild); cout<<"\t"<<sr->data; } else return; } btree::~btree() { del(root);

} void btree::del(btnode*sr) { if(sr!=NULL) { del(sr->leftchild); del(sr->rightchild); } } void main() { clrscr(); btree bt; int r,num,i=0; cout<<"Specify the no of items to be inserted:"; cin>>r; while(i++<=r) { cout<<"\n Enter the data:"; cin>>num; bt.buildtree(num); } bt.traverse(); getch(); }

OUTPUT:
Specify the no of items to be inserted:5 Enter the data:9 Enter the data:5 Enter the data:4 Enter the data:6 Enter the data:3 In order traversal: 3 Preorder traversal: 9 Postorder traversal: 3 4 5 4 5 4 6 6 3 5 9 6 9

MERGE SORT
#include<iostream.h> #include<conio.h> class array { private: int*arr; int size; int count; public: array(); array(int sz); void add(int num); void display(); static void sort(int *a,int sz); void merge(array &a,array &b); ~array(); }; array::array() { count=size=0; arr=NULL; } array::array(int sz) { count=0; size=sz; arr=new int[sz]; } void array::add(int num) { if(count<size) { arr[count]=num; count++; } else cout<<"\n Array is full"<<endl; } void array::display() { for(int i=0;i<count;i++) cout<<arr[i]<<"\t"; cout<<endl; } void array::merge(array &a,array &b)

{ sort(a.arr,a.size); sort(b.arr,b.size); size=a.count+b.count; arr=new int[size]; int i,j,k; for(i=j=k=0;j<a.count || k<b.count;) { if(a.arr[j]<=b.arr[k]) arr[i++]=a.arr[j++]; else arr[i++]=b.arr[k++]; count++; if(j==a.count || k==b.count) break; } for(;j<a.count;) { arr[i++]=a.arr[j++]; count++; } for(;k<b.count;) { arr[i++]=b.arr[k++]; count++; } } void array::sort(int *a,int sz) { int temp; for(int i=0;i<=sz-2;i++) { for(int j=i+1;j<=sz-1;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } } array::~array() { delete arr; } void main()

{ int i,x,item; clrscr(); cout<<"\t\t\t ***MERGE SORT*** \n"; cout<<"\n Enter the no. of terms:"; cin>>x; array a(x); for(i=0;i<x;i++) { cout<<"\n Enter the elements:"; cin>>item; a.add(item); } cout<<"\n First array:"<<endl; a.display(); cout<<"\n\n Enter the no. of terms:"; cin>>x; array b(x); for(i=0;i<x;i++) { cout<<"\n Enter the elements:"; cin>>item; b.add(item); } cout<<"\n Second array:"<<endl; b.display(); array c; c.merge(a,b); cout<<"\n Array after sorting:"<<endl; c.display(); getch(); }

OUTPUT:
***MERGE SORT***
Enter the no. of terms:5 Enter the elements:4 Enter the elements:0 Enter the elements:3 Enter the elements:1 Enter the elements:2 First array: 4 0 3 1 2

Enter the no. of terms:5 Enter the elements:9 Enter the elements:5 Enter the elements:7 Enter the elements:6 Enter the elements:8 Second array: 9 5 7 6 8

Array after sorting: 0 1 2 3 4 5 6 7 8 9

Вам также может понравиться