Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
54 views

CPP Prog

The document provides code examples for various algorithms including recursion, sorting, searching, and stacks and queues implemented using arrays and linked lists. Specifically, it includes code for factorial recursion, quicksort, stack implementations using arrays and linked lists, queue implementations using arrays and linked lists including a circular queue, sequential, binary and indexed sequential search, and sorting algorithms like merge sort, bubble sort, insertion sort and selection sort. The output of running the code is also shown for some examples.

Uploaded by

Ankit Goel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

CPP Prog

The document provides code examples for various algorithms including recursion, sorting, searching, and stacks and queues implemented using arrays and linked lists. Specifically, it includes code for factorial recursion, quicksort, stack implementations using arrays and linked lists, queue implementations using arrays and linked lists including a circular queue, sequential, binary and indexed sequential search, and sorting algorithms like merge sort, bubble sort, insertion sort and selection sort. The output of running the code is also shown for some examples.

Uploaded by

Ankit Goel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 32

1.

Recursion
Factorial.cpp //Factorial #include<iostream.h> #include<conio.h> int factorial(unsigned int n) { return n==0?1:n*factorial(n-1); } void main() { clrscr(); unsigned int n; cout<<"Enter a number\n"; cin>>n; cout<<"\nThe factorial of "<<n<<" is "<<factorial(n); getch(); } OUTPUT:

Quick_sort.cpp //Quicksort #include<iostream.h> #include<conio.h> int q_sort(int*,int,int); void input(int *it,int n) { cout<<"Enter the elements\n"; for(int i=0;i<n;i++) cin>>it[i]; } void swap(int *items,int &key,int r) { int temp=items[key]; items[key]=items[r]; items[r]=temp; key=r; } void show(int*,int,int); void main() { int items[15],num; cout<<"\nEnter the number of elements:"; cin>>num; input(items,num); q_sort(items,0,num-1); cout<<"Sorted list is:\n"; show(items,0,num-1); getch(); } int q_sort(int items[],int lower,int upper) { int key,l,r,key_placed=0; l=lower; r=upper; key=lower; if(lower>=upper) return 0; while(key_placed==0) {

while(items[key]<=items[r]&&key!=r) r--; if (key==r) key_placed=1; if(items[key]>items[r]) swap(items,key,r); while (items[key]>=items[l]&&l!=key) l++; if (key==l) key_placed=1; if(items[key]<items[l]) swap(items,key,l); } cout<<"\n*key placed is: "<<items[key]<<endl; show(items,lower,upper); cout<<endl; q_sort(items,lower,key-1); q_sort(items,key+1,upper); return 0; } void show(int *items,int lower,int upper) { while(lower<=upper) cout<<" "<<items[lower++]; } OUTPUT:

2. Stacks
Stack_array.cpp //Stack as Array #include<iostream.h> #include<conio.h> #include<process.h> //Array int a[50],top=-1; //Variables int size,i,c,res,item; //Functions int push(int); int pop(); void display(); void sw(int); void main() { clrscr(); while(1) { cout<<"\n\t Stack Menu"; cout<<"\n 1. Insertion"; cout<<"\n 2. Deletion"; cout<<"\n 3. Display"; cout<<"\n 4. Exit"; cout<<"\n Enter your choice\t"; cin>>c; sw(c); } } void sw(int c) { clrscr(); switch(c) { case 1: cout<<"\nEnter the element that you want to insert\t"; cin>>item; res=push(item);

if(res==-1) { cout<<"\nOVERFLOW"; getch(); exit(1); } cout<<"\nStack is now as"; display(); break; case 2: res=pop(); cout<<"\n Now the deletion begins"; if(res==-1) { cout<<"\nUNDERFLOW"; getch(); exit(1); } else { cout<<"\n The element that has been deleted is"; cout<<res; } cout<<"\nThe stack now is as"; display(); break; case 3: cout<<"\n The stack is"; display(); break; case 4: exit(1); break; default: cout<<"\n Enter any valid choice"; getch(); main(); break; } } int push(int ele)

{ if(top==49) { return -1; } else { top=top+1; a[top]=ele; } return 0; } int pop() { int ret; if(top==-1) { return -1; } else { ret=a[top]; top--; } return ret; } void display() { clrscr(); cout<<"\nThe stack now is:\n"; for(int i=top;i>=0;i--) cout<<a[i]<<" "; cout<<endl; getch(); } OUTPUTS:

Stack_Linked.cpp #include<iostream.h> #include<conio.h> #include<process.h> struct node{ int info; node* next; }*top,*save,*ptr,*newptr; node* createnode(int); void push(node*); void pop(); void display(node*); void main() { clrscr(); int inf,c; top=NULL; while(1) { cout<<"\n cout<<"\n cout<<"\n cout<<"\n cin>>c; switch(c) {

Stack Menu"; 1. Insertion"; 2. Deletion"; 3. Display\n 4. Exit\nEnter choice\t";

case 1: cout<<"\nEnter the information:\t"; cin>>inf; newptr=createnode(inf); if (newptr==NULL) { cout<<"\n The element coud not be created"; getch(); main(); } push(newptr); display(top); break;

case 2: pop(); display(top); break; case 3: cout<<"\n The elements are as follows"; display(top); break; case 4: exit(1); break; default: cout<<"\n The valid choices are 1,2,3"; getch(); main(); break; } } } node* createnode(int inf) { ptr=new node; ptr->info=inf; ptr->next=NULL; return ptr; } void push(node* ptr1) { if (top==NULL) { top=ptr1; } else { save=top; top=ptr1; ptr1->next=save; } } void pop() { if(top==NULL)

{ cout<<"\NUnderflow"; } else { ptr=top; top=top->next; delete ptr; } } void display(node* ptr3) { cout<<endl<<"!!!"; while(ptr3!=NULL) { cout<<ptr3->info<<"->"; ptr3=ptr3->next; } getch(); } OUTPUT:

3. Queues
Queue.cpp #include<iostream.h> #include<conio.h> #include<process.h> struct node{ int info; node* next; }*front,*rear,*save,*ptr,*newptr; node* createnode(int); void insert(node*); void remove(); void display(node*); void main() { clrscr(); int inf,c; front=rear=NULL; while(1) { cout<<"\n\tQueue Menu:"; cout<<"\n 1. Insertion"; cout<<"\n 2. Deletion"; cout<<"\n 3.Display\n 4. Exit\n"; cout<<"\n Enter choice\t"; cin>>c; switch(c) { case 1: cout<<"\Enter the information to be inserted into the node:\n"; cin>>inf; newptr=createnode(inf); if (newptr==NULL)

{ cout<<"\n The element coud not be created."; getch(); exit(1); } insert(newptr); display(front); break; case 2: remove(); display(front); break; case 3: cout<<"\n The elements are as follows\n"; display(front); break; case 4: exit(1); break; default: cout<<"\n The valid choices are 1,2,3,4"; getch(); main(); break; } } } node* createnode(int inf) { ptr=new node; ptr->info=inf; ptr->next=NULL; return ptr; } void insert(node* ptr1) { if (front==NULL) { front=rear=ptr1; } else { rear->next=ptr1; rear=ptr1; }

} void remove() { if(front==NULL) { cout<<"\NUnderflow"; } else { ptr=front; front=front->next; delete ptr; } } void display(node* ptr3) { cout<<"\n Queue is\t>>>> "; while(ptr3!=NULL) { cout<<ptr3->info<<"->"; ptr3=ptr3->next; } cout<<" <<<<\n"; } OUTPUT:

Circular_Queue.cpp

4. Searching
Sequential.cpp //Sequential Search #include<iostream.h> #include<conio.h> void input(int *ar,int n) { for(int i=0;i<n;i++) cin>>ar[i]; } int search(int *ar,int n, int item) { for(int i=0;i<n;i++) { if(ar[i]==item) break; } return i==n?-1:i; } void main() { clrscr(); int a[50],int n; cout<<"Enter no. of elements: "; cin>>n; cout<<"\nEnter the elements :"; input(a,n); int item; cout<"\nEnter item to be searched: "; cin>>item; int pos=search(a,n,item); if(pos==-1) cout<<"Search Unsuccessful\n"; else cout<<"\nSearch successful "<<item<<" found at "<<pos+1; getch();

} OUTPUT:

Binary.cpp #include<iostream.h> #include<conio.h> void bsearch(int *a,int n) { int x; int first=0,last=n-1; int middle; int pos=-1; cout<<"\nEnter the element to be searched "; cin>>x; while((first<=last)&&(pos==-1)) { middle=(first+last)/2; if(a[middle]==x) { pos=middle; } else if(a[middle]<x) { first=middle+1; } else { last=middle-1; } } if(pos==-1) { cout<<"\n The search was unsuccessful"; } else { cout<<"\nThe value is stored at position "<<++pos; } } void main() { clrscr();

int a[30],n; cout<<"\nEnter the size of the array: "; cin>>n; cout<<"\nEnter the elements of the array in the ascending order:\n"; for(int i=0;i<n;i++) { cin>>a[i]; } bsearch(a,n); getch(); } OUTPUT:

Indexed_Seq.cpp //Sequential Search #include<iostream.h> #include<conio.h> void input(int *ar,int n) { for(int i=0;i<n;i++) cin>>ar[i]; } void createindex(int *a,int *in,int n,int &i) { int m=0,k=0; for(;m<n;m+=4,k++) //Indexed at every 5th position { in[k]=a[m]; } in[k]=a[n-1]; i=k; } int isearch(int *a,int *in,int i, int item) { int d=0,pos=0; int high=0,low=0; if(item>in[i-1] && item<in[0]) return -1; while(d<i) { if(item==in[d]) { pos=4*i; return pos; } else if(item<in[d]) { low=4*(d-1); high=4*d; } else

{ low=4*d; high=4*(d+1); } d++; } while(low<high) { if(item==a[low]) return low; else low++; } return -1; } void main() { clrscr(); int a[50],n,in[30],i; cout<<"Enter no. of elements: "; cin>>n; cout<<"\nEnter the elements :"; input(a,n); int item; cout<<"\nEnter item to be searched: "; cin>>item; createindex(a,in,n,i); int pos=isearch(a,in,i,item); if(pos==-1) cout<<"\nSearch unsuccessful"; else cout<<"\t"<<item<<" found at position "<<pos+1; getch(); } OUTPUT:

5. Sorting
Merge_Sort.cpp //Merge Sort #include<iostream.h> #include<conio.h> //Array definitions int a[]={5,7,8,34,66,88},la=6; int b[]={1,2,6,9,55,74,82,100},lb=8; int c[14]; void input(int *ar,int n) { for(int i=0;i<n;i++) cin>>ar[i]; } void out(int*ar,int n) { cout<<endl; for(int i=0; i<n; i++) { cout<<"\t"<<ar[i]; } cout<<endl; } void msort(int la,int lb) { int na=0,nb=0,nc=0; while(na<la&&nb<lb) { if(a[na]<b[nb]) c[nc++]=a[na++]; else c[nc++]=b[nb++]; } while(na<la)

c[nc++]=a[na++]; while(nb<lb) c[nc++]=b[nb++]; } void main() { clrscr(); //Arrays a,b are already defined //Sort msort(la,lb); cout<<"Array A is\n"; out(a,la); cout<<"\nArray B is\n"; out(b,lb); cout<<"\nThe sorted array is:\n"; out(c,(la+lb)); getch(); } OUTPUT:

Bubble_Sort.cpp //Bubble Sort #include<iostream.h> #include<conio.h> //Array int ar[100]; void input(int n) { for(int i=0;i<n;i++) cin>>ar[i]; } void out(int n) { cout<<endl; for(int i=0; i<n; i++) { cout<<"\t"<<ar[i]; } cout<<endl; } void swap(int l) { int t; t=ar[l]; ar[l]=ar[l+1]; ar[l+1]=t; } void bsort(int n) { int l,u,i=1; for(u=n-1;u>0;u--) { for(l=0;l<u;l++) { if(ar[l]>ar[l+1]) swap(l); cout<<"\nArray after iteration "<<i++<<" is:"; out(n); }

} } void main() { clrscr(); int ar[100]; int n; cout<<"Enter the size of array:\n"; cin>>n; cout<<"\nEnter the elements:\n"; input(n); //Sort bsort(n); cout<<"\nThe sorted array is:\n"; out(n); getch(); } OUTPUT:

Insertion_Sort.cpp //Insertion Sort #include<iostream.h> #include<conio.h> //Array int ar[100]; void input(int n) { for(int i=0;i<n;i++) cin>>ar[i]; } void out(int n) { cout<<endl; for(int i=0; i<n; i++) { cout<<"\t"<<ar[i]; } cout<<endl; } void insort(int n) { int l,u,i=1,temp; for(u=1;u<n;u++) { l=u; temp=ar[u]; while(l>0 && ar[l-1]>=temp) { ar[l]=ar[l-1]; l--; cout<<"\nArray after iteration "<<i++<<" is:"; out(n); } ar[l]=temp; } } void main()

{ clrscr(); int ar[100]; int n; cout<<"Enter the size of array:\n"; cin>>n; cout<<"\nEnter the elements:\n"; input(n); //Sort insort(n); cout<<"\nThe sorted array is:\n"; out(n); getch(); } OUTPUT:

Selsort.cpp //Selection Sort #include<iostream.h> #include<conio.h> //Array int ar[100]; void input(int n) { for(int i=0;i<n;i++) cin>>ar[i]; } void out(int n) { cout<<endl; for(int i=0; i<n; i++) { cout<<"\t"<<ar[i]; } cout<<endl; } int getmax(int n) { int max=0; for(int i=1;i<n;i++) { if(ar[i]>ar[max]) max=i; } return max; } void selsort(int n) { int temp=0,max,j=1,x=1; for(int i=n;i>=1;i--) { max=getmax(i); temp=ar[max]; ar[max]=ar[i-1];

ar[i-1]=temp; cout<<"\nArray after iteration "<<x++<<" is\n"; out(ar,n); } } void main() { clrscr(); int n; cout<<"Enter the size of array:\n"; cin>>n; cout<<"\nEnter the elements:\n"; input(n); //Sort selsort(n); cout<<"\nThe sorted array is:\n"; out(n); getch(); } OUTPUT:

6. Binary trees
binarytree.cpp //Binary Tree #include<iostream.h> #include<conio.h> #include<process.h> struct tree { int info; tree *left; tree *right; }*root,*leaf; tree *create(int n) { leaf=new tree; leaf->info=n; leaf->left=NULL; leaf->right=NULL; return leaf; } void insert(int n,tree *leaf) { if(root==NULL) root=create(n); else if(n<leaf->info) { if(leaf->left!=NULL) insert(n,leaf->left); else leaf->left=create(n); } else if(n>leaf->info) { if(leaf->right!=NULL) insert(n,leaf->right);

else leaf->right=create(n); } } void disp(tree *p) { if(p!=NULL) { disp(p->left); if(p!=root) cout<<" "<<p->info; else cout<<" --> "<<p->info; disp(p->right); } } tree* search(tree *leaf,int n) { if(leaf) { if(n==leaf->info) return leaf; else if(n<leaf->info) return search(leaf->left,n); else return search(leaf->right,n); } else return NULL; } void del(tree *leaf) { if(leaf) { del(leaf->left); del(leaf->right); delete leaf; } } void main() { clrscr(); int s,n,info;

while(1) { cout<<"\n\tMenu\n1. Insert\t2. Search\t3. Display\t5. Delete\t4. Exit\n"; cin>>n; switch(n) { case 1: cout<<"\nInsert info: "; cin>>info; insert(info,root); break; case 2: cout<<"\nEnter Item to be searched: ";cin>>s; if(!search(root,s)) cout<<"\nSEARCH UNSUCCESSfUL\n"; else cout<<endl<<search(root,s)->info<<" Found."; break; case 3: disp(root); getch(); break; case 4: exit(1); break; case 5: del(root); break; default: cout<<"\n Invalid choice."; getch(); main(); break; } } } OUTPUT:

You might also like