ds 2 cycle

42
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; } }

Upload: chaitanya-kn

Post on 11-May-2015

313 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Ds 2 cycle

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 Empty\n"; } else

Page 2: Ds 2 cycle

{ 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 Empty\n"; 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.Insert\n 2.Delete\n 3.Display\n 4.Exit\n"; cout<<"\n Enter Your Choice:"; cin>>ch; switch(ch) { case 1:c.insert();

Page 3: Ds 2 cycle

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: 11

1.Insert2.Delete3.Display4.ExitEnter Your Choice: 1Enter the value to insert:22

1.Insert2.Delete3.Display4.ExitEnter Your Choice: 1Enter the value to insert:33

1.Insert2.Delete3.Display4.ExitEnter Your Choice: 311 22 33

Page 4: Ds 2 cycle

1.Insert2.Delete3.Display4.ExitEnter Your Choice:2Deleted Element is: 11

1.Insert2.Delete3.Display4.ExitEnter Your Choice:2Deleted Element is: 22

1.Insert2.Delete3.Display4.ExitEnter Your Choice:2Deleted Element is: 33

1.Insert2.Delete3.Display4.ExitEnter Your Choice:3Queue is Empty

Page 5: Ds 2 cycle

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;}

Page 6: Ds 2 cycle

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()

Page 7: Ds 2 cycle

{ 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;

}

Page 8: Ds 2 cycle

}

void main(){int ch1,ch2,num,nd;dllist st;clrscr();while(1){ cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";cin>>ch1;switch(ch1){ case 1: { cout<<"\nInsert :\n1.Beginning\n2.End\n3.Middle\n";

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;

Page 9: Ds 2 cycle

}case 2: { cout<<"\nDelete:\n1.Beginning\n2.End\n3.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";}

}}

Page 10: Ds 2 cycle

OUTPUT:1.Insert2.Delete3.Display4.ExitEnter your choice: 1

Insert:1.Beginning2.End3.Middle1Enter the element:11

1.Insert2.Delete3.Display4.ExitEnter your choice:1

Insert:1.Beginning2.End3.Middle2Enter the element:22

1.Insert2.Delete3.Display4.ExitEnter your choice:1

Insert:1.Beginning2.End3.Middle3Enter the node after which to insert:11Enter the element:33

1.Insert2.Delete3.Display4.Exit

Page 11: Ds 2 cycle

Enter your choice:311->33->22

1.Insert2.Delete3.Display4.ExitEnter your choice:2

Delete:1.Beginning2.End3.Middle3Enter the node to be deleted: 33Deleted element :33

1.Insert2.Delete3.Display4.ExitEnter your choice:2

Delete:1.Beginning2.End3.Middle1Deletion from the beginning 11

1.Insert2.Delete3.Display4.ExitEnter your choice:2

Delete:1.Beginning2.End3.Middle2Deletion from the end 22

Page 12: Ds 2 cycle

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);}

};

Page 13: Ds 2 cycle

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){

Page 14: Ds 2 cycle

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)

Page 15: Ds 2 cycle

{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); }

} }

Page 16: Ds 2 cycle

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-Preorder\n 4-Postord \n 5. Exit\n"; cout<<"\n ENTER YOUR CHOICE:"; cin>>opt;

switch(opt){

case 1: cout<<"\n enter the number :";cin>>info;b.insert(root,info);break;

Page 17: Ds 2 cycle

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.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 2

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 67

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 12

1.Insert2.Inorder3.Preorder4.Postorder

Page 18: Ds 2 cycle

5.ExitEnter your choice: 1Enter the element: 45

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 74

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 21

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 2In order Non recursive display: 2 12 21 45 67 741.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 3Pre order Non recursive display: 2 67 12 45 21 741.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 4Post order Non recursive display: 21 45 12 74 67 2

IMPLEMENTATION OF BINARY SEARCH TREE RECURSIVE TRAVERSAL

Page 19: Ds 2 cycle

#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);}

};

Page 20: Ds 2 cycle

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){

head=(struct bstree*)malloc(sizeof(struct bstree));

Page 21: Ds 2 cycle

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;

}}

Page 22: Ds 2 cycle

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-Preorder\n 4-Postorder \n 5. Exit\n"; 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;

case 3:cout<<"\n recursive display is:";

Page 23: Ds 2 cycle

b.preorder(root);break;

case 4:cout<<"\n recursive display is:"; b.postorder(root); break;

case 5: exit(0);}

}while(1); }

OUTPUT

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 2

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 67

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 12

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 45

Page 24: Ds 2 cycle

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 74

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 1Enter the element: 21

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 2In order Recursive display: 2 12 21 45 67 74

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 3Pre order Recursive display: 2 67 12 45 21 74

1.Insert2.Inorder3.Preorder4.Postorder5.ExitEnter your choice: 4Post order Recursive display: 21 45 12 74 67 2

IMPLEMENTATION OF Breadth First Search

Page 25: Ds 2 cycle

#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 vertices\n";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;}}

Page 26: Ds 2 cycle

OUTPUT:Enter the no of vertices: 9Enter the no of edges:9EDGES

1 22 31 51 44 77 88 92 65 7Enter initial vertex: 1

Visited vertices1 2 4 5 3 6 7 8 9

IMPLEMENTATION OF Depth First Search

Page 27: Ds 2 cycle

#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;}}

Page 28: Ds 2 cycle

OUTPUT:

Enter the no of vertices: 9Enter the no of edges:9EDGES

1 22 31 51 44 77 88 92 65 7Enter initial vertex: 1

Visited vertices1 2 3 6 4 7 8 9 5

Page 29: Ds 2 cycle

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 value\n";cin>>n;int l=n;cout<<"enter array of elements\n";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);}}

Page 30: Ds 2 cycle

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

Page 31: Ds 2 cycle

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);

Page 32: Ds 2 cycle

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<<"\n\nEnter 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();

Page 33: Ds 2 cycle

}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<<"\n\nDo you want to continue (Y/y) =";cin>>choice;

Page 34: Ds 2 cycle

}while(choice == 'Y' || choice == 'y');}

OUTPUT:

Enter the infix expression:A+BThe postfix expression is AB+Do you wish to continue(Y/y)= y

Enter the infix expression:A+(B-C)The postfix expression isABC-+Do you wish to continue(Y/y)=n