This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA
q[right]) { int temp; temp=q[loc]; q[loc]=q[right]; q[right]=temp; loc=right; } while(q[left]<=q[loc]&&left!=loc) { ++left; } 23
Data Structures & System Programming if(loc==left) return loc; if(q[left]>q[loc]) { int temp; temp=q[loc]; q[loc]=q[left]; q[left]=temp; loc=left; } } } Output: This is the program of quick sort Enter the number of elements 5 Enter the element in integer 12 33 44 11 16 The sorted list is following 11 12 16 33 44
24
Data Structures & System Programming //14.Program to implement stacks using array #include#include int push(int a[],int top) {if(top==20) cout<<"operation not possible"; else{ cout<<"Enter the element to push"; cin>>a[top]; top=top+1;} return top; } int pop(int top) {if(top==0) cout<<"operation not possible"; else top=top-1; return top;} void main() {int a[20],i,top,c,v=0; char ch; clrscr(); cout<<"Enter the no. of stack elements(>19)"; cin>>top; cout<<"Enter the elements\n"; for(i=0;i >a[i];
DATE:-12/10/09
do{cout<<"What operation do you want to perform\n"; cout<<"1. Push\n2. Pop\n3.Exit\n enter your choice(1-3) "; cin>>c; switch (c) {case 1:top=push(a,top); break; case 2:top=pop(top); break; case 3: v=1; break; 25
Data Structures & System Programming default: cout<<"wrong choice"; break;} if(v==1) break; cout<<"Do you want to perform operation again(n/y)"; cin>>ch; }while(ch=='y'); cout<<"\nThe stack elements are\n"; for(i=0;i
26
Data Structures & System Programming 15.//Stack using linked list DATE:-12/10/09 #include#include struct stack { int info; stack *next; }; class sta { stack *temp,*top,*ptr; public: sta () { top=NULL; } void push(); void pop(); void display(); }; void main() { clrscr(); sta st; int ch; char c='y'; cout<<"This is the programme of stack"<<endl; do { cout<<"The following choice in stack"<<endl; cout<<"1. Push()"<<endl<<"2. Pop"<<endl<<"3. Display"<<endl<<"4. Exit"<<endl; cout<<"Enter the chioce (1-4) :"<<endl; cin>>ch; switch(ch) { case 1:st.push(); getch(); 27
Data Structures & System Programming break; case 2:st.pop(); getch(); break; case 3:st.display(); getch(); break; case 4:c='n'; break; default: cout<<"You enter the wrong choice"<<endl; } }while(c=='y'||c=='Y'); cout<<"thank you"<<endl; getch(); } void sta::push() { temp=new stack; cout<<"Enter the info of the new node "<<endl; cin>>temp->info; temp->next=NULL; if(top==NULL) top=temp; else { temp->next=top; top=temp; } } void sta::pop() { if(top==NULL) cout<<"The stack is underflow"<<endl; else { top=top->next; cout<<"The pop opreation of stack is complete"<<endl; } } 28
Data Structures & System Programming void sta::display() { if(top==NULL) cout<<"The stack is empty"<<endl; else { cout<<"The element in stack is following"<<endl; ptr=top; while(ptr!=NULL) { cout<info<<"\t"; ptr=ptr->next; } cout<<endl; }}
29