Vous êtes sur la page 1sur 36

GLOBAL

INSTITUTE OF ENGINEERING & TECHNOLOGY


MELVISHARAM, VELLORE 632 509.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

CS2208 CS2208DATA DATASTRUCTURES STRUCTURESLAB LAB

Name Reg No Branch Year

:__________________________ :__________________________ :__________________________ :__________________________

GLOBAL INSTITUTE
OF ENGINEERING AND TECHNOLOGY
(APPROVED BY AICTE & AFFILIATED TO ANNA UNIVERSITY CHENNAI)

Melvisharam, Vellore District 632 509

CERTIFICATE
Name............................... Year..Semester.Branch University Register No: Certified that this is a Bonafide Record of work done by the above student in the ......................Laboratory during the year 20 - 20

Signature of Head of the Department

Signature of Course Lecturer

Submitted for the University practical Examination held on ..... at Global Institute of Engineering and Technology, Melvisharam, Vellore 632509.

Signature of Examiners Internal: . External:

INDEX
EX- NO 1 2 3 DATE NAME OF THE PROGRAM IMPLEMENTATION OF SINGLY LINKED LIST IMPLEMENTATION OF DOUBLYLINKED LIST PROGRAM TO ADD TWO POLYNOMIALS IMPLEMENT STACK AND USE IT TO CONVERT INFIX TO POSTFIX EXPRESSION IMPLEMENT A DOUBLE ENDED QUEUE (DEQUE) IMPLEMENTATION OF AN EXPRESSION TREE PAGE NO STAFF INITIAL

IMPLEMENT BINARY SEARCH TREE

IMPLEMENT INSERTION IN AVL TREES IMPLEMENT PRIORITY QUEUE USING BINARY HEAPS IMPLEMENT HASHING AND OPEN ADDRESSING IMPLEMENT PRIM'S ALGORITHM USING PRIORITY QUEUES TO FIND MST OF AN UNDIRECTED GRAPH.

10

11

IMPLEMENTATION OF SINGLY LINKED LIST PROGRAM:#include<stdio.h> #include<conio.h> #include<stdlib.h> typedef struct Node *PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; int IsEmpty(List L); int IsLast(Position P, List L); Position Find (int X, List L); Position FindPrevious (int X, List L); void Insert (int X,List L, Position P); void Delet (int X, List L); void Display(List L); struct Node { int Element; Position Next; }; int IsEmpty(List L) { return L->Next==NULL; } int IsLast(Position P, List L) { return P->Next==NULL; } Position Find(int X,List L) { Position P; P = L->Next; if(P==NULL || X==0) //Assume header is used return L; while(P!=NULL && P->Element!=X) P = P->Next; return P; } Position FindPrevious(int X,List L) { Position P; P = L;

while(P->Next!=NULL && P->Next->Element!=X) P = P->Next; return P; } void Insert(int X, List L, Position P) { Position TmpCell; TmpCell = malloc(sizeof(struct Node)); TmpCell->Element = X; TmpCell->Next = P->Next; P->Next = TmpCell; } void Delet(int X, List L) { Position P, TmpCell; P = FindPrevious(X,L); if(!IsLast(P,L)) { TmpCell = P->Next; P->Next = TmpCell->Next; free(TmpCell); } } void Display(List L) { Position P; P = L->Next; while(P!=NULL) { printf("\n %d ",P->Element); P = P->Next; } } void main() { int opt,a,X; int ch=1; List L; Position P,TmpCell; clrscr(); L = malloc(sizeof(struct Node)); L->Element = 0; L->Next = NULL;

while(ch==1) { printf("\nLinked List Implementation\n"); printf("\n1.Insert"); printf("\n2.Delete"); printf("\n3.Display"); printf("\n4.Exit"); printf("\nEnter your option: "); scanf("%d",&opt); switch(opt) { case 1: printf("\nEnter after which element you want to insert: "); scanf("%d",&a); printf("\nEnter the element you want to insert: "); scanf("%d",&X); P = Find(a,L); Insert(X,L,P); break; case 2: printf("\nEnter the element which you want to delete: "); scanf("%d",&X); if(IsEmpty(L)) printf("\nList is empty\n"); else { Delet(X,L); printf("\n%d is deleted",X); } break; case 3: if(IsEmpty(L)) printf("\nList is empty\n"); else Display(L); break; case 4: exit(0); } printf("\nDo U want to continue(1/0): "); scanf("%d",&ch); } getch(); }

IMPLEMENTATION OF DOUBLYLINKED LIST PROGRAM:#include<stdio.h> #include<stdlib.h> #include<conio.h> typedef struct Node *PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; int IsEmpty List L); int IsLast(Position P, List L); Position Find(int X, List L); Position FindPrevious(int X, List L); void Insert(int X, List L, Position P); void Delet(int X, List L); void Display(List L); struct Node { int Element; Position Next; Position Prev; }; int IsEmpty(List L) { return L->Next==NULL; } int IsLast(Position P, List L) { return P->Next==NULL; } Position Find(int X, List L) { Position P,Q; P=L->Next; Q=L->Prev; if(P==NULL && Q==NULL || X==0) return L; while(P!=NULL && P->Element!=X) P=P->Next; return P; } Position FindPrevious(int X, List L) { Position P;

P=L; while(P->Next!=NULL && P->Next->Element!=X) P=P->Next; return P; } void Insert(int X, List L, Position P) { PtrToNode Newnode; Newnode=malloc(sizeof(struct Node)); if(Newnode!=NULL) { Newnode->Element=X; Newnode->Next=P->Next; P->Next->Prev=Newnode; P->Next=Newnode; Newnode->Prev=P; } } void Delet(int X, List L) { Position P,Temp; P=FindPrevious(X,L); if(!IsLast(P,L)) { Temp=P->Next; P->Next=Temp->Next; Temp->Next->Prev=P; free(Temp); } else { Temp=P; P->Next=Temp->Next; free(Temp); } } void Display(List L) { Position P; P=L->Next; while(P!=NULL) { printf("%d->",P->Element); P=P->Next; } printf("NULL\n"); }

void main() { int opt, a, X; int ch=1; List L; Position P, Temp; clrscr(); L=malloc(sizeof(struct Node)); L->Element=0; L->Next=NULL; L->Prev=NULL; while(ch==1) { printf("\n Doubly Linked List Implementation\n"); printf("\n1.Insert"); printf("\n2.Delete"); printf("\n3.Display"); printf("\n4.Exit"); printf("\nEnter Your Opotion:"); scanf("%d",&opt); switch(opt) { case 1: printf("\n Enter after which element U want to insert:\n"); scanf("%d",&a); printf("\n Enter the element U want to insert:\n"); scanf("%d",&X); P=Find(a,L); Insert(X,L,P); break; case 2: printf("\n Enter the element which U want to delete:\n"); scanf("%d",&X); if(IsEmpty(L)) printf("\n List is Empty\n"); else { Delet(X,L); printf("\n %d is Deleted",X); } break; case 3: if(IsEmpty(L)) printf("\n List is Empty\n"); else Display(L); break;

case 4: exit(0); } printf("\n Do u want to continue(1/0):"); scanf("%d",&ch); } getch(); }

PROGRAM TO ADD TWO POLYNOMIALS PROGRAM:#include<stdio.h> #include<malloc.h> #include<conio.h> struct link { int coeff; int pow; struct link *next; }; struct link *poly1=NULL,*poly2=NULL,*poly=NULL; void create(struct link *node) { char ch; do { printf("\n enter coeff:"); scanf("%d",&node->coeff); printf("\n enter power:"); scanf("%d",&node->pow); node->next=(struct link *)malloc(sizeof(struct link)); node=node->next; node->next=NULL; printf("\n continue (Y/N):"); ch=getch(); }while(ch=='y' || ch=='Y'); } void show(struct link *node) { while(node->next!=NULL) { printf("%dx^%d", node->coeff,node->pow); node=node->next; if(node->next!=NULL) printf("+"); } } void polyadd(struct link *poly1,struct link *poly2,struct link *poly) { while(poly1->next && poly2->next) { if(poly1->pow>poly2->pow) { poly->pow=poly1->pow; poly->coeff=poly1->coeff;

poly1=poly1->next; } else if(poly1->pow<poly2->pow) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } else { poly->pow=poly1->pow; poly->coeff=poly1->coeff+poly2->coeff; poly1=poly1->next; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } } while(poly1->next || poly2->next) { if(poly1->next) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } if(poly2->next) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } main() { char ch; do { poly1=(struct link *)malloc(sizeof(struct link)); poly2=(struct link *)malloc(sizeof(struct link)); poly=(struct link *)malloc(sizeof(struct link)); printf("\n Enter 1st Number:"); create(poly1); printf("\n Enter 2nd Number:");

create(poly2); printf("\n 1st Number:"); show(poly1); printf("\n 2nd Number:"); show(poly2); polyadd(poly1,poly2,poly); printf("\n Added Polynomial:"); show(poly); printf("\nAdd two more number:"); ch=getch(); } while(ch=='y' || ch=='Y'); }

IMPLEMENT STACK AND USE IT TO CONVERT INFIX TO POSTFIX EXPRESSION PROGRAM:#include<stdio.h> #include<string.h> #include<conio.h> #define operand(x) (x>='a' && x<='z' || x>='A' && x<='Z' || x>='0' && x<='9') char infix[30],postfix[30],stack[30]; int top,i=0; void init() { top=-1; } void push(char x) { stack[++top]=x; } char pop() { return(stack[top--]); } int isp(char x) { int y; y=(x=='('?0:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?6:-1); return y; } int icp(char x) { int y; y=(x=='('?4:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?6:-1); return y; } void infixtopostfix() { int j,l=0; char x,y; stack[++top]='\0'; for (j=0; (x=infix[i++])!='\0'; j--) if (operand(x)) postfix[l++]=x; else if (x==')') while ((y=pop())!='(') postfix[l++]=y; else

{ while (isp(stack[top])>=icp(x)) postfix[l++]=pop(); push(x); } while (top>=0) postfix[l++]=pop(); } int main() { init(); clrscr(); printf("Enter an infix expression :\n"); scanf("%s",infix); infixtopostfix(); printf("The resulting postfix expression is %s",postfix); getch(); return 0; } // End of main

IMPLEMENT A DOUBLE ENDED QUEUE (DEQUE) PROGRAM:#include<stdio.h> #include<conio.h> #include<stdlib.h> #define QSIZE 5 void displayMenu(); int isEmpty(); int isFull(); int enqueueRear(int value); int dequeueRear(int *value); int enqueueFront(int value); int dequeueFront(int *value); int size(); void view(); int queue[QSIZE]; int front=-1,rear=-1; void main() { int status,choice,data; clrscr(); displayMenu(); while(1) { printf("\nEnter Your Choice:"); scanf("%d",&choice); switch(choice) { case 0: displayMenu(); break; case 1: printf("\n Enter the element:"); fflush(stdin); scanf("%d",&data); status=enqueueFront(data); if(status==-1) printf("Deque Overflow on ENQUEUE at Front..."); break; case 2: printf("\n Enter the element:"); fflush(stdin); scanf("%d",&data);

status=enqueueRear(data); if(status==-1) printf("Deque Overflow on ENQUEUE at Rear..."); break; case 3: status=dequeueFront(&data); if(status==-1) printf("Deque Underflow on DEQUEUE at Front..."); else printf("\n The Dequeued value is %d",data); break; case 4: status=dequeueRear(&data); if(status==-1) printf("Deque Underflow on DEQUEUE at Rear..."); else printf("\n The Dequeued value is %d",data); break; case 5: printf("Number of elements in Deque is %d",size()); break; case 6: view(); break; default: printf("\n End of Run of your Program...."); exit(0); } } } void displayMenu() { printf("\n Representation of Linear Deque using arrays..."); printf("\n\t 0.View Menu"); printf("\n\t 1.Enqueue at Front"); printf("\n\t 2.Enqueue at Rear"); printf("\n\t 3.Dequeue at Front"); printf("\n\t 4.Dequeue at Rear"); printf("\n\t 5.Size of the Queue "); printf("\n\t 6.View"); printf("\n\t 7.Exit"); } int isEmpty() { extern int queue[],front,rear; if(front==-1 && rear==-1) /* Check for Empty Queue */ return 1; else

return 0; }

int isFull() { extern int queue[],front,rear; if(rear==(QSIZE-1)) return 1; else return 0; } int enqueueFront(int value) { extern int queue[],front,rear; if(isEmpty()) front=rear=0; else if(isFull()) return -1; else front=front-1; queue[front]=value; return 0; } int enqueueRear(int value) { extern int queue[],front,rear; if(isEmpty()) front=rear=0; else if(isFull()) return -1; else rear=rear+1; queue[rear]=value; return 0; } int dequeueFront(int *value) { extern int queue[],front,rear; if(isEmpty()) return -1; *value=queue[front]; if(front==rear) front=rear=-1; else front=front+1;

return 0; }

int dequeueRear(int *value) { extern int queue[],front,rear; if(isEmpty()) return -1; *value=queue[rear]; if(front==rear) front=rear=-1; else rear=rear-1; return 0; } int size() { extern int queue[],front,rear; if(isEmpty()) return 0; return (rear-front+1); } void view() { extern int queue[],front,rear; int f; if(isEmpty()) { printf("\n Deque is EMPTY!!!"); return; } printf("\n Content of the deque is... \n FRONT->"); for(f=front;f!=rear;f=f+1) printf("%d-->",queue[f]); printf("%d->REAR",queue[f]); if(isFull()) printf("\n Deque is FULL!!!"); }

IMPLEMENTATION OF AN EXPRESSION TREE PROGRAM:#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> typedef struct tree*node; node insert(int, node T); void inorder(node T); void preorder(node T); void postorder(node T); struct tree { int data; struct tree *right,*left; }*root; void main() { node T=NULL; int data,ch,i=0,n; clrscr(); printf("\n enter the number of element in the tree :"); scanf("%d",&n); printf("\n enter the elements :"); while(i<n) { scanf("%d",&data); T=insert(data,T); i++; } printf("\n 1.INORDER \t 2.PREORDER \t 3.POSTORDER \t 4.EXIT"); do { printf("\n Enter your choice :"); scanf("%d",&ch); switch(ch) { case 1:printf("\n Inorder traversal of the given tree"); inorder(T); break; case 2:printf("\n Preorder traversal of the given tree"); preorder(T); break;

case 3:printf("\n Postorder traversal of the given tree"); postorder(T); break; case 4:printf("Exit"); exit(0); } } while(ch<4); getch(); } node insert(int X,node T) { struct tree *newnode; newnode=(struct tree *)malloc(sizeof(struct tree)); if(newnode==NULL) printf("\n out of space \n"); else { if(T==NULL) { newnode->data=X; newnode->left=NULL; newnode->right=NULL; T=newnode; } else { if(X<T->data) T->left=insert(X,T->left); else T->right=insert(X,T->right); } } return T; } void inorder(node T) { if(T!=NULL) { inorder(T->left); printf("%d \t",T->data); inorder(T->right); } } void preorder(node T) { if(T!=NULL) { printf("%d \t",T->data);

preorder(T->left); preorder(T->right); } } void postorder(node T) { if(T!=NULL) { postorder(T->left); postorder(T->right); printf("%d \t",T->data); } }

IMPLEMENT BINARY SEARCH TREE PROGRAM:#include<stdio.h> #include<conio.h> #include<stdlib.h> struct treenode; typedef struct treenode *position; typedef struct treenode *searchtree; struct treenode { int element; searchtree left; searchtree right; }; searchtree makeempty(searchtree t) { if(t!=NULL) { makeempty(t->left); makeempty(t->right); free(t); } return NULL; } position find(int x,searchtree t) { if(t==NULL) return NULL; else if(x<t->element) return find(x,t->left); if(x>t->element) return find(x,t->right); else return t; } position findmin(searchtree t) { if(t==NULL) return NULL; else if(t->left==NULL) return t; else return findmin(t->left); } position findmax(searchtree t) {

if(t!=NULL) while(t->right!=NULL) t=t->right; return t; } searchtree insert(int x,searchtree t) { if(t==NULL) { t=(searchtree)malloc(sizeof(struct treenode)); t->element=x; t->left=NULL; t->right=NULL; } else { if(x<t->element) { t->left=insert(x,t->left); } else if(x>t->element) t->right=insert(x,t->right); } return t; } searchtree del(int x,searchtree t) { position tmp; if(t==NULL) { printf("\n TREE NOT FOUND"); exit(1); } if(x<t->element) t->left=del(x,t->left); else if(x>t->element) t->right=del(x,t->right); else if(t->left && t->right) { tmp=findmin(t->right); t->element=tmp->element; t->right=del(t->element,t->right); } else { tmp=t; if(t->left==NULL) t=t->right; else if(t->right==NULL) t=t->left;

free(tmp); } return t; } void display(searchtree t) { if(t!=NULL) { display(t->left); printf("\t%d",t->element); display(t->right); } } void main() { searchtree t=NULL; int ch,item; clrscr(); while(1) { printf("\n 1.INSERT"); printf("\n 2.DELETE"); printf("\n 3.DISPLAY"); printf("\n 4.EXIT"); printf("\n ENTER THE CHOICE"); scanf("%d",& item); switch(item) { case 1: printf("\n ENTER THE ELEMENT TO BE INSERT"); scanf("\n %d",& item); t=insert(item,t); printf("\n THE INSERTED ELEMENTS is %d",item); break; case 2: printf("\n ENTER THE ELEMENT TO BE DELETE"); scanf("%d",&item); printf("\n THE DELETED ELEMENT is %d",item); t=del(item,t); break; case 3: display(t); break; default: exit(0); } } }

IMPLEMENT INSERTION IN AVL TREES PROGRAM:#include <stdlib.h> #include <stdio.h> #include <conio.h> typedef int ElementType; struct AvlNode; typedef struct AvlNode *Position; typedef struct AvlNode *AvlTree; AvlTree MakeEmpty( AvlTree T ); Position Find( int X, AvlTree T ); Position FindMin( AvlTree T ); Position FindMax( AvlTree T ); AvlTree Insert( int X, AvlTree T ); AvlTree Delete( int X, AvlTree T ); void Display( AvlTree T ); struct AvlNode { int Element; AvlTree Left; AvlTree Right; int Height; }; AvlTree MakeEmpty( AvlTree T ) { if( T != NULL ) { MakeEmpty( T->Left ); MakeEmpty( T->Right ); free( T ); } return NULL; } Position Find( int X, AvlTree T ) { if( T == NULL ) return NULL; if( X < T->Element ) return Find( X, T->Left ); else if( X > T->Element ) return Find( X, T->Right ); else

return T; } Position FindMin( AvlTree T ) { if( T == NULL ) return NULL; else if( T->Left == NULL ) return T; else return FindMin( T->Left ); } Position FindMax( AvlTree T ) { if( T != NULL ) while( T->Right != NULL ) T = T->Right; return T; } static int Height( Position P ) { if( P == NULL ) return -1; else return P->Height; } static int Max( int Lhs, int Rhs ) { return Lhs > Rhs ? Lhs : Rhs; } static Position SingleRotateWithLeft( Position K2 ) { Position K1; K1 = K2->Left; K2->Left = K1->Right; K1->Right = K2; K2->Height = Max( Height( K2->Left ), Height( K2->Right ) ) + 1; K1->Height = Max( Height( K1->Left ), K2->Height ) + 1; return K1; } static Position SingleRotateWithRight( Position K1 ) { Position K2; K2 = K1->Right; K1->Right = K2->Left;

K2->Left = K1; K1->Height = Max( Height( K1->Left ), Height( K1->Right ) ) + 1; K2->Height = Max( Height( K2->Right ), K1->Height ) + 1; return K2; } static Position DoubleRotateWithLeft( Position K3 ) { K3->Left = SingleRotateWithRight( K3->Left ); return SingleRotateWithLeft( K3 ); } static Position DoubleRotateWithRight( Position K1 ) { K1->Right = SingleRotateWithLeft( K1->Right ); return SingleRotateWithRight( K1 ); } AvlTree Insert( ElementType X, AvlTree T ) { if( T == NULL ) { T = malloc( sizeof( struct AvlNode ) ); if( T == NULL ) printf( "Out of space!!!" ); else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } } else if( X < T->Element ) { T->Left = Insert( X, T->Left ); if( Height( T->Left ) - Height( T->Right ) == 2 ) if( X < T->Left->Element ) T = SingleRotateWithLeft( T ); else T = DoubleRotateWithLeft( T ); } else if( X > T->Element ) { T->Right = Insert( X, T->Right ); if( Height( T->Right ) - Height( T->Left ) == 2 ) if( X > T->Right->Element ) T = SingleRotateWithRight( T );

else T = DoubleRotateWithRight( T ); } T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1; return T; } AvlTree Delete( ElementType X, AvlTree T ) { printf( "Sorry; Delete is unimplemented; %d remains\n", X ); return T; } ElementType Retrieve( Position P ) { return P->Element; } void Display(AvlTree T) { if(T!=NULL) { Display(T->Left); printf(" %d",T->Element); Display(T->Right); } } main() { AvlTree T; Position P,s; int i; int ch,n,x,opt; clrscr(); T = MakeEmpty( NULL ); printf("\n Enter the number of elements:"); scanf("%d",&n); for( i = 0; i < n; i++ ) printf("\n***** IMPLEMENTATION OF AVL TREE *****"); printf("\n 1.Insert"); printf("\n 2.FindMin"); printf("\n 3.FindMax"); printf("\n 4.Find"); printf("\n 5.Display"); printf("\n 6.Exit"); do { printf("\nEnter Your choice:"); scanf("%d",&ch); switch(ch) {

case 1: printf("Enter the elemnts to insert:"); scanf("%d",&x); T=Insert(x,T); break; case 2: printf("\n The minimum element in the tree"); s=FindMin(T); if(s==NULL) printf("\n Tree is Empty"); else printf(" %d",s->Element); break; case 3: printf("\n The maximum element in the tree:"); s=FindMax(T); printf(" %d",s->Element); break; case 4: printf("\n Enter the element to find: "); scanf("%d",&x); s=Find(x,T); if(s==NULL) printf("\nGiven Element is not in tree"); else printf("\nElement is found"); break; case 5: if(T==NULL) printf("\n Tree is Empty"); else Display(T); break; case 6: exit(0); break; } printf("\n Do you want to continue(0/1):"); scanf("%d",&opt); }while(ch<6); return 0; }

IMPLEMENT PRIORITY QUEUE USING BINARY HEAPS PROGRAM:#include<stdio.h> #include<conio.h> #include<process.h> #define size 5 int que[size]; int rear,front; Qfull() { if(rear ==size-1) { return 1; } else return 0; } void insert() { int item; void pririty(); printf("\n \n \t Enter the element"); scanf("%d",&item); if(front == -1) front++; que[++rear]=item; //priority(); } void priority() { int i,j,temp; for(i=front;i<=rear-1;i++) for(j=front;j<rear;j++) if(que[j]>que[j+1]) { temp=que[j]; que[j]=que[j+1]; que[j+1]=temp; } } Qempty() { if((front==-1)||(front>rear)) return 1; else return 0; } void delet() {

int item; item=que[front]; printf("\n \t The item deleted is %d",item); front++; } void display() { int i; printf("\n \n The queue is :"); for(i=front;i<=rear;i++) printf("\n %d",que[i]); } void main(void) { int ch; char ans; clrscr(); front=rear=-1; printf("\n \n \t Priority queue \n"); do { printf("\n Main menu \n"); printf("\n 1.Insert \n2.Delete \n3.Display"); printf("\n Enter your choice :"); scanf("%d",&ch); switch(ch) { case 1:if(Qfull()) printf("\n Queue is full"); else insert(); break; case 2:if(Qempty()) printf("\n \n You can't delete the element"); else delet(); break; case 3:if(Qempty()) printf("\n Queue is empty"); else display(); break; default : printf("\n Wrong choice"); break; } printf("\n Do You Want To Continue?"); ans=getche(); }while(ans=='Y' || ans=='y'); getch(); }

IMPLEMENT HASHING AND OPEN ADDRESSING PROGRAM:#include<stdio.h> #include<conio.h> void main() { int a[10]={0,0,0,0,0,0,0,0,0,0}; int n,value,temp,hashvalue; clrscr(); printf("\n Enter value of n(table size):"); scanf("%d",&n); do { printf("\n Enter the hash value"); scanf("%d",&value); hashvalue=value%n; if(a[hashvalue]==0) { a[hashvalue]=value; printf("\na[%d] the value %d is stored",hashvalue,value); } else { for(hashvalue++;hashvalue<n;hashvalue++) { if(a[hashvalue]==0) { printf("\n Space is allocated give other value"); a[hashvalue]=value; printf("\na[%d] the value %d is stored",hashvalue,value); goto ll; } } hashvalue=0; for(hashvalue=0;hashvalue<n;hashvalue++) { if(a[hashvalue]==0) { printf("\n Space is allocated give other value"); a[hashvalue]=value; printf("\na[%d] the value %d is stored",hashvalue,value); goto ll; }} } ll: printf("\n Do u want to enter more(0/1)"); scanf("%d",&temp); }while (temp==1); getch(); }

IMPLEMENT PRIM'S ALGORITHM USING PRIORITY QUEUES TO FIND MST OF AN UNDIRECTED GRAPH. PROGRAM:#include<stdio.h> #define MAX 10 #define TEMP 0 #define PERM 1 #define FALSE 0 #define TRUE 1 #define infinity 9999 struct node { int predecessor; int dist; int status; }; struct edge { int u; int v; }; int adj[MAX][MAX]; int n; main() { int i,j; int path[MAX]; int wt_tree,count; struct edge tree[MAX]; create_graph(); printf("Adjacency matrix is :\n"); display(); count = maketree(tree,&wt_tree); printf("Weight of spanning tree is : %d\n", wt_tree); printf("Edges to be included in spanning tree are : \n"); for(i=1;i<=count;i++) { printf("%d->",tree[i].u); printf("%d\n",tree[i].v); } } create_graph() { int i,max_edges,origin,destin,wt; printf("Enter number of vertices : "); scanf("%d",&n); max_edges=n*(n-1)/2; for(i=1;i<=max_edges;i++)

{ printf("Enter edge %d(0 0 to quit) : ",i); scanf("%d %d",&origin,&destin); if((origin==0) && (destin==0)) break; printf("Enter weight for this edge : "); scanf("%d",&wt); if( origin > n || destin > n || origin<=0 || destin<=0) { printf("Invalid edge!\n"); i--; } else { adj[origin][destin]=wt; adj[destin][origin]=wt; } } if(i==0) { printf("Spanning tree is not possible\n"); exit(1); } } display() { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%3d",adj[i][j]); printf("\n"); } } int maketree(struct edge tree[MAX],int *weight) { struct node state[MAX]; int i,k,min,count,current,newdist; int m; int u1,v1; *weight=0; for(i=1;i<=n;i++) { state[i].predecessor=0; state[i].dist = infinity; state[i].status = TEMP; }

state[1].predecessor=0; state[1].dist = 0; state[1].status = PERM; current=1; count=0; while( all_perm(state) != TRUE ) { for(i=1;i<=n;i++) { if ( adj[current][i] > 0 && state[i].status == TEMP ) { if( adj[current][i] < state[i].dist ) { state[i].predecessor = current; state[i].dist = adj[current][i]; } } } min=infinity; for(i=1;i<=n;i++) { if(state[i].status == TEMP && state[i].dist < min) { min = state[i].dist; current=i; } } state[current].status=PERM; u1=state[current].predecessor; v1=current; count++; tree[count].u=u1; tree[count].v=v1; *weight=*weight+adj[u1][v1]; } return (count); } int all_perm(struct node state[MAX] ) { int i; for(i=1;i<=n;i++) if( state[i].status == TEMP ) return FALSE; return TRUE; }

Vous aimerez peut-être aussi