Ds 2 cycle

346
-1

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
346
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ds 2 cycle

  1. 1. IMPLEMENTATION OF QUEUE USING LINKED LIST#include<iostream.h>#include<process.h>#include<conio.h>class equeue{ struct node { int info; node *next; }*front,*rear;public:equeue(){ front=rear=NULL;} void insert(void); void deleted(void); void display(void);};void equeue::insert(){ node *temp=new node; cout<<"n Enter Value To Insert:"; cin>>temp->info; temp->next=NULL; if(front==NULL) { front=rear=temp; } else { rear->next=temp; rear=rear->next; }}void equeue::deleted(){ if(front==NULL) { cout<<"n Queue Is Emptyn"; } else
  2. 2. { if(front==rear) { cout<<"n Deleted Element Is :"<<front->info; front=rear=NULL; } else { node *temp=front; cout<<"n Deleted Element Is :"<<temp->info; front=front->next; delete(temp); } }}void equeue::display(){ if(front==NULL) cout<<"Queue Is Emptyn"; else { node *t=front; while(t->next!=NULL) { cout<<t->info<<"n"; t=t->next; } cout<<t->info; }}void main(){ equeue c; int ch; clrscr(); while(1) { cout<<"n 1.Insertn 2.Deleten 3.Displayn 4.Exitn"; cout<<"n Enter Your Choice:"; cin>>ch; switch(ch) { case 1:c.insert();
  3. 3. break; case 2:c.deleted(); break; case 3:c.display(); break; case 4:exit(0); break; default:cout<<"n Enter The Right Choice"; } }}OUTPUT:1.Insert2.Delete3.Display4.ExitEnter Your Choice: 1Enter the value to insert: 111.Insert2.Delete3.Display4.ExitEnter Your Choice: 1Enter the value to insert:221.Insert2.Delete3.Display4.ExitEnter Your Choice: 1Enter the value to insert:331.Insert2.Delete3.Display4.ExitEnter Your Choice: 311 22 33
  4. 4. 1.Insert2.Delete3.Display4.ExitEnter Your Choice:2Deleted Element is: 111.Insert2.Delete3.Display4.ExitEnter Your Choice:2Deleted Element is: 221.Insert2.Delete3.Display4.ExitEnter Your Choice:2Deleted Element is: 331.Insert2.Delete3.Display4.ExitEnter Your Choice:3Queue is Empty
  5. 5. IMPLEMENTATION OF DOUBLE LINKED LIST#include <iostream.h>#include<MEMMGR.H>#include<process.h>#include<conio.h>class dllist{ struct node { int value; struct node *next; struct node *prev; }; struct node *start; public: dllist() { start=NULL; } void insertbegin(int value); void insertend(int value); void insertmiddle(int value,int nvalue); void deletebegin(); void deleteend(); void deletemiddle(int value); void display(); };void dllist::insertmiddle(int value,int nvalue) { struct node *temp,*curr,*ncurr; temp=(struct node*)new (struct node); temp->value=value; curr=start; while(curr->value!=nvalue) { curr=curr->next; } temp->prev=curr; temp->next=curr->next; curr->next=temp; }
  6. 6. void dllist::insertbegin(int value) { struct node *temp; temp=(struct node*)new (struct node); temp->value=value; if(start==NULL) { temp->prev=NULL; temp->next=NULL; start=temp; } else { temp->next=start; temp->prev=NULL; start=temp; } }void dllist::insertend(int value) { struct node *temp,*curr; temp=(struct node *)new(struct node); temp->value=value; curr=start; while(curr->next!=NULL) { curr=curr->next; } temp->prev=curr; curr->next=temp; temp->next=NULL; }void dllist::deletebegin() { struct node *curr; curr=start; start=curr->next; start->prev=NULL; cout<<curr->value; free(curr); }void dllist::deleteend()
  7. 7. { struct node *curr=start,*pcurr; while(curr->next!=NULL) { pcurr=curr; curr=curr->next; } pcurr->next=NULL; cout<<curr->value; free(curr);}void dllist::deletemiddle(int value){ struct node *curr=start,*pcurr,*ncurr; pcurr=ncurr=start; while(curr->value!=value) { pcurr=curr; curr=curr->next; } ncurr=curr->next; pcurr->next=curr->next; ncurr->prev=curr->prev; cout<<"deleted element:"<<curr->value; free(curr);}void dllist::display(){ struct node *curr; curr=start; cout<<"nThe list is :n"; if(curr==NULL) cout<<"list is empty"; else { while(curr->next!=NULL) { cout<<curr->value<<"->"; curr=curr->next; } cout<<curr->value; }
  8. 8. }void main(){int ch1,ch2,num,nd;dllist st;clrscr();while(1){ cout<<"n1.Insertn2.Deleten3.Displayn4.Exitn";cin>>ch1;switch(ch1){ case 1: { cout<<"nInsert :n1.Beginningn2.Endn3.Middlen"; cin>>ch2; switch(ch2) { case 1: { cout<<"enter the element:"; cin>>num; st.insertbegin(num); break; } case 2: { cout<<"enter the element:"; cin>>num; st.insertend(num); break; } case 3: { cout<<"enter the node after which to insert:"; cin>>nd; cout<<"enter the element:"; cin>>num; st.insertmiddle(num,nd); break; } default: cout<<"enter the correct choice"; } break;
  9. 9. }case 2: { cout<<"nDelete:n1.Beginningn2.Endn3.Middle"; cin>>ch2; switch(ch2) { case 1: { cout<<"Deletion fron beginning:"; st.deletebegin(); break; } case 2: { cout<<"Deletion from the end:"; st.deleteend(); break; } case 3: { cout<<"enter the node to be deleted:"; cin>>nd; st.deletemiddle(nd); break; } default: cout<<"enter the correct choice"; } break; }case 3: { st.display(); break; }case 4:exit(0);default: cout<<"enter the correct choice";}}}
  10. 10. OUTPUT:1.Insert2.Delete3.Display4.ExitEnter your choice: 1Insert:1.Beginning2.End3.Middle1Enter the element:111.Insert2.Delete3.Display4.ExitEnter your choice:1Insert:1.Beginning2.End3.Middle2Enter the element:221.Insert2.Delete3.Display4.ExitEnter your choice:1Insert:1.Beginning2.End3.Middle3Enter the node after which to insert:11Enter the element:331.Insert2.Delete3.Display4.Exit
  11. 11. Enter your choice:311->33->221.Insert2.Delete3.Display4.ExitEnter your choice:2Delete:1.Beginning2.End3.Middle3Enter the node to be deleted: 33Deleted element :331.Insert2.Delete3.Display4.ExitEnter your choice:2Delete:1.Beginning2.End3.Middle1Deletion from the beginning 11 1.Insert2.Delete3.Display4.ExitEnter your choice:2Delete:1.Beginning2.End3.Middle2Deletion from the end 22
  12. 12. IMPLEMNTATION OF BINARY SEARCH TREE NON RECURSIVE TRAVERSAL#include<iostream.h>#include<conio.h>#include<stdio.h>#include<malloc.h>#include<process.h>struct bstree{ int data; struct bstree*left; struct bstree*right;}*root=NULL;class bst { private: public : void insert(bstree *,int); void inorder_non_rec(bstree*); void preorder_non_rec(bstree*); void postorder_non_rec(bstree*); };class stack { int top; bstree *stackel[20]; public: stack() { top=-1; } void push(bstree *); bstree* pop(); int empty() { if(top==-1) return(1); else return(0); } };
  13. 13. class stack_int { int top; int stack_int[20]; public: stack_int() { top=-1; } void push(int flag); int pop(); int empty_int() { if(top==-1) return(1); else return(0); } };void stack_int::push(int flag) { stack_int[++top]=flag; }int stack_int::pop() { return(stack_int[top--]); }void stack::push(bstree *node) { stackel[++top]=node; }bstree *stack::pop() { return(stackel[top--]); }void bst :: insert(struct bstree*head,int val){ struct bstree *temp1,*temp2; if(head == NULL) {
  14. 14. head=(struct bstree*)malloc(sizeof(struct bstree)); root=head; head->data=val; head->left=NULL; head->right=NULL; } else { temp1=head; while(temp1 != NULL) { temp2=temp1; if(temp1->data > val) temp1=temp1->left; else temp1=temp1->right; } if(temp2->data > val) { temp2->left=(struct bstree*)malloc(sizeof(struct bstree)); temp2=temp2->left; temp2->data=val; temp2->left=NULL; temp2->right=NULL; } else { temp2->right=(struct bstree*)malloc(sizeof(struct bstree)); temp2=temp2->right; temp2->data=val; temp2->left=NULL; temp2->right=NULL; } }}void bst::postorder_non_rec(bstree *root) { stack stk; stack_int stk1; int flag; bstree *temp=root; do { if(temp!=NULL)
  15. 15. { stk.push(temp); stk1.push(1); temp=temp->left; } else { if(stk.empty()) break; temp=stk.pop(); flag=stk1.pop(); if(flag==2) { cout<<temp->data; temp=NULL; } else { stk.push(temp); stk1.push(2); temp=temp->right; } } }while(1); }void bst::preorder_non_rec(bstree *root) { stack stk; bstree *temp=root; stk.push(temp); while(!stk.empty()) { temp=stk.pop(); if(temp!=NULL) { cout<<temp->data<<" "; stk.push(temp->right); stk.push(temp->left); } } }
  16. 16. void bst::inorder_non_rec(bstree *root) { stack stk; bstree *temp; if(root!=NULL) { temp=root; do { while(temp!=NULL) { stk.push(temp); temp=temp->left; } if(!stk.empty()) { temp=stk.pop(); cout<<temp->data<<" "; temp=temp->right; } else break; }while(1); } else cout<<"Empty tree";}void main() { int info,opt; char choice; clrscr(); bst b; do { cout<<"n 1-Insert n 2-Inorder n 3-Preordern 4-Postord n 5. Exitn"; cout<<"n ENTER YOUR CHOICE:"; cin>>opt; switch(opt) { case 1: cout<<"n enter the number :"; cin>>info; b.insert(root,info); break;
  17. 17. case 2: cout<<"n Inorder n non recursive display is:"; b.inorder_non_rec(root); break; case 3: cout<<"nPreorder n non recursive display is:"; b.preorder_non_rec(root); break; case 4: cout<<"n Post order n non recursive display is:"; b.postorder_non_rec(root); break; case 5: exit(0); } }while(1);}OUTPUT 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit Enter your choice: 1 Enter the element: 2 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit Enter your choice: 1 Enter the element: 67 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit Enter your choice: 1 Enter the element: 12 1.Insert 2.Inorder 3.Preorder
  18. 18. 4.Postorder5.ExitEnter your choice: 1Enter the element: 451.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 741.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 211.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 2In orderNon recursive display: 2 12 21 45 67 741.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 3Pre orderNon recursive display: 2 67 12 45 21 741.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 4Post orderNon recursive display: 21 45 12 74 67 2
  19. 19. IMPLEMENTATION OF BINARY SEARCH TREE RECURSIVE TRAVERSAL#include<iostream.h>#include<conio.h>#include<stdio.h>#include<malloc.h>#include<process.h>struct bstree{ int data; struct bstree*left; struct bstree*right;}*root=NULL;class bst { private: public : void insert(bstree *,int); void inorder(bstree*); void postorder(bstree *); void preorder(bstree *); };class stack { int top; bstree *stackel[20]; public: stack() { top=-1; } void push(bstree *); bstree* pop(); int empty() { if(top==-1) return(1); else return(0); } };
  20. 20. class stack_int { int top; int stack_int[20]; public: stack_int() { top=-1; } void push(int flag); int pop(); int empty_int() { if(top==-1) return(1); else return(0); } };void stack_int::push(int flag) { stack_int[++top]=flag; }int stack_int::pop() { return(stack_int[top--]); }void stack::push(bstree *node) { stackel[++top]=node; }bstree *stack::pop() { return(stackel[top--]); }void bst :: insert(struct bstree*head,int val){ struct bstree *temp1,*temp2; if(head == NULL) {
  21. 21. head=(struct bstree*)malloc(sizeof(struct bstree)); root=head; head->data=val; head->left=NULL; head->right=NULL; } else { temp1=head; while(temp1 != NULL) { temp2=temp1; if(temp1->data > val) temp1=temp1->left; else temp1=temp1->right; } if(temp2->data > val) { temp2->left=(struct bstree*)malloc(sizeof(struct bstree)); temp2=temp2->left; temp2->data=val; temp2->left=NULL; temp2->right=NULL; } else { temp2->right=(struct bstree*)malloc(sizeof(struct bstree)); temp2=temp2->right; temp2->data=val; temp2->left=NULL; temp2->right=NULL; } }}void bst :: postorder(struct bstree*head){ if( head != NULL) { postorder(head->left); postorder(head->right); cout<<head->data; }}
  22. 22. void bst :: preorder(struct bstree*head){ if( head != NULL) { cout<<head->data; preorder(head->left); preorder(head->right); }}void bst::inorder(bstree *root) { stack stk; bstree *temp; temp=root; if(temp!=NULL) { inorder(temp->left); cout<<temp->data; inorder(temp->right); }}void main() { int info,opt; char choice; clrscr(); bst b; do { cout<<"n 1-Insert n 2-Inorder n 3-Preordern 4-Postorder n 5. Exitn"; cout<<"n ENTER YOUR CHOICE:"; cin>>opt; switch(opt) { case 1: cout<<"n enter the number :"; cin>>info; b.insert(root,info); break; case 2:cout<<"n recursive display is:"; b.inorder(root); break;
  23. 23. case 3:cout<<"n recursive display is:"; b.preorder(root); break; case 4:cout<<"n recursive display is:"; b.postorder(root); break; case 5: exit(0); } }while(1);}OUTPUT 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit Enter your choice: 1 Enter the element: 2 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit Enter your choice: 1 Enter the element: 67 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit Enter your choice: 1 Enter the element: 12 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit Enter your choice: 1
  24. 24. Enter the element: 451.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 741.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 211.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 2In orderRecursive display: 2 12 21 45 67 741.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 3Pre orderRecursive display: 2 67 12 45 21 741.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 4Post orderRecursive display: 21 45 12 74 67 2
  25. 25. IMPLEMENTATION OF Breadth First Search#include<iostream.h>#include<conio.h>#include<stdlib.h>int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];main(){int m;cout <<"enter no of vertices";cin >> n;cout <<"enter no of edges";cin >> m;cout <<"nEDGES n";for(k=1;k<=m;k++){cin >>i>>j;cost[i][j]=1;}cout <<"enter initial vertex";cin >>v;cout <<"Visitied verticesn";cout << v;visited[v]=1;k=1;while(k<n){for(j=1;j<=n;j++)if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1){visit[j]=1;qu[rare++]=j;}v=qu[front++];cout<<v << " ";k++;visit[v]=0; visited[v]=1;}}
  26. 26. OUTPUT:Enter the no of vertices: 9Enter the no of edges:9EDGES 1 2 2 3 1 5 1 4 4 7 7 8 8 9 2 6 5 7 Enter initial vertex: 1 Visited vertices 1 2 4 5 3 6 7 8 9
  27. 27. IMPLEMENTATION OF Depth First Search#include<iostream.h>#include<conio.h>#include<stdlib.h>int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];main(){int m;cout <<"enterno of vertices";cin >> n;cout <<"ente no of edges";cin >> m;cout <<"nEDGES n";for(k=1;k<=m;k++){cin >>i>>j;cost[i][j]=1;}cout <<"enter initial vertex";cin >>v;cout <<" VISITED VERTICES";cout << v <<" ";visited[v]=1;k=1;while(k<n){for(j=n;j>=1;j--)if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1){visit[j]=1;stk[top]=j;top++;}v=stk[--top];cout<<v << " ";k++;visit[v]=0; visited[v]=1;}}
  28. 28. OUTPUT:Enter the no of vertices: 9Enter the no of edges:9EDGES 1 2 2 3 1 5 1 4 4 7 7 8 8 9 2 6 5 7 Enter initial vertex: 1 Visited vertices 1 2 3 6 4 7 8 9 5
  29. 29. IMPLEMENTATION OF HEAP SORT#include<iostream.h>#include<conio.h>#include<stdio.h>void heapify(int a[],int i);void buildheap(int a[]);void heapsort(int a[]);int n;void main(){int a[100];clrscr();cout<<"enter n valuen";cin>>n;int l=n;cout<<"enter array of elementsn";for(int i=0;i<n;i++)cin>>a[i];heapsort(a);cout<<"the sorted list of elements are:"<<endl;for(i=0;i<l;i++)cout<<a[i]<<endl;getch();}void heapify(int a[],int i){int l,r,large;l=2*i+1;r=2*i+2;if(l<n&&a[l]>=a[i])large=1;elselarge=i;if(r<n&&a[r]>=a[large])large=r;if(i!=large){int t;t=a[i];a[i]=a[large];a[large]=t;heapify(a,large);}}
  30. 30. void buildheap(int a[]){ int i; for(i=(n/2)-1;i>=0;i--) heapify(a,i); }void heapsort(int a[]) { buildheap(a); for(int i=n-1;i>=1;i--) { int t; t=a[0]; a[0]=a[i]; a[i]=t; n--; heapify(a,0); } }OUTPUT:enter n value4enter array of elements5632the sorted list of elements are:2356
  31. 31. IMPLEMENTATION OF PROGRAM TO CONVERT INFIX TO POSTFIX FORM#include<iostream.h>#include<conio.h>#include<string.h>#define MAXSIZE 100class STACK_ARRAY{int stack[MAXSIZE];int Top;public:STACK_ARRAY(){Top=-1;}void push(char);char pop();int prec(char);void Infix_Postfix();};void STACK_ARRAY::push(char item){if (Top == MAXSIZE-1){cout<<"nThe Stack Is Full";getch();}elsestack[++Top]=item;}char STACK_ARRAY::pop(){char item=#;if (Top == -1)cout<<"nThe Stack Is Empty. Invalid Infix expression";elseitem=stack[Top--];return(item);}int STACK_ARRAY::prec(char symbol){switch(symbol){case (:return(1);
  32. 32. case ):return(2);case +:case -:return(3);case *:case /:case %:return(4);case ^:return(5);default:return(0);}}void STACK_ARRAY::Infix_Postfix(){int len,priority;char infix[MAXSIZE],postfix[MAXSIZE],ch;cout<<"nnEnter the infix expression = ";cin>>infix;len=strlen(infix);infix[len++]=);push(();for(int i=0,j=0;i<len;i++){switch(prec(infix[i])){case 1:push(infix[i]);break;case 2:ch=pop();while(ch != (){postfix[j++]=ch;ch=pop();}break;case 3:ch=pop();while(prec(ch) >= 3){postfix[j++]=ch;ch=pop();
  33. 33. }push(ch);push(infix[i]);break;case 4:ch=pop();while(prec(ch) >= 4){postfix[j++]=ch;ch=pop();}push(ch);push(infix[i]);break;case 5:ch=pop();while(prec(ch) == 5){postfix[j++]=ch;ch=pop();}push(ch);push(infix[i]);break;default:postfix[j++]=infix[i];break;}}cout<<"nThe Postfix expression is =";for(i=0;i<j;i++)cout<<postfix[i];}void main(){char choice;STACK_ARRAY ip;clrscr();do{clrscr();ip.Infix_Postfix();cout<<"nnDo you want to continue (Y/y) =";cin>>choice;
  34. 34. }while(choice == Y || choice == y);}OUTPUT:Enter the infix expression:A+BThe postfix expression isAB+Do you wish to continue(Y/y)= yEnter the infix expression:A+(B-C)The postfix expression isABC-+Do you wish to continue(Y/y)=n

×