Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ds program-print

476 views

Published on

  • Be the first to comment

  • Be the first to like this

Ds program-print

  1. 1. LINEAR SEARCH#include<iostream.h>#include<conio.h>#include<stdio.h>int search;class linear_s{public: int flag; input(int*,int,int); int n,key,list[200]; void linear_search(int*,int,int); void display(int*,int);};void linear_s::linear_search(int l[],int n,int element){flag=1;for(int k=0;k<n-1;k++){if(l[k]==element){cout<<"nSEARCH IS SUCCESSFULLn"<<"n";cout<<"ELEMENT:t"<<element<<"tFOUND AT LOCATIONt"<<(k+1)<<"n";flag=0;}}if(flag)cout<<"nSEARCH IS UNSUCCESSFULLn";}void linear_s::display(int list[],int n){for(int i=0;i<n;i++){cout<<" "<<list[i];}}linear_s::input(int list[],int number,int key){cout<<"INPUT THE NUMBER OF ELEMENTS IN THE LIST:";cin>>number;cout<<"NUMBER OF ELEMENTS IN THE LIST IS:"<<number<<"n";for(int i=0;i<number;i++){
  2. 2. cout<<"INPUT THE ELEMENTS OF THE LIST:"<<i+1<<":";cin>>list[i];}cout<<"ENTER THE ELEMENT TO BE SEARCHED:";cin>>key;search=key;return number;}void main(){linear_s sort;int number,key,list[200];clrscr();number=sort.input(list,number,key);key=search;cout<<"nENTERED LIST AS FOLLOWS:n";sort.display(list,number);sort.linear_search(list,number,key);cout<<"nIN THE FOLLOWING LIST n";sort.display(list,number);getch();}
  3. 3. OUTPUT: LINEAR SEARCHINPUT THE NUMBER OF ELEMENTS IN THE LIST: 5NUMBER OF ELEMENTS IN THE LIST IS: 5INPUT THE ELEMENTS OF THE LIST:1INPUT THE ELEMENTS OF THE LIST:2INPUT THE ELEMENTS OF THE LIST:3INPUT THE ELEMENTS OF THE LIST:4INPUT THE ELEMENTS OF THE LIST:5ENTER THE ELEMENT TO BE SEARCHED: 4ENTERED LIST AS FOLLOWS:1 2 3 4 5SEARCH IS SUCCESSFULELEMENT 4 FOUND AT LOCATION 4 IN THE FOLLOWING LIST 1 2 3 4 5INPUT THE NUMBER OF ELEMENTS IN THE LIST: 5NUMBER OF ELEMENTS IN THE LIST IS: 5INPUT THE ELEMENTS OF THE LIST:1INPUT THE ELEMENTS OF THE LIST:2INPUT THE ELEMENTS OF THE LIST:3INPUT THE ELEMENTS OF THE LIST:4INPUT THE ELEMENTS OF THE LIST:5ENTER THE ELEMENT TO BE SEARCHED: 14ENTERED LIST AS FOLLOWS:1 2 3 4 5SEARCH IS UNSUCCESSFUL
  4. 4. BINARY SEARCH#include<iostream.h>#include<conio.h>class binary_s{public: void binary_search(int,int,int*); void sort(int*,int); void display(int*,int);};void binary_s::binary_search(int element,int n,int l[]){int flag=1,low,mid,high;low=0,high=n-1;while(low<=high){mid=(low+high)/2;if(element<l[mid])//ELEMENTS IN UPPER HALFhigh=mid-1;else if(element>l[mid])//ELEMENT IN LOWER HALFlow=mid+1;else if(element==l[mid]){cout<<"nSEARCH IS SUCCESSFULLn";cout<<"ELEMENT:t"<<element<<"tFOUND AT LOCATION:t"<<(mid+1);flag=0;break;}}if(flag){cout<<"n SEARCH IS UNSUCCESSFULL";}}void binary_s::sort(int list[],int number){for(int j=0;j<number;j++)for(int k=0;k<number-1;k++){if(list[k]>list[k+1]){int temp=list[k];list[k]=list[k+1];
  5. 5. list[k+1]=temp;}}}void binary_s::display(int list[],int number){cout<<"nENTERED LIST AS FOLLOWS IN ASCENDING ORDERn";for(int i=0;i<number;i++)cout<<" "<<list[i];}void main(){binary_s search;int number,key,list[200];clrscr();cout<<"ENTER NUMBER OF ELEMENTS IN THE LIST:";cin>>number;cout<<"NUMBER OF ELEMENTS IN THE LIST IS :"<<number<<"n";for(int i=0;i<number;i++){cout<<"INPUT THE ELEMENTS OF THE LIST:"<<(i+1)<<":";cin>>list[i];}cout<<"ENTERED LIST IS AS FOLLOWS";for(i=0;i<number;i++)cout<<" "<<list[i];cout<<"nENTER THE ELEMENT TO BE SEARCHED:";cin>>key;search.sort(list,number);search.display(list,number);search.binary_search(key,number,list);getch();}
  6. 6. OUTPUT: BINARY SEARCHINPUT THE NUMBER OF ELEMENTS IN THE LIST: 5NUMBER OF ELEMENTS IN THE LIST IS: 5INPUT THE ELEMENTS OF THE LIST:11INPUT THE ELEMENTS OF THE LIST:2INPUT THE ELEMENTS OF THE LIST:13INPUT THE ELEMENTS OF THE LIST:4INPUT THE ELEMENTS OF THE LIST:15ENTER THE ELEMENT TO BE SEARCHED: 4ENTERED LIST AS FOLLOWS IN ASCENDING ORDER: 2 4 11 13 15SEARCH IS SUCCESSFULINPUT THE NUMBER OF ELEMENTS IN THE LIST: 5NUMBER OF ELEMENTS IN THE LIST IS: 5INPUT THE ELEMENTS OF THE LIST:1INPUT THE ELEMENTS OF THE LIST:2INPUT THE ELEMENTS OF THE LIST:3INPUT THE ELEMENTS OF THE LIST:4INPUT THE ELEMENTS OF THE LIST:5ENTER THE ELEMENT TO BE SEARCHED: 14ENTERED LIST AS FOLLOWS IN ASCENDING ORDER:2 4 11 13 15SEARCH IS UNSUCCESSFUL
  7. 7. Insertion sort#include<iostream.h>#include<stdio.h>#include<conio.h>class insertion{private: int i,k;public: void insert(int*,int); void display(int*,int);};void insertion::insert(int l[],int n){l[0]=-0;for(int i=1;i<=n;i++){int pointer=i-1;int temp=l[i];while(temp<l[pointer]){l[pointer+1]=l[pointer];pointer--;}l[pointer+1]=temp;cout<<"nSTEP="<<i;for(k=1;k<=n;k++)cout<<" "<<l[k];}}void insertion::display(int l[],int n){cout<<"n SORTED LIST IS AS FOLLOWS:";for(i=1;i<=n;i++)cout<<" "<<l[i];}void main(){insertion inssort;int number,list[100];clrscr();cout<<"ENTER NUMBER OF ELEMENTS:";cin>>number;for(int i=1;i<=number;i++)cin>>list[i];
  8. 8. inssort.insert(list,number);inssort.display(list,number);getch();}OUTPUT: INSERTION SORTENTER NUMBER OF ELEMENTS: 511214233SORTED LIST IS AS FOLLOWS:2 3 11 14 23
  9. 9. BUBBLE SORT#include<iostream.h>#include<conio.h>#include<stdio.h>class bubble{public: void bsort(int,int*); void display(int*,int);};void bubble::bsort(int n,int list[]){int flag=1;for(int i=0;i<n-1;i++){for(int j=0;j<n-i-1;j++){if(list[j]>list[j+1]){int temp=list[j];list[j]=list[j+1];list[j+1]=temp;flag=0;}}if(flag) break;else flag=1;}}void bubble::display(int list[],int num){for(int i=0;i<num;i++)cout<<" "<<list[i];}void main(){bubble sort;int n,key,list[100];clrscr();cout<<"ENTER NUMBER OF ELEMENTS:";cin>>n;cout<<"ENTER THE ELEMENTS:";for(int i=0;i<n;i++){cin>>list[i];
  10. 10. cout<<"n";}cout<<"BEFORE SORTING ENTERED LIST IS AS FOLLOWS:n";sort.display(list,n);sort.bsort(n,list);cout<<"nAFTER SORTING THE LIST IS AS FOLLOWS:n";sort.display(list,n);getch();}OUTPUT: BUBBLE SORTENTER NUMBER OF ELEMENTS: 5ENTER THE ELEMENTS:11214233BEFORE SORTING ENTERED LIST IS AS FOLLOWS:11 2 14 23 3AFTER SORTING THE LIST IS AS FOLLOWS:2 3 11 14 23
  11. 11. SELECTION SORT#include<iostream.h>#include<conio.h>#include<stdio.h>class selection{private:int temp,current,j;public: void select(int*,int); void display(int*,int);};void selection::select(int a[],int size){for(current=0;current<size-1;current++)for(j=current+1;j<size;j++)if(a[current]>a[j]){temp=a[current];a[current]=a[j];a[j]=temp;}}void selection::display(int list[],int n){cout<<"SORTED LIST IS AS FOLLOWS:n";for(int i=0;i<n;i++){cout<<" "<<list[i];}}void main(){selection sort;int list[30],number;clrscr();cout<<"ENTER NUMBER OF ELEMENTS:";cin>>number;for(int i=0;i<number;i++){cout<<"ENTER THE VALUES FOR:"<<i+1<<"t";cin>>list[i];}sort.select(list,number);sort.display(list,number);getch();}
  12. 12. OUTPUT: SELECTION SORTENTER NUMBER OF ELEMENTS: 5ENTER THE VALUES FOR: 1 11ENTER THE VALUES FOR: 2 2ENTER THE VALUES FOR: 3 14ENTER THE VALUES FOR: 4 23ENTER THE VALUES FOR: 5 3SORTED LIST IS AS FOLLOWS:2 3 11 14 23
  13. 13. QUICK SORT#include<iostream.h>#include<conio.h>int a[10],l,u,i,j;void quick(int *,int,int);void main(){clrscr();cout <<"ENTER THE VALUES OF 10 ELEMENTS:";for(i=0;i<10;i++)cin >> a[i];l=0;u=9;quick(a,l,u);cout <<"SORTED ELEMENTS ARE:";for(i=0;i<10;i++)cout << a[i] << " ";getch();}void quick(int a[ ],int l,int u){ int p,temp; if(l<u) { p=a[l]; i=l; j=u; while(i<j) { while(a[i] <= p && i<j ) i++; while(a[j]>p && i<=j ) j--; if(i<=j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[j]; a[j]=a[l]; a[l]=temp;
  14. 14. cout <<"n";for(i=0;i<10;i++)cout <<a[i]<<" ";quick(a,l,j-1);quick(a,j+1,u);}}OUTPUT: QUICK SORTENTER THE VALUES OF 10 ELEMENTS1122314358913322SORTED ELEMENTS ARE:1 2 3 5 11 14 22 23 33 89
  15. 15. MERGE SORT#include<stdio.h>#include<conio.h>#include<iostream.h>void mergesort(int *,int,int);void merge(int *,int,int,int);int a[20],i,n,b[20];void main(){ clrscr(); cout<<"nENTER NUMBER OF ELEMENTS:"; cin>>n; cout<<"nENTER THE VALUES OF THE ELEMENTS:"; for(i=0;i<n;i++) cin>>a[i]; mergesort(a,0,n-1); cout<<"nENTERED VALUES AFTER SORT:"; for(i=0;i<n;i++) cout<<a[i]<<" "; getch();}void mergesort(int a[],int i,int j){ int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); mergesort(a,mid+1,j); merge(a,i,mid,j); }}void merge(int a[],int low,int mid,int high){ int h,i,j,k; h=low; i=low; j=mid+1; while(h<=mid&&j<=high) { if(a[h]<=a[j]) b[i]=a[h++]; else
  16. 16. b[i]=a[j++]; i++; } if(h>mid) for(k=j;k<=high;k++) b[i++]=a[k]; else for(k=h;k<=mid;k++) b[i++]=a[k]; cout<<"n"; for(k=low;k<=high;k++) { a[k]=b[k]; cout<<a[k]<<" "; }}OUTPUT: MERGE SORTENTER NUMBER OF ELEMENTS: 5ENTER THE VALUES OF THE ELEMENTS: 11 2 14 23 3ENTERED VALUES AFTER SORT:2 3 11 14 23
  17. 17. SINGLE LINKED LIST#include<iostream.h>#include<conio.h>#include<process.h>class Linked_List{struct node{int info;struct node *link;};struct node *start;public:Linked_List(){start = NULL;}void Create_List(int);void AddAtBeg(int);void AddAfter(int,int);void Delete();void traverse();};void Linked_List::Create_List(int data){struct node *q,*tmp;tmp= (struct node *)new(struct node);tmp->info=data;tmp->link=NULL;if (start==NULL)start=tmp;else{q=start;while(q->link!=NULL)q=q->link;q-> link=tmp;}}void Linked_List::AddAtBeg(int data){struct node *tmp;
  18. 18. tmp=(struct node*)new(struct node);tmp->info=data;tmp->link=start;start=tmp;}void Linked_List::AddAfter(int data,int pos){struct node *tmp,*q;int i;q=start;for(i=0;i<pos-1;i++){q=q->link;if(q==NULL){cout<<"nnThere are less than "<<pos<<" elements";getch();return;}}tmp=(struct node*)new(struct node);tmp->link=q->link;tmp->info=data;q->link=tmp;}void Linked_List::Delete(){struct node *tmp,*q;int data;if(start==NULL){cout<<"nnList is empty";getch();return;}cout<<"nnEnter the element for deletion : ";cin>>data;if(start->info == data){tmp=start;start=start->link;delete(tmp);return;}q=start;
  19. 19. while(q->link->link != NULL){if(q->link->info==data){tmp=q->link;q->link=tmp->link;delete(tmp);return;}q=q->link;}/*End of while */if(q->link->info==data){tmp=q->link;delete(tmp);q->link=NULL;return;}cout<<"nnElement "<<data<<" not found";getch();}void Linked_List::traverse(){struct node *q;if(start == NULL){cout<<"nnList is empty";return;}q=start;cout<<"nnList is : ";while(q!=NULL){cout<<q->info;q=q->link;}cout<<"n";getch();}/*End of display() */void main(){int choice,n,m,position,i;Linked_List po;while(1){
  20. 20. clrscr();cout<<"1.Create Listn";cout<<"2.Add at beginingn";cout<<"3.Add after n";cout<<"4.Deleten";cout<<"5.Traversen";cout<<"6.exitn";cout<<"nEnter your choice:";cin>>choice;switch(choice){case 1:cout<<"nnHow many nodes you want:";cin>>n;for(i=0;i<n;i++){cout<<"nEnter the element:";cin>>m;po.Create_List(m);}break;case 2:cout<<"nnEnter the element:";cin>>m;po.AddAtBeg(m);break;case 3:cout<<"nnEnter the element:";cin>>m;cout<<"nEnter the position after which this element is inserted:";cin>>position;po.AddAfter(m,position);break;case 4:po.Delete();break;case 5:po.traverse();break;case 6:exit(0);default:cout<<"nnWrong choice";}/*End of switch */}/*End of while */
  21. 21. }/*End of main()*/OUTPUT : SINGLE LINKED LIST1.Create List2.Add at begining3.Add after4.Delete5.Traverse6.exitEnter your choice: 1How many nodes you want:5Enter the element: 11Enter the element: 22Enter the element: 33Enter the element: 44Enter the element: 551.Create List2.Add at begining3.Add after4.Delete5.Traverse6.exitEnter your choice: 2Enter the element: 661.Create List2.Add at begining3.Add after4.Delete5.Traverse6.exitEnter your choice: 3Enter the element:77Enter the position after which this element is inserted: 31.Create List2.Add at begining3.Add after4.Delete5.Traverse6.exit
  22. 22. Enter your choice: 4Enter the element for deletion : 441.Create List2.Add at begining3.Add after4.Delete5.Traverse6.exitEnter your choice: 5List is :66 11 22 77 33 55
  23. 23. SORTED LIST#include<iostream.h>#include<conio.h>#include<process.h>class Linked_List{//Structure declaration for the nodestruct node{int info;struct node *link;};//private structure variable declaredstruct node *start;public:Linked_List()//Constructor defined{start = NULL;}//public fucntion declaredvoid Create(int);void Sort();void Disp();};//This function will create a new linked list of elementsvoid Linked_List::Create(int data){struct node *q,*tmp;//New node is created with new operatortmp= (struct node *)new(struct node);tmp->info=data;tmp->link=NULL;if (start==NULL) /*If list is empty */start=tmp;else{ /*Element inserted at the end */q=start;while(q->link!=NULL)q=q->link;q-> link=tmp;}}/*End of create_list()*/
  24. 24. void Linked_List::Display(){struct node *q;if(start == NULL){cout<<"nnList is empty";return;}q=start;cout<<"nnList is : ";while(q!=NULL){cout<<q->info;q=q->link;}cout<<"n";getch();}/*End of display() */void linked_list:: Sort(){ struct node *q,*next;int temp;q=start;next=q->link;while(q!= NULL){ while(next!=NULL) { If(q->info > next->info) { temp= q->info; q->info=next->info; next->info=temp; }}}}
  25. 25. void main(){int n,m;Linked_List po;cout<<"nnEnter size of the list:";cin>>n;for(i=0;i<n;i++){cout<<"nEnter data element:";cin>>m;po.Create(m);}cout<<"nnlist before sorting";po.Disp();cout<<"n nlist after sorting:";po.Sort();po.disp();}/*End of main()*/OUTPUT : SORTED LISTEnter size of the list: 5Enter data element: 23Enter data element: 6Enter data element: 78Enter data element: 3Enter data element: 1list before sorting23 6 78 3 1list after sorting1 3 6 23 78
  26. 26. STACKS USING LINKED LIST#include<conio.h>#include<iostream.h>#include<process.h>//Class is created for the linked listclass Stack_ Linked{//Structure is created for the nodestruct node{int info;struct node *link;//A link to the next node};//A variable top is been declared for the structurestruct node *top;//NODE is defined as the data type of the structure nodetypedef struct node *NODE;public://Constructer is defined for the classStack_ Linked(){//top pointer is initializedtop=NULL;}//function declarationsvoid push();void pop();void display();};//This function is to perform the push operationvoid Stack_ Linked::push(){NODE NewNode;int pushed_ item;//A new node is created dynamicallyNewNode=(NODE)new(struct node);cout<<"nInput the new value to be pushed on the stack:";cin>>pushed_item;NewNode->info=pushed_ item;//Data is pushed to the stackNewNode->link=top;//Link pointer is set to the next nodetop=NewNode;//Top pointer is set}/*End of push()*///Following function will implement the pop operation
  27. 27. void Stack_ Linked::pop(){NODE tmp;if(top == NULL)//checking whether the stack is empty or notcout<<"nStack is emptyn";else{ tmp=top;//popping the elementcout<<"nPopped item is:"<<tmp->info;top=top->link;//resetting the top pointertmp->link=NULL;delete(tmp);//freeing the popped node}}/*End of pop()*///This is to display all the element in the stackvoid Stack_ Linked::display(){if(top==NULL)//Checking whether the stack is empty or notcout<<"nStack is emptyn";else{NODE ptr=top;cout<<"nStack elements:n";while(ptr != NULL){cout<<"n"<<ptr->info;ptr = ptr->link;}/*End of while */}/*End of else*/}/*End of display()*/void main(){char opt;int choice;Stack_ Linked So;do{clrscr();//The menu options are listed belowcout<<"n1.PUSHn";cout<<"2.POPn";cout<<"3.DISPLAYn";cout<<"4.EXITn";cout<<"nEnter your choice : ";cin>>choice;switch(choice)
  28. 28. {case 1:So.push();//push function is calledbreak;case 2:So.pop();//pop function is calledbreak;case 3:So.display();//display function is calledbreak;case 4:exit(1);default:cout<<"nWrong choicen";}/*End of switch */cout<<"nnDo you want to continue (Y/y) = ";cin>>opt;}while((opt == Y) || (opt == y));}/*End of main() */
  29. 29. OUTPUT: STACK USING LINKED LIST1.PUSH2.POP3.DISPLAY4.EXITEnter your choice : 1Input the new value to be pushed on the stack:111.PUSH2.POP3.DISPLAY4.EXITEnter your choice : 1Input the new value to be pushed on the stack:121.PUSH2.POP3.DISPLAY4.EXITEnter your choice : 1Input the new value to be pushed on the stack:131.PUSH2.POP3.DISPLAY4.EXITEnter your choice : 2Popped item is: 131.PUSH2.POP3.DISPLAY4.EXITEnter your choice : 312 111.PUSH2.POP3.DISPLAY4.EXITEnter your choice : 4

×