Sie sind auf Seite 1von 35

SREE RAMA ENGINEERING COLLEGE

1A/* Write C++ programs for the implementation of STACK USING ARRAY */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #define namespace std; class stack { int stk[5]; int top; public: stack() { top=-1; } void push(int x) { if(top > 4) { cout <<"stack over flow"; return; } stk[++top]=x; cout <<"inserted" <<x; } void pop() { if(top <0) { cout <<"stack under flow"; return; } cout <<"deleted" <<stk[top--]; } void display() { if(top<0) { cout <<" stack empty"; return; } for(int i=top;i>=0;i--) cout <<stk[i] <<" "; } };

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


main() { int ch; stack st; while(1) { cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; st.push(ch); break; case 2: st.pop(); break; case 3: st.display();break; case 4: exit(0); } } return (0); } /* Output 1.push 2.pop 3.display 4.exit Enter ur choice2 stack under flow 1.push 2.pop 3.display 4.exit Enter ur choice1 enter the element25 inserted25 1.push 2.pop 3.display 4.exit Enter ur choice1 enter the element34 inserted34 1.push 2.pop 3.display 4.exit Enter ur choice3 34 25 1.push 2.pop 3.display 4.exit Enter ur choice2 deleted34

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


1.push 2.pop 3.display 4.exit Enter ur choice1 enter the element45 inserted45 1.push 2.pop 3.display 4.exit Enter ur choice3 45 25 1.push 2.pop 3.display 4.exit Enter ur choice 4 */

RESULT: Using Array the Stack program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


1B/* Write C++ programs for the implementation of Stack using linked list */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> template<class t> class llist { struct node { t data; node *link; } *top,*head,*temp,*temp1; public: llist(); void push(); void pop(); void display(); }; template<class t> llist<t>::llist() { top=NULL; head->link=NULL; } template<class t> void llist<t>::push() { t ele; cout<<"enter the element:\n"; cin>>ele; temp=(struct node*)malloc(sizeof(struct node)); if(temp==NULL) { cout<<"memory allocation error\n"; return; } temp->data=ele; if(top==NULL) { head->link=temp; temp->link=NULL; }

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


else { top->link=temp; temp->link=NULL; } top=temp; } template<class t> void llist<t>::pop() { t ele; if(top==NULL) { cout<<"linked list is empty:\n"; return; } for(temp1=head;temp1!=top;temp1=temp1->link) { temp=temp1; } temp->link=NULL; ele=temp1->data; free(temp1); top=temp; cout<<"the deleted element is"<<ele<<endl; } template<class t> void llist<t>::display() { if(top==NULL) { cout<<"linked list is empty:\n"; return; } for(temp=head->link;temp!=top->link;temp=temp->link) { cout<<temp->data<<" "; } } void main() { int ch; llist<int> t; clrscr(); while(1)

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


{ cout<<"\n menu\n"; cout<<"1.push\n"; cout<<"2.pop\n"; cout<<"3.display\n"; cout<<"4.exit\n"; cout<<"enter your choice\n"; cin>>ch; switch(ch) { case 1: t.push(); break; case 2:t.pop(); break; case 3: t.display(); break; case 4: exit(0); break; } } } /*output: 4.exit enter your choice 1 enter the element: 10 menu 1.push 2.pop 3.display 4.exit enter your choice 1 enter the element: 20 menu 1.push 2.pop 3.display 4.exit enter your choice 3

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


10 20 menu 1.push 2.pop 3.display 4.exit enter your choice 2 the deleted element is20 menu 1.push 2.pop 3.display 4.exit enter your choice 3 10 menu 1.push 2.pop 3.display 4.exit enter your choice 4 */ RESULT: Using Linked list the Stack program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


2A/* Write C++ programs for the implementation of QUEUE USING ARRAY */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #define namespace std; class queue { int queue1[5]; int rear,front; public: queue() { rear=-1; front=-1; } void insert(int x) { if(rear > 4) { cout <<"queue over flow"; front=rear=-1; return; } queue1[++rear]=x; cout <<"inserted" <<x; } void delet() { if(front==rear) { cout <<"queue under flow"; return; } cout <<"deleted" <<queue1[++front]; } void display() { if(rear==front) { cout <<" queue empty"; return; }

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


for(int i=front+1;i<=rear;i++) cout <<queue1[i]<<" "; } }; main() { int ch; queue qu; clrscr(); while(1) { cout <<"\n1.insert 2.delet 3.display 4.exit\nEnter ur choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; qu.insert(ch); break; case 2: qu.delet(); break; case 3: qu.display();break; case 4: exit(0); } } return (0); } /* Output 1.insert 2.delet 3.display 4.exit Enter ur choice1 enter the element25 inserted25 1.insert 2.delet 3.display 4.exit Enter ur choice1 enter the element56 inserted56 1.insert 2.delet 3.display 4.exit Enter ur choice3 25 56

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

SREE RAMA ENGINEERING COLLEGE


1.insert 2.delet 3.display 4.exit Enter ur choice2 deleted25 1.insert 2.delet 3.display 4.exit Enter ur choice3 56 1.insert 2.delet 3.display 4.exit Enter ur choice 4*/

RESULT: Using Array the Queue program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

10

SREE RAMA ENGINEERING COLLEGE


2B/* Write C++ programs for the implementation of QUEUE USING LINKED LIST */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> template<class t> class cqueue { struct node { t data; node *link; } *head,*temp,*front,*rear; public: cqueue(); void push(); void pop(); void display(); }; template<class t> cqueue<t>::cqueue() { head->link=NULL; rear=NULL; front=NULL; } template<class t> void cqueue<t>::push() { t ele; cout<<"enter the element:\n"; cin>>ele; temp=(struct node*)malloc(sizeof(struct node)); if(temp==NULL) { cout<<"memory allocation error\n"; return; } if(front==NULL) { temp->data=ele; head->link=temp;

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

11

SREE RAMA ENGINEERING COLLEGE


temp->link=NULL; front=rear=temp; } else { temp->data=ele; rear->link=temp; temp->link=NULL; rear=temp; } } template<class t> void cqueue<t>::pop() { t ele; if(front==NULL) { cout<<"linked list is empty:\n"; return; } if(front==rear) { ele=front->data; front=rear=NULL; } else { ele=front->data; temp=front; front=front->link; free(temp); } cout<<"the deleted element is"<<ele<<endl; } template<class t> void cqueue<t>::display() { if(rear==NULL) { cout<<"linked list is empty:\n"; return; } for(temp=front;temp!=rear->link;temp=temp->link) { cout<<temp->data<<" ";

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

12

SREE RAMA ENGINEERING COLLEGE


} } void main() { int ch; cqueue<int> q; clrscr(); while(1) { cout<<"1.front\n"; cout<<"2.rear\n"; cout<<"3.display\n"; cout<<"4.exit\n"; cout<<"enter your choice\n"; cin>>ch; switch(ch) { case 1: q.push(); break; case 2:q.pop(); break; case 3: q.display(); break; case 4: exit(0); break; } } }

/*output: 1.front 2.rear 3.display 4.exit enter your choice1 enter the element: 56 1.front 2.rear 3.display 4.exit enter your choice 1 enter the element:

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

13

SREE RAMA ENGINEERING COLLEGE


23 1.front 2.rear 3.display 4.exit enter your choice 3 56 23 1.front 2.rear 3.display 4.exit enter your choice 2 the deleted element is56 1.front 2.rear 3.display 4.exit enter your choice 1 enter the element: 21 1.front 2.rear 3.display 4.exit enter your choice 3 23 21 1.front 2.rear 3.display 4.exit enter your choice 4 */ RESULT: Using Linked list the Queue program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

14

SREE RAMA ENGINEERING COLLEGE

3/* Write a C++ program to implement circular queue ADT using an array */

#include<iostream.h> #include<conio.h> #include<stdlib.h> #define namespace std; class cqueue { int q[5],front,rare; public: cqueue() { front=-1; rare=-1; } void push(int x) { if(front ==-1 && rare == -1) { q[++rare]=x; front=rare; return; } else if(front == (rare+1)%5 ) { cout <<" Circular Queue over flow"; return; } rare= (rare+1)%5; q[rare]=x; } void pop() { if(front==-1 && rare== { cout <<"under flow"; return; } else if( front== rare ) { front=rare=-1;

-1)

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

15

SREE RAMA ENGINEERING COLLEGE


return; } front= (front+1)%5; } void display() { int i; if( front <= rare) { for(i=front;i<=rare;i++) cout << q[i]<<" "; } else { for(i=front;i<=4;i++) { cout <<q[i] << " "; } for(i=0;i<=rare;i++) { cout << q[i]<< " "; } } } }; main() { int ch; cqueue q1; while( 1) { cout<<"\n1.INSERT 2.DELETE 3.DISPLAY cin >> ch; switch(ch) { case 1: cout<<"enter element"; cin >> ch; q1.push(ch); break; case 2: q1.pop(); break; case 3: q1.display(); break; case 4: exit(0); } }

4.EXIT\nEnter ur choice";

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

16

SREE RAMA ENGINEERING COLLEGE


} /* Output 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element45 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element67 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element98 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice3 21 23 45 67 98 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element54 Circular Queue over flow 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice3 23 45 67 98 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element34 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice3 23 45 67 98 34 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice1 enter element45 Circular Queue over flow 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

17

SREE RAMA ENGINEERING COLLEGE


Enter ur choice2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice3 67 98 34 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice4*/

RESULT: The Circular Queue program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

18

SREE RAMA ENGINEERING COLLEGE

/* Write C++ programs for the implementation of BST */ #include<stdio.h> #include<stdlib.h> #include<iostream.h> #include<conio.h> struct BST { int data; struct BST *lchild,*rchild; }*root=NULL,*ptr,*ptr1,*n; int item,k=0,s; void insert() { cout<<"enter element to be insert"; cin>>item; ptr=root; if(ptr==NULL) { n=(struct BST*)malloc(sizeof(struct BST)); n->data=item; n->lchild=n->rchild=NULL; cout<<"Element is successfully inserted"; root=n; return; } while(ptr!=NULL){ if(item<ptr->data) { ptr1=ptr; ptr=ptr->lchild; } else if(item>ptr->data) { ptr1=ptr; ptr=ptr->rchild; } else { if(item==ptr->data) {

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

19

SREE RAMA ENGINEERING COLLEGE


cout<<"item already exits"; return; } } } if(ptr==NULL) { n=(struct BST*)malloc(sizeof(struct BST)); n->data=item; n->lchild=n->rchild=NULL; if(ptr1->data<item) ptr1->rchild=n; else ptr1->lchild=n; } } void case1() { if(ptr1->lchild==ptr) ptr1->lchild=NULL; else ptr1->rchild=NULL; cout<<"element is successfully deleted"; ptr=NULL; } void case2() { struct BST*ptr2,*y; int x=0; ptr2=ptr->rchild; if(ptr2->lchild==NULL&&ptr2->rchild==NULL) { ptr1->lchild=ptr2; ptr1->lchild->lchild=ptr->lchild; } else if(ptr2->lchild==NULL&&ptr2->rchild!=NULL) { ptr1->rchild=ptr2; ptr1->rchild->lchild=ptr->lchild; ptr=NULL; free(ptr); } else { while(ptr2->lchild!=NULL)

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

20

SREE RAMA ENGINEERING COLLEGE


ptr2=ptr2->lchild; if(ptr2->rchild!=NULL) { x=1; y=ptr2->rchild; } ptr1->lchild=ptr2; ptr2->lchild=ptr->lchild; ptr2->rchild=ptr->rchild; if(x==1) ptr2->rchild->lchild=y; else ptr2->rchild->lchild=NULL; ptr=NULL; free(ptr); } } void case3() { if(ptr->lchild==ptr) { if(ptr->lchild==NULL) ptr1->lchild=ptr->rchild; else ptr1->lchild=ptr->lchild ; } else { if(ptr1->rchild==ptr) { if(ptr->lchild==NULL) ptr1->rchild=ptr->rchild; else ptr1->rchild=ptr->lchild; } } cout<<"element is successfully deleted"; ptr=NULL; } void delet(int item) { int c=0; struct BST*pt; ptr=root; if(ptr==NULL)

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

21

SREE RAMA ENGINEERING COLLEGE


{ cout<<"tree id empty"; return; } if(ptr->data==item) { cout<<item<<"is A root node deletion is not possible"; return; } if(ptr->data==item&&ptr->lchild==NULL&&ptr->rchild==NULL) { root=NULL; cout<<"item"<<item<<"is successfully deleted"; return; } while((ptr!=NULL)&&(c==0)) { if(item<ptr->data) { ptr1=ptr; ptr=ptr->lchild; } else if(item>ptr->data) { ptr1=ptr; ptr=ptr->rchild; } else c=1; } if(c==0) { cout<<"item does not exist..,deletion is not possible"; return; } if(ptr->lchild==NULL && ptr->rchild==NULL) case1(); else if(ptr->lchild!=NULL && ptr->rchild!=NULL) case2(); else case3(); } void traverse(struct BST *ptr) { if(ptr!=NULL)

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

22

SREE RAMA ENGINEERING COLLEGE


{ traverse(ptr->lchild); cout<<ptr->data<<" "; traverse(ptr->rchild); } } int searching(struct BST *p,int key) { if(p!=NULL) { if(p->data==key) k=1; else { searching(p->lchild,key); searching(p->rchild,key); } } return k; } main() { int ch; clrscr(); while(1) { cout<<"\n binary search tree operations\n 1.insertion\n2.deletion\n"; cout<<"3.traverse\n 4.searching\n5.exit\n"<<"enter your choice:"; cin>>ch; switch(ch) { case 1:insert(); break; case 2:cout<<"which item you want to delet"; cin>>item; delet(item); break; case 3:if(root==NULL) { cout<<"tree is empty"; break; } cout<<"tree elements are"; traverse(root); break;

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

23

SREE RAMA ENGINEERING COLLEGE


case 4:if(root==NULL) { cout<<"tree is empty"; break; } s=k=0; cout<<"which item you want to search"; cin>>item; s=searching(root,item); if(s==1) cout<<"item"<<item<<"is found"; else cout<<"item"<<item<<"is not found"; break; case 5:exit(0); default:cout<<"invalid choice"; } } } /*output: binary search tree operations 1.insertion 2.deletion 3.traverse 4.searching 5.exit enter your choice:2 which item you want to delet24 binary search tree operations 1.insertion 2.deletion 3.traverse 4.searching 5.exit enter your choice:3 tree elements are10 20 21 22 23 25 30 35 40 binary search tree operations 1.insertion 2.deletion 3.traverse 4.searching 5.exit

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

24

SREE RAMA ENGINEERING COLLEGE


enter your choice:3 tree elements are10 20 21 22 23 25 30 35 40 binary search tree operations 1.insertion 2.deletion 3.traverse 4.searching 5.exit enter your choice:2 which item you want to delet20 binary search tree operations 1.insertion 2.deletion 3.traverse 4.searching 5.exit enter your choice:3 tree elements are10 21 25 30 35 40 binary search tree operations 1.insertion 2.deletion 3.traverse 4.searching 5.exit enter your choice:5

*/ RESULT: The above program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

25

SREE RAMA ENGINEERING COLLEGE

5/* Write C++ programs for the implementation of BFS for a given graph */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #define namespace std; int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10]; main() { int m; clrscr(); 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 <<"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; }

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

26

SREE RAMA ENGINEERING COLLEGE


getch(); return 0; }

/* output:

enterno of vertices8 ente no of edges10 EDGES 12 13 24 25 36 37 48 58 68 78 enter initial vertex1 Visitied vertices 12 3 4 5 6 7 8*/ RESULT: The above program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

27

SREE RAMA ENGINEERING COLLEGE

/* Write C++ programs for the implementation of DFS for a given graph */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #define namespace std; int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10]; main() { int m; clrscr(); 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 <<"ORDER OF 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 << " ";

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

28

SREE RAMA ENGINEERING COLLEGE


k++; visit[v]=0; visited[v]=1; } getch(); return 0; } /* output: enterno of vertices8 ente no of edges10 EDGES 12 13 24 25 36 37 48 58 68 78 enter initial vertex1 ORDER OF VISITED VERTICES1 2 4 8 5 3 6 7*/ RESULT: The above program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

29

SREE RAMA ENGINEERING COLLEGE

7/* Write C++ programs for sorting a given list of elements in ascending order using Merge sort the following sorting methods */

#include<iostream.h> #include<conio.h> #define namespace std; void mergesort(int *,int,int); void merge(int *,int,int,int); int a[20],i,n,b[20]; main() { clrscr(); cout <<"\n enter no of elements"; cin >> n; cout <<"enter the elements"; for(i=0;i<n;i++) cin >> a[i]; mergesort(a,0,n-1); cout <<" numbers after sort"; for(i=0;i<n;i++) cout << a[i] << " "; getch(); return 0; } 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) {

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

30

SREE RAMA ENGINEERING COLLEGE


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 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 enter no of elements10 enter the elements 310 285 179 652 351 423 861 254 450 520 285 310 179 285 310 351 652 179 285 310 351 652 423 861 254 423 861 450 520 254 423 450 520 861 179 254 285 310 351 423 450 520 652 861 numbers after sort179 254 285 310 351 423 450 520 652 861*/ RESULT: The above program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

31

SREE RAMA ENGINEERING COLLEGE

8/* Write C++ programs for sorting a given list of elements in ascending order using Quick sort sorting methods */

#include<iostream.h> #include<conio.h> int a[10],l,u,i,j; void quick(int *,int,int); void main() { clrscr(); cout <<"enter 10 elements"; for(i=0;i<10;i++) cin >> a[i]; l=0; u=9; quick(a,l,u); cout <<"\nsorted elements"; 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--;

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

32

SREE RAMA ENGINEERING COLLEGE


if(i<=j) { temp=a[i]; a[i]=a[j]; a[j]=temp;} } temp=a[j]; a[j]=a[l]; a[l]=temp; cout <<"\n"; for(i=0;i<10;i++) cout <<a[i]<<" "; quick(a,l,j-1); quick(a,j+1,u); } } /* Output enter 10 elements 65 70 75 80 85 60 55 50 45 40 60 40 45 50 55 65 85 80 75 70 55 40 45 50 60 65 85 80 75 70 50 40 45 55 60 65 85 80 75 70 45 40 50 55 60 65 85 80 75 70 40 45 50 55 60 65 85 80 75 70 40 45 50 55 60 65 85 80 75 70 40 45 50 55 60 65 85 80 75 70 40 45 50 55 60 65 85 80 75 70 40 45 50 55 60 65 85 80 75 70 40 45 50 55 60 65 70 80 75 85 40 45 50 55 60 65 70 80 75 85 40 45 50 55 60 65 70 75 80 85 sorted elements40 45 50 55 60 65 70 75 80 85*/ RESULT: The above program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

33

SREE RAMA ENGINEERING COLLEGE

9/* Write C++ programs for ALL PAIRS SHORTEST PATH PROBLEM*/ #include<iostream.h> #include<conio.h> #define namespace std; int min(int a,int b); int cost[10][10],a[10][10],i,j,k,c; main() { int n,m; clrscr(); cout <<"enter no of vertices"; cin >> n; cout <<"enter no od edges"; cin >> m; cout<<"enter the\nEDGE Cost\n"; for(k=1;k<=m;k++) { cin>>i>>j>>c; a[i][j]=cost[i][j]=c; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(a[i][j]== 0 && i !=j) a[i][j]=31999; } for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) a[i][j]=min(a[i][j],a[i][k]+a[k][j]); cout <<"Resultant adj matrix\n"; for(i=1;i<=n;i++) {

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

34

SREE RAMA ENGINEERING COLLEGE


for( j=1;j<=n;j++) { if(a[i][j] !=31999) cout << a[i][j] <<" "; } cout <<"\n"; } getch(); return 0; } int min(int a,int b) { if(a<b) return a; else return b; } /* Output enter no of vertices3 enter no od edges5 enter the EDGE Cost 124 1 3 11 216 232 313 Resultant adj matrix 046 502 370 */ RESULT: The above program executed without any error

Advanced Data Structures and Algorithms |


P.LOKANADHA 124C1D5812

35

Das könnte Ihnen auch gefallen