Sie sind auf Seite 1von 46

Practical No: 1

AIM: Implementation of different operation on stack


#include<iostream.h> #include<conio.h> class stack { public: int top=-1,a[ ]; int maxsize=20; void push( ) { int item; if(top=maxsize-1) { cout<<Stack is full; getch(); } else { cout<<Enter the element to be inserted in stack; cin>>item; top=top+1; a[top]=item; } } int pop( ) { int item; if(top=-1) { cout<<Stack is empty; getch(); } else { item=a[top]; top=top-1; } return (item); }

void display( ) { int item cout<<a[item]; } } void main() { stack s; int choice; char ch; do { cout<<1.PUSH OPERATION cout<<2.POP OPERATION cout<<Enter the choice; cin>>choice; switch(choice) { case 1: s.push(); s.display(); break; case 2:cout<<The element is deleted; cout<<s.pop( ) ; s.display(); break; default: cout<<Choice entered is wrong } cout<<Want to continue(Y/N); cin>>ch; } while(ch=Y || ch=y) }

OUTPUT: 1.PUSH OPERATION 2.POP OPERATION Enter the choice 1 Enter the element to be inserted in stack 2 2 Want to continue(Y/N) Y Enter the choice 2 The element is deleted

Practical No: 2
AIM:To convert a decimal no. into its equivalent binary number Source Code
#include<iostream.h> #include<conio.h> class convert { int number; public: void input(); void display(); void dec_bin(); }; void convert::display() { cout<<number<<endl; } void convert::input() { cin>>number; } void convert::dec_bin() { int x,y; x=y=0; for(y=7; y>=0; y--) { x=number/(1<<y); number=number - x * (1<<y); cout<<x; } cout<<endl; } void main() { clrscr(); convert c1; cout<<"Enter the number:"; c1.input(); cout<<"The decimal number entered is "<<endl; c1.display(); cout<<"The binary equivalent is"<<endl;

c1.dec_bin(); getch(); }

OUTPUT
Enter the number:3 The decimal number entered is 3 The binary equivalent is 00000011

Practical No: 3
AIM:To Convert Infix Expression To Postfix Expression Using Stack Source Code
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> char stack[50]; int top=-1; void in_to_post(char infix[]); void push(char); char pop(); void main() { char infix[25]; clrscr(); cout<<"\n Enter the infix expression: "; gets(infix); in_to_post(infix); getch(); } void push(char symb) { if (top>=49) { cout<<"\n Stack Overflow"; getch(); return; } else { top=top+1; stack[top]=symb; } }

char pop() { char item; if (top==-1) { cout<<"\n Stack empty"; getch(); return(0); } else { item=stack[top]; top--; } return (item); } int preced(char ch) { if (ch==47) { return (5); } else if (ch==42) { return (4); } else if (ch==43) { return (3); } else return (2); } void in_to_post(char infix[]) { int length; static int index=0, pos=0; char symbol,temp; char postfix[40]; length=strlen(infix); push('#'); while(index<length) {

symbol=infix[index]; switch(symbol) { case '(': push(symbol); break; case ')': temp=pop(); while(temp!='(') { postfix[pos]=temp; pos++; temp=pop(); } break; case '+': case '-': case '*': case '/': case '^': while(preced(stack[top])>=preced(symbol)) { temp=pop(); postfix[pos]=temp; pos++; } push(symbol); break; default: postfix[pos++]=symbol; break; } index++; } while(top>0) { temp=pop(); postfix[pos++]=temp; } postfix[pos++]='\0'; cout<<"\n The required postfix expression is: "; puts(postfix); return; }

Output
Enter the infix expression: a*(b+c) The required postfix expression is: abc+*

Enter the infix expression: a*(b+c)/(e+f) The required postfix expression is: abc+ef+/*

Enter the infix expression: (a+b)/(c*d)*e The required postfix expression is: ab+cd*/e*

Practical No: 4
AIM:To Implement Various Operation On Singly Linked List At Different Positions. (i) Traversing (ii) Inserting (iii) Deleting
class linklist { private: struct ll { int d; struct ll *n; }*s; struct ll *gn() { struct ll *nn,*p; int n; nn=(struct ll *) malloc(sizeof(struct ll)); y: cout<<"\nEnter No"; cin>>n; if(s==NULL) { nn->d=n; nn->n=NULL; } else { p=s; while(p!=NULL) { if(p->d==n) { cout<<"No Already exists"; goto y; } else { p=p->n; } } nn->d=n;

nn->n=NULL; } return nn; } Public: void an() { struct ll *nn,*p; nn=gn(); if(s==NULL) { s=nn; } else { p=s; while(p->n!=NULL) { p=p->n; } p->n=nn; } } void beg() { struct ll *nn,*p; nn=gn(); nn->n=s; s=nn; } void anap() { struct ll *nn,*p; int se; z: if(s==NULL) { cout<<"List is Empty"; } else { cout<<"Enter No after which you want to insert node"; cin>>se; p=s;

while(p!=NULL) { if(se==p->d) { cout<<"No Found"; break; } else { p=p->n; } } if(p==NULL) { cout<<"No not Found"; goto z; } else { nn=gn(); nn->n=p->n; p->n=nn; } } } void anbp() { struct ll *nn,*p; int se,i,j; a: i=0; if(s==NULL) { cout<<"List is Empty"; } else { cout<<"Enter No before which you want to insert node"; cin>>se; p=s; while(p!=NULL) { i++;

if(se==p->d) { cout<<"No Found"; break; } else { p=p->n; } } if(p==NULL) { cout<<"No not Found"; goto a; } else { nn=gn(); p=s; if(i==1) { nn->n=s; s=nn; } else { for(j=1;j<i-1;j++) { p=p->n; } nn->n=p->n; p->n=nn; } } } } void dl() { struct ll *p,*t; int i=0,j=1; if(s==NULL) { cout<<"List is Empty"; }

else { p=s; while(p!=NULL) { i++; p=p->n; } p=s; if(i==1) { free(p); s=NULL; cout<<"List Deleted"; } else { while(j<i) { t=p; p=p->n; j++; } t->n=NULL; free(p); } } } void df() { struct ll *p,*t; int i=0; if(s==NULL) { cout<<"List is Empty"; } else { p=s; while(p!=NULL) { i++; p=p->n; } p=s; if(i==1)

{ free(p); s=NULL; } else { s=p->n; free(p); } } } void dp() { struct ll *p,*t; int se,i,l,j; pd: i=0; l=0; j=1; if(s==NULL) { cout<<"List is Empty"; } else { cout<<"Enter No you want to delete"; cin>>se; p=s; while(p!=NULL) { i++; p=p->n; } p=s; while(p!=NULL) { l++; if(p->d==se) { cout<<"Number Found"; break; } else { p=p->n;

} } if(p==NULL) { cout<<"No not Found"; goto pd; } else { if(i==1 && l==1) { free(p); s=NULL; } else if(l==1) { t=s; s=t->n; free(t); } else if(i==l) { p=s; while(j<i) { t=p; p=p->n; j++; } t->n=NULL; free(p); } else { p=s; while(j<l) { t=p; p=p->n; j++; } t->n=p->n; free(p); } }

} } void dis() { struct ll *ptr; ptr=s; if(s==NULL) { cout<<"List is Empty"; } else { cout<<"\nList ; while(ptr!=NULL) { cout<<ptr->d; ptr=ptr->n; } } } } void main() { linklist m; int ch; s=NULL; clrscr(); x: cout<<"\n\nEnter \n1 if you want to insert no at the begining"; cout<<"\n2 if you want to insert no at the end of the list"; cout<<"\n3 if you want to insert node after a particular node"; cout<<"\n4 if you want to insert node before a particular node"; cout<<"\n5 if you want to Delete Last Node"; cout<<"\n6 if you want to Delete First Node"); cout<<"\n7 if you want to Delete Particular Node"; cout<<"\n8 if you want to Display the List"; cout<<"\n9 if you want to exit\n"; cin>>ch; switch(ch) { case 1: m.beg(); goto x; case 2: m.an(); goto x;

case 3: m.anap(); goto x; case 4: m.anbp(); goto x; case 5: m.dl(); goto x; case 6: m.df(); goto x; case 7: m.dp(); goto x; case 8: m.dis(); goto x; case 9: exit(0); default: goto x; } }

PRE CONDITION Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 1 POST CONDITION Enter No10 PRE CONDITION Enter 1 if you want to insert no at the begining

2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 2 POST CONDITION Enter No25 PRE CONDITION Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 1 POST CONDITION Enter No5 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 3 Enter No after which you want to insert node10 No Found Enter No15 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list

3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 4 Enter No before which you want to insert node25 No Found Enter No20 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 8 List is 5 10 15 20 25 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 5 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 8 List is 5 10 15 20

Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 6 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 8 List is 10 15 20 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 7 PRE CONDITION Enter No you want to delete15 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node

7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 8 POST CONDITION List is 10 20 Enter 1 if you want to insert no at the begining 2 if you want to insert no at the end of the list 3 if you want to insert node after a particular node 4 if you want to insert node before a particular node 5 if you want to Delete Last Node 6 if you want to Delete First Node 7 if you want to Delete Particular Node 8 if you want to Display the List 9 if you want to exit 9

Practical No: 5
AIM:To accept an integer no. and print using words. Source Code
#include<iostream.h> #include<conio.h> void main() { long int n; int i,a[10]; clrscr(); cout<<"\n Enter the number"; cin>>n; i=0; while(n!=0) { a[i++]=n%10; n=n/10; } cout<<"\n The words representation is: "; while(i>=0) { switch(a[--i]) { case 0:cout<<"Zero "; break; case 1:cout<<"One "; break; case 2:cout<<"Two "; break; case 3:cout<<"Three "; break; case 4:cout<<"Four "; break; case 5:cout<<"Five "; break; case 6:cout<<"Six "; break; case 7:cout<<"Seven "; break; case 8:cout<<"Eight "; break; case 9:cout<<"Nine "; break; } }

getch(); }

OUTPUT
Enter the number: 234412 The words representation is: Two Three Four Four One Two

Enter the number: 9876543 The words representation is: Nine Eight Seven Six Five Four Three

Enter the number: 543210 The words representation is: Five Four Three Two One Zero

Practical No: 6
AIM:To Traverse A Binary Tree Using 1. INORDER 2. PREORDER 3. POST ORDER Source Code
// Tree traversal program(inorder, post ,pre) #include<iostream.h> #include<conio.h> struct NODE { char info; NODE *left_child; NODE *right_child; }; class traverse { public: NODE *Binarytree(char *,int,int); void output(NODE *,int); void preorder(NODE *); void inorder(NODE *); void postorder(NODE *); }; NODE * traverse::Binarytree(char *list,int lower,int upper) { NODE *node; int mid=(lower+upper)/2; node=new(NODE); node->info=list[mid]; if(lower>=upper) { node->left_child=NULL; node->right_child=NULL; return(node); } if(lower<=mid-1) node->left_child=Binarytree(list,lower,mid-1); else node->left_child=NULL; if(mid+1<=upper) node->right_child=Binarytree(list,mid+1,upper);

else node->right_child=NULL; return(node); } void traverse::output(NODE *t,int level) { if(t) { output(t->right_child,level+1); cout<<"\n"; for(int i=0;i<level;i++) cout<<""; cout<<t->info; output(t->left_child,level+1); } } void traverse::preorder(NODE *node) { if(node) { cout<<""<<node->info; preorder(node->left_child); preorder(node->right_child); } } void traverse::inorder(NODE *node) { if(node) { inorder(node->left_child); cout<<""<<node->info; inorder(node->right_child); } } void traverse::postorder(NODE *node) { if(node) { postorder(node->left_child); postorder(node->right_child); cout<<""<<node->info; } } void main() { clrscr();

traverse btree; char list[100]; int number=0; char info; char choice; NODE *t=new(NODE); t=NULL; cout<<"\n input choice 'b' to break:"; choice=getche(); while(choice!='b') { cout<<"\n input inf. of the node:"; cin>>info; list[number++]=info; cout<<"\n input choice 'b' to break:"; choice=getche(); } number--; cout<<"\n Number of elements in the list is"<<number; t=btree.Binarytree(list,0,number); btree.output(t,1); cout<<"\n preorder traversal \n"; btree.preorder(t); cout<<"\n inorder traversal \n" ; btree.inorder(t); cout<<"\n postorder traversal\n"; btree.postorder(t); getch(); }

OUTPUT
Input choice b to break: Input information of the node:A Input choice b to break: Input information of the node:B Input choice b to break: Input information of the node:C Input choice b to break: Input information of the node:D Input choice b to break: Input information of the node:E Input choice b to break: Input information of the node:F Input choice b to break: Input information of the node:G Input choice b to break: Input information of the node:H Input choice b to break: Input information of the node:J Input choice b to break: Input information of the node:K Input choice b to break: Input information of the node:L Input choice b to break: Input information of the node:M Input choice b to break: Input information of the node:O Input choice b to break: b Number of elements in the list is 13 O M L K J H G F E D C B A Preorder Traversal: A,B,D,E,F,C,G,H,J,L,M,K,O

Inorder Traversal: DBFEAGCLJMHOK Postorder Traversal: DFEBGLMJOKHCA

Practical No: 7
AIM:To Sort Array Using Bubble Sort Source Code
#include<iostream.h> #include<conio.h> class sort { int a[10]; public: void inputarr(int num); void display(int num); void bubble(int num); }; void sort::inputarr(int num) { int i; cout<<"Enter the elements in the array"<<endl; for(i=0;i<num;i++) cin>>a[i]; } void sort::display(int num) { int i; for(i=0;i<num;i++) cout<<a[i]<<" "; cout<<endl; } void sort::bubble(int num) { int i,j,temp; for(i=0;i<num;i++) { for(j=i+1;j<num;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } }

} void main() { int num,i; sort s1; cout<<" BUBBLE SORT "<<endl; cout<<"Enter the number of elements"<<endl; cin>>num; s1.inputarr(num); cout<<"The numbers in the unsorted array are"<<endl; s1.display(num); s1.bubble(num); cout<<"Array after bubble sort is applied"<<endl; s1.display(num); getch(); }

OUTPUT
BUBBLE SORT Enter the number of elements6 Enter the elements in the array 67 55 43 21 6 9 The numbers in the unsorted array are 67 55 43 21 6 9 Array after bubble sort is applied 6 9 21 43 55 67

Practical No: 8
AIM:To Sort The Given List Using Quick Sort Source Code
#include<iostream.h> #include<conio.h> class sort { int a[10]; public: void inputarr(int num); void display(int num); int partition(int left, int right); void quick(int left, int right); }; void sort::inputarr(int num) { int i; cout<<"Enter the elements in the array"<<endl; for(i=0;i<num;i++) cin>>a[i]; } void sort::display(int num) { int i; for(i=0;i<num;i++) cout<<a[i]<<" "; cout<<endl; } int sort::partition(int left,int right) { int pivot,high,low,temp; pivot=a[left]; while(right>left) { high=a[right]; while (pivot<high) { if (right<=left) break; right--; high = a[right];

} temp=a[left]; a[left]=high; a[right]=temp; low=a[left]; while(pivot>low) { if(right<=left) break; left++; low=a[left]; } temp=a[right]; a[right]=low; a[left]=temp; } a[left]=pivot; return left; } void sort::quick(int left, int right) { int index; if(left<right) { index=partition(left,right); quick(left,index-1); quick(index+1,right); } } void main() { clrscr(); int num,i,left,right; sort s1; cout<<" QUICKSORT PROGRAM "<<endl; cout<<"Enter the number of elements"<<endl; cin>>num; s1.inputarr(num); cout<<"The numbers in the unsorted array are"<<endl; s1.display(num); left=0; right=num-1; s1.quick(left,right); cout<<"Array after quicksort is applied"<<endl; s1.display(num); getch();

OUTPUT QUICKSORT PROGRAM Enter the number of elements 5 Enter the elements in the array 2 3 1 5 8 The numbers in the unsorted array are 23158 Array after quicksort is applied 12358

Practical No: 9
AIM:To Sort The Given List Using Selectig Sort Source Code
#include<iostream.h> #include<conio.h> class sort { int a[10]; public: void inputarr(int num); void display(int num); void selection(int num); }; void sort::inputarr(int num) { int i; cout<<"Enter the elements in the array"<<endl; for(i=0;i<num;i++) cin>>a[i]; } void sort::display(int num) { int i; for(i=0;i<num;i++) cout<<a[i]<<" "; cout<<endl; } void sort::selection(int num) { int i,j,t,l,m; for(i=0;i<num;i++) { m=a[i]; l=i; for(j=i+1;j<num;j++) { if(m>a[j]) { m=a[j]; l=j; } }

t=a[i]; a[i]=a[l]; a[l]=t; } } void main() { int num,i; sort s1; cout<<" SELECTION SORT "<<endl; cout<<"Enter the number of elements"<<endl; cin>>num; s1.inputarr(num); cout<<"The numbers in the unsorted array are"<<endl; s1.display(num); s1.selection(num); cout<<"Array after selection sort is applied"<<endl; s1.display(num); getch(); }

OUTPUT
SELECTION SORT Enter the number of elements4 Enter the elements in the array 7 83 69 2 The numbers in the unsorted array are 7 83 69 2 Array after selection sort is applied 2 7 69 83

Practical No: 10
AIM:Implementation of Different Operation on Stack Using Link List
#include<iostream.h> #include<stdlib.h> class stack { int element; stack* next; public: stack* push(stack*,int); stack* pop(stack*); void stack_display(stack*); }*head,object; stack* stack::push(stack* head,int key) { stack* temp,*temp1; temp1=head; temp=new stack; temp->element=key; temp->next=NULL; if(head==NULL) head=temp; else { while(head->next!=NULL) head=head->next; head->next=temp; head=temp1; } return head; } stack* stack::pop(stack* head) { stack* temp; if(head!=NULL) { temp=head; if(head->next==NULL) {

cout<<"\nThe pooped element from the stack is:"<<head>element; return NULL; } while(head->next->next!=NULL) head=head->next; cout<<"The popped element from the stack is:"<<head->next->element; head->next=head->next->next; head=temp; return head; } else { cout<<"\nIt is impossible to pop an element from the stack as"; return head; } } void stack::stack_display(stack* head) { if(head!=NULL) { while(head->next!=NULL) { cout<<head->element<<"->"; head=head->next; } cout<<head->element; cout<<endl; } else cout<<"The stack is empty\n"; } void main() { int key,ch; cout<<"\nChoose the operation to be performed:\t1.push\t2.pop\t3.exit\n"; cin>>ch; head=NULL; while(1) { switch(ch) { case 1: cout<<"\nEnter the element to be pushed:";

cin>>key; head=object.push(head,key); cout<<"\nThe stack after push operation is:"; object.stack_display(head); break; case 2: head=object.pop(head); cout<<"\nThe stack after pop operation is:"; object.stack_display(head); break; case 3: exit(1); default: cout<<"\nEnter the correct choice"; break; } cout<<"\nChoose the operation to be performed:\t1.push\t2.pop\t3.exit\n"; cin>>ch; } }

OUTPUT: Choose the operation to be performed: 1.push 2.pop 3.exit 1 Enter the element to be pushed:12 The stack after push operation is:12 Choose the operation to be performed: 1.push 2.pop 3.exit 2 The pooped element from the stack is:12 The stack after pop operation is:The stack is empty Choose the operation to be performed: 1.push 2.pop 3.exit 1 Enter the element to be pushed:12 The stack after push operation is:12 Choose the operation to be performed: 1.push 2.pop 3.exit 1 Enter the element to be pushed:34 The stack after push operation is:12->34 Choose the operation to be performed: 1.push 2.pop 3.exit 1 Enter the element to be pushed:56 The stack after push operation is:12->34->56 Choose the operation to be performed: 1.push 2.pop 3.exit 2 The popped element from the stack is:56 The stack after pop operation is:12->34 Choose the operation to be performed: 1.push 2.pop 3.exit 3

Das könnte Ihnen auch gefallen