Vous êtes sur la page 1sur 58

R.M.D ENGG.

COLLEGE

DEPARTMENT OF CSE

R.M.D ENGINEERING COLLEGE


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS2208 - DATA STRUCTURES LAB MANUAL

PREPARED BY 1. Mrs. D.RAJALAKSHMI. 2. Mr.K.MOHANASUNDARAM

CS 2208 - DATA STRUCTURES LAB MANUAL


1

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB SYLLABUS


AIM: To develop programming skills in design and implementation of data structures and their applications. 1. Implement singly and doubly linked lists. 2. Represent a polynomial as a linked list and write functions for polynomial addition. 3. Implement stack and use it to convert infix to postfix expression 4. Implement a double-ended queue (dequeue) where insertion and deletion operations are possible at both the ends. 5. Implement an expression tree. Produce its pre-order, in-order, and postorder traversals. 6. Implement binary search tree. 7. Implement insertion in AVL trees. 8. Implement priority queue using binary heaps 9. Implement hashing with open addressing. 10. Implement Prim's algorithm using priority queues to find MST of an undirected graph.

List of Equipments and components for A Batch of 30 students (1 per batch) 1. SOFTWARE REQUIRED TURBOC version 3 or GCC version 3.3.4. 2. OPERATING SYSTEM WINDOWS 2000 / XP / NT OR LINUX 3. COMPUTERS REQUIRED 30 Nos. (Minimum Requirement : Pentium III or Pentium IV with 256 RAM and 40 GB harddisk)

CS 2208 - DATA STRUCTURES LAB MANUAL


2

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX NO:1a SINGLY LINKED LIST


AIM:
To write a C program to perform the implementation of Singly Linked List.

ALGORITHM:
1. Define the node structure & read the choice to perform the operations. 2. If the choice is to insert at first, then, a. Create a new node & add data to the data field. b. Assign the address of head to the address field of new node. c. Assign new node to the head. 3. If the choice is to insert at last, then, a. Create a new node & add data to the data field. b. Assign the address of new node to the address field of last node. c. Assign new node to the last. 4. If the choice is to insert at position, p, then, a. b. c. d. Assign some temporary node temp to the head. Create a new node & add data to the data field. Traverse the list till the given position is reached. Let pre contains address of previous node and curr contains address of the next node. Assign address of new node to address field of pre node and address of curr to address field of new node.

5.

If the choice is to delete the first node then, a. Assign some temporary node temp to the first. b. Assign address of the next immediate node to first. c. Delete the temp node.

6.

If the choice is to delete the last node then, a. Traverse the list and assign the address of last node to temp and address of last before node to pre. b. Assign null value to the address field of node pre. c. Assign the address of pre to last. d. Delete the temp node. If the choice is to delete the middle node then, Traverse the list , find the node to be deleted and assign its address to t. Assign the address of next node to temp . Assign the address of temp to address field of previous node pre. Delete the node t.

7. a. b. c. d.

CS 2208 - DATA STRUCTURES LAB MANUAL


3

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

8. If the choice is display, then, a. Display the contents of the list until you encounter the NULL pointer.

PROGRAM:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<malloc.h> struct node { int info; struct node *link; }; struct node *first=NULL,*last=NULL; struct node *n, *temp, *curr, *pre, *t; void main() { int choice,c,p,i=1; clrscr(); do { printf("\n 1. INSERTION OF NODE AT LAST POSITION"); printf("\n 2. INSERTION OF NODE AT FIRST POSITION"); printf("\n 3. INSERTION OF NODE AT MIDDILE POSITION"); printf("\n 4. DELETION OF NODE"); printf("\n 5. DISPLAY OF NODES"); printf("\n 6.EXIT"); printf("\n ENTER YOUR CHOICE: \t"); scanf("%d",&choice); switch(choice) { case 1: n=(node *)malloc(sizeof(struct node)); printf("\n ENTER THE NODE TO BE INSERTED AT LAST POSITION\t"); scanf("%d",&n->info); if(first==NULL) { first=last=n; first->link=NULL; } else { last->link=n; last=n; last->link=NULL; } printf("\n\t\t NODE INSERTED"); break; case 2: n=(node *)malloc(sizeof(struct node)); printf("\n ENTER THE NODE TO BE INSERTED AT FIRST

CS 2208 - DATA STRUCTURES LAB MANUAL


4

R.M.D ENGG. COLLEGE POSITION:"); scanf("%d",&n->info); n->link=first; first=n; break; case 3:

DEPARTMENT OF CSE

n=(node *)malloc(sizeof(struct node)); temp=curr=first; pre=NULL; c=0; printf("\n ENTER THE NODE TO BE INSERTED AT ANY POSITION"); scanf("%D",&n->info); n->link=NULL; while(temp!=NULL) { temp=temp->link; c=c+1; } printf("\n TOTAL NUMBER OF ELEMENTS ARE %d",c); printf("\n ENTER THE POSITION"); scanf("%d",&p); c=1; while((c<p)&&(curr!=NULL)) { pre=curr; curr=curr->link; c=c+1; } if(curr==NULL) printf("\n\t THE ELEMENT CAN'T BE INSERTED\n"); else { if(c==p) { pre->link=n; n->link=curr; printf("\n\t\t NODE INSERTED\n"); } } break; case 4: printf("\n MENU"); printf("\n 1. DELETE THE FIRST NODE"); printf("\n 2. DELETE THE LAST NODE"); printf("\n 3.DELETE THE MIDDLE NODE"); printf("\n ENTER YOUR CHOICE"); scanf("%d",&c); switch(c) { case 1: if(first==NULL) printf("\n\t\t LIST IS EMPTY, CAN'T DELETE");

CS 2208 - DATA STRUCTURES LAB MANUAL


5

R.M.D ENGG. COLLEGE else {

DEPARTMENT OF CSE

temp=first; first=first->link; free(temp); printf("\n\t\t FIRST NODE IS DELETED"); } break; case 2: if(first==NULL) printf("\n\t\tLIST IS EMPTY"); else { temp=first; pre=NULL; if(first==last) { first=last=NULL; free(temp); } while(temp!=last) { pre=temp; temp=temp->link; } pre->link=NULL; last=pre; free(temp); printf("\n\t\t LAST NODE DELETED"); } break; case 3: if(first==NULL) printf("\n\t\t LIST IS EMPTY"); else { temp=first; printf("\n ENTER THE POSITION"); scanf("%d",&p); if(p==1) { first=first->link; free(temp); } else { temp=first; while(i<p) { pre=temp; temp=temp->link; i++; } t=temp; temp=temp->link;

CS 2208 - DATA STRUCTURES LAB MANUAL


6

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE pre->link=temp; free(t); } printf("\n Node is deleted"); break; } } case 5: temp=first; while(temp!=NULL) { printf("%d-->",temp->info); temp=temp->link; } printf("NULL"); break; case 6: exit(0); break;

} }while(choice<6); getch(); }

OUTPUT :
1. INSERTION OF NODE AT LAST POSITION 2. INSERTION OF NODE AT FIRST POSITION 3. INSERTION OF NODE AT MIDDILE POSITION 4. DELETION OF NODE 5. DISPLAY OF NODES 6.EXIT ENTER YOUR CHOICE: 1 ENTER THE NODE TO BE INSERTED AT LAST POSITION 10 NODE INSERTED 1. INSERTION OF NODE AT LAST POSITION 2. INSERTION OF NODE AT FIRST POSITION 3. INSERTION OF NODE AT MIDDILE POSITION 4. DELETION OF NODE 5. DISPLAY OF NODES 6.EXIT ENTER YOUR CHOICE: 2 ENTER THE NODE TO BE INSERTED AT FIRST POSITION:20

CS 2208 - DATA STRUCTURES LAB MANUAL


7

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

1. INSERTION OF NODE AT LAST POSITION 2. INSERTION OF NODE AT FIRST POSITION 3. INSERTION OF NODE AT MIDDILE POSITION 4. DELETION OF NODE 5. DISPLAY OF NODES 6.EXIT ENTER YOUR CHOICE: 5 20-->10-->NULL 1. INSERTION OF NODE AT LAST POSITION 2. INSERTION OF NODE AT FIRST POSITION 3. INSERTION OF NODE AT MIDDILE POSITION 4. DELETION OF NODE 5. DISPLAY OF NODES 6.EXIT ENTER YOUR CHOICE:

RESULT:
Thus the singly linked list is implemented .

CS 2208 - DATA STRUCTURES LAB MANUAL


8

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX No 1b: DOUBLY LINKED LIST


AIM:
To implement doubly linked list for performing insert, delete and search operations.

ALGORITHM:
1. Create a node with data and rlink and llink fields. Assign null value to rlink and llink. 2. If the choice is to insert, then a. Create a new node temp & add data to the data field. Assign some temporary node next to the head. b. Traverse the list till the given position is reached. c. Let next contains address of previous node and temp contains address of the new node. Assign 1.address of next node to llink field of temp node , 2.rlink of next to rlink of temp. 3.address of temp to next->rlink->llink. 4.address of temp to rlink of next. 3. If the choice is to delete, then a. Assign some temporary node next to the head. b. Traverse the list till the given position is reached. c. Let next contains address of previous node of the node to be deleted. Assign rlink of next to temp. rlink of temp to rlink of next rlink of next to prev address of next to llink of prev. 4. If the choice is to display, then a. Display the contents of the list until you encounter the NULL pointer.

PROGRAM :
#include<stdio.h> #include<conio.h> #include<malloc.h> #include<stdlib.h> struct node { int data; struct node *llink,*rlink; }*temp,*next,*prev; typedef struct node node; struct node *start=NULL,*end; void main()

CS 2208 - DATA STRUCTURES LAB MANUAL


9

R.M.D ENGG. COLLEGE { void insert(); void del(); void display(); void create(); int ch; clrscr(); create(); do { printf("\t\t\nMENU\n"); printf("1.INSERT\n"); printf("2.DELETE\n"); printf("3.DISPLAY\n"); printf("4.EXIT\n"); printf("ENTER UR CHOICE:"); scanf("%d",&ch); switch(ch) { case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: exit(0); } }while(ch<=4); getch(); }

DEPARTMENT OF CSE

void create() { int data; temp=(node *)malloc(sizeof(node)); temp->llink=NULL; temp->rlink=NULL; printf("\nENTER THE ELEMENT:"); scanf("%d",&data); temp->data=data; start=temp; } void insert() { int p,data,i; temp=(node *)malloc(sizeof(node)); printf("\nENTER THE POSITION TO BE INSERTED:"); scanf("%d",&p); printf("\nENTER THE ELEMENT:"); scanf("%d",&data); temp->data=data;

CS 2208 - DATA STRUCTURES LAB MANUAL


10

R.M.D ENGG. COLLEGE if(p==1) { temp->llink=NULL; temp->rlink=start; start->llink=temp; start=temp; } else { i=1; next=start; while(i<p-1) { next=next->rlink; i++; } temp->llink=next; temp->rlink=next->rlink; next->rlink->llink=temp; next->rlink=temp; } printf("\n\nNODE INSERTED SUCCESSFULLY");

DEPARTMENT OF CSE

} void del() { int p,i; printf("\nENTER THE POSITION TO BE DELETED:"); scanf("%d",&p); if(p==1) { temp=start; start=temp->rlink; start->llink=NULL; } else { i=1; next=start; while(i<p-1) { next=next->rlink; i++; } temp=next->rlink; next->rlink=temp->rlink; prev=next->rlink; prev->llink=next; } free(temp); printf("\n\nNODE REMOVED SUCCESSFULLY"); } void display() {

CS 2208 - DATA STRUCTURES LAB MANUAL


11

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

temp=start; printf("\nTHE ELEMENTS IN THE DOUBLY LINKED LIST ARE:"); if(temp==NULL) { printf("\nTHERE ARE NO NODES IN THE LIST"); } else { while(temp->rlink!=NULL) { printf("\t%d",temp->data); temp=temp->rlink; } printf("\t%d",temp->data); } }

OUTPUT:
ENTER THE ELEMENT:10 MENU 2.DELETE 4.EXIT ENTER UR CHOICE:1 ENTER THE POSITION TO BE INSERTED:2 ENTER THE ELEMENT:20 NODE INSERTED SUCCESSFULLY 1.INSERT 3.DISPLAY

MENU 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT ENTER UR CHOICE:3 THE ELEMENTS IN THE DOUBLY LINKED LIST ARE: 10 20 MENU 2.DELETE 4.EXIT ENTER THE POSITION TO BE DELETED:1 NODE REMOVED SUCCESSFULLY 1.INSERT 3.DISPLAY ENTER UR CHOICE:2

MENU 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT ENTER UR CHOICE:3 THE ELEMENTS IN THE DOUBLY LINKED LIST ARE: 20 MENU 2.DELETE 4.EXIT ENTER UR CHOICE:4 1.INSERT 3.DISPLAY

RESULT:
Thus doubly linked list is implemented.

CS 2208 - DATA STRUCTURES LAB MANUAL


12

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX. NO : 2 POLYNOMIAL ADDITION


AIM:
To represent a polynomial as a linked list and write functions for polynomial addition.

ALGORITHM:
Step 1: Start the program Step 2: Read the number of terms for the first polynomial. Step 3: Get the co efficient and exponents of every term in the first polynomial. Step 4:Read the number of terms for the second polynomial. Step 5 : Get the co efficient and exponents of every term in the second polynomial. Step 6: Add the polynomials using function poly_add Step 6.1: Check for the exponents of first polynomial and the second polynomial. Step 6.2: If both the exponents are same the co efficients of first polynomial and second polynomial are added. Step 7: Display the result Step 8: Terminate the program.

PROGRAM :
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> struct polynode { int coeff; int exp; struct polynode *link; }; void poly_append(struct polynode**,int,int); void poly_display(struct polynode *); void poly_add(struct polynode*,struct polynode*,struct polynode**);

CS 2208 - DATA STRUCTURES LAB MANUAL


13

R.M.D ENGG. COLLEGE struct polynode *first,*second,*result; void main() { int i,n,x,y,m; clrscr(); first=second=result=NULL; /* Empty Linked Lists*/

DEPARTMENT OF CSE

printf("\nEnter the number of terms in the first polynomial:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nEnter the coefficient of term %d of first polynomial:",i); scanf("%d",&x); printf("\nEnter the exponent of term %d of first polynomial:",i); scanf("%d",&y); poly_append(&first,x,y); } printf("\nEnter the number of terms in the second polynomial:"); scanf("%d",&m); for(i=1;i<=m;i++) { printf("\nEnter the coefficient of term %d of second polynomial:",i); scanf("%d",&x); printf("\nEnter the exponent of term %d of second polynomial:",i); scanf("%d",&y); poly_append(&second,x,y); } printf("\nThe first polynomial is:\n"); poly_display(first); printf("\nThe second polynomial is:\n"); poly_display(second); poly_add(first,second,&result); printf("\nThe resultant polynomial is:\n"); poly_display(result); getch(); } void poly_append(struct polynode **q,int x ,int y) {

CS 2208 - DATA STRUCTURES LAB MANUAL


14

R.M.D ENGG. COLLEGE struct polynode *temp; temp=*q; /* Creates a new node if the list is empty */ if(*q==NULL) { *q=(polynode*)malloc(sizeof(struct polynode)); temp=*q; } else { /*Traverse the entire linked list */ while(temp->link!=NULL) temp=temp->link; /* Create new nodes at intermediate stages */

DEPARTMENT OF CSE

temp->link=(polynode*)malloc(sizeof(struct polynode)); temp=temp->link; } /*Assign coefficient and exponent */ temp->coeff=x; temp->exp=y; temp->link=NULL; } /* Displays the contents of linked list representing a polynomial */ void poly_display(struct polynode *q) { /* Traverse till the end of the linked list */ while(q!=NULL) { printf("%dX^%d :",q->coeff,q->exp); q=q->link; } } /* Adds two polynomials */ void poly_add(struct polynode *x,struct polynode *y, struct polynode **s) { struct polynode *z; /* if both linked lists are empty */

CS 2208 - DATA STRUCTURES LAB MANUAL


15

R.M.D ENGG. COLLEGE if(x==NULL && y==NULL) return; /*Traverse till one of the lists ends */ while(x!=NULL&&y!=NULL) { /*Creates a new node if the list is empty */ if(*s==NULL) {

DEPARTMENT OF CSE

*s=(polynode*)malloc(sizeof(struct polynode)); z=*s; } /* Create new nodes at intermediate stages */ else { z->link=(polynode*)malloc(sizeof(struct polynode)); z=z->link; } /*store a term of the larger degree polynomial */ if(x->exp<y->exp) { z->coeff=y->coeff; z->exp=y->exp; y=y->link; /*go to the next node */ } else { if(x->exp>y->exp) { z->coeff=x->coeff; z->exp=x->exp; x=x->link; /*go to the next node */ } else { /* Add the coefficients when exponents are equal*/ if(x->exp==y->exp) {

CS 2208 - DATA STRUCTURES LAB MANUAL


16

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE z->coeff=x->coeff+y->coeff; z->exp=x->exp; x=x->link; y=y->link; } }

} } /*assign remaining terms of the first polynomial to the result*/ while(x!=NULL) { if(*s==NULL) { *s=(polynode*)malloc(sizeof(struct polynode)); z=*s; } else { z->link=(polynode*)malloc(sizeof(struct polynode)); z=z->link; } z->coeff=x->coeff; z->exp=x->exp; x=x->link; } /*assign remaining terms of the second polynomial to the result*/ while(y!=NULL) { if(*s==NULL) { *s=(polynode*)malloc(sizeof(struct polynode)); z=*s; } else { z->link=(polynode*)malloc(sizeof(struct polynode)); z=z->link;

CS 2208 - DATA STRUCTURES LAB MANUAL


17

R.M.D ENGG. COLLEGE } z->coeff=y->coeff; z->exp=y->exp; y=y->link; }

DEPARTMENT OF CSE

z->link=NULL;/*assign NULL at end of resultant linked list*/ }

OUTPUT:
Enter the number of terms in the first polynomial:3 Enter the coefficient of term 1 of first polynomial:1 Enter the exponent of term 1 of first polynomial:2 Enter the coefficient of term 2 of first polynomial:2 Enter the exponent of term 2 of first polynomial:1 Enter the coefficient of term 3 of first polynomial:3 Enter the exponent of term 3 of first polynomial:0 Enter the number of terms in the second polynomial:3 Enter the coefficient of term 1 of second polynomial:1 Enter the exponent of term 1 of second polynomial:2 Enter the coefficient of term 2 of second polynomial:2 Enter the exponent of term 2 of second polynomial:1 Enter the coefficient of term 3 of second polynomial:3 Enter the exponent of term 3 of second polynomial:0 The first polynomial is: The second polynomial is: The resultant polynomial is:

1X^2 :2X^1 :3X^0 : 1X^2 :2X^1 :3X^0 : 2X^2 :4X^1 :6X^0 :

RESULT :
Thus functions for polynomial addition is implemented by usinglinked list.

CS 2208 - DATA STRUCTURES LAB MANUAL


18

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX.NO: 3 CONVERT INFIX TO POSTFIX EXPRESSION


AIM:
To write a C program to implement stack and use it to convert infix to postfix expression.

ALGORITHM:

1. Start the program 2. Scan the Infix string from left to right. 3. Initialise an empty stack. 4. If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.

If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.

Repeat this step till all the characters are scanned.

5. (After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty. 6. Return the Postfix string. 7. Terminate the program.

PROGRAM :
#include<stdio.h> #include<conio.h> char inf[50],post[50],st[50]; int top=-1; void push(int); char pop(); void postfix(); void main() {

CS 2208 - DATA STRUCTURES LAB MANUAL


19

R.M.D ENGG. COLLEGE clrscr(); printf("\nEnter the infix expression:"); scanf("%s",inf); postfix(); getch(); } void postfix() { int i,j=0; for(i=0;inf[i]!='\0';i++) { switch(inf[i]) { case '+':while(st[top]>=1) post[j++]=pop(); push(1); break; case '-':while(st[top]>=1) post[j++]=pop(); push(2); break; case '*':if((st[top]==1)||(st[top]==2)) push(3); else if(st[top]==5) { post[j++]=pop(); push(3); } else { while(st[top]>=1) post[j++]=pop(); push(3); } break; case '/': if((st[top]==1)||(st[top]==2)) push(4); else { while(st[top]>=1) post[j++]=pop(); push(4); } break; case '^':if((st[top]==1)||(st[top]==2)) push(5); else { while(st[top]>=1) post[j++]=pop(); push(5); } break; case '(':push(0); break;

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


20

R.M.D ENGG. COLLEGE case ')':while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=inf[i]; } } while(top>=0) post[j++]=pop();

DEPARTMENT OF CSE

printf("\nThe equivalent Postfix expression is %s",post); } void push(int ele) { top++; st[top]=ele; } char pop() { int e; char el; e=st[top]; top--; switch(e) { case 1:el='+'; break; case 2:el='-'; break; case 3:el='*'; break; case 4:el='/'; break; case 5:el='^'; break; } return el; }

OUTPUT:
Enter the infix expression:a+b*c The equivalent Postfix expression is abc*+

RESULT:
Thus the program to convert infix expression to postfix expression is executed.

CS 2208 - DATA STRUCTURES LAB MANUAL


21

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX. NO : 4 IMPLEMENTATION OF A DEQUE(DOUBLE-ENDED QUEUE) USING AN ARRAY


A Deque(Double- ended queue) is a linear list in which elements can be added or removed at either end but not in the middle. The items can be added or deleted from the front or rear end, but no changes can be made elsewhere in the list.

AIM:
To write a C program to implement a deque where insertion or deletion operations are possible at both ends.

ALGORITHM:
Step 1: Declare 4 functions addatbeg() , addatend(),delatbeg(), delatend() addatbeg() to add an element at beginning addatend() to add an element at end delatbeg() to delete an element at beginning delatend() to delete an element at end Step 2: Declare two variables front and rear and initialise them to -1. Step 3: addatbeg() function checks whether the deque is full before adding an element.When an element is added at beginning then the element in the front is shifted to the right and the new element is added at front. Step 4: addatend() function checks whether the deque is full before adding an element.When an element is added at the end then the element in the front is shifted to the left and the new element is added at end.The front is decremented by 1. Step 5: delatbeg() function removes an element from front position.After an element is removed , front stores the index of next element in deque.Front is incremented by 1. Step 6: delatend()function removes an element from rear position.After an element is removed , rear stores the index of next element that occupies the position to the left of the element being deleted.Rear is decremented by 1. Step 7: Count() function is used to count the number of elements in the deque. Step 8: Display() function is called after every insertion or deletion operation to show the contents of the deque.

CS 2208 - DATA STRUCTURES LAB MANUAL


22

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

PROGRAM :
#include <stdio.h> #include <conio.h> #include<stdlib.h> #define MAX 10 void addqatbeg ( int *, int, int *, int * ) ; void addqatend ( int *, int, int *, int * ) ; int delqatbeg ( int *, int *, int * ) ; int delqatend ( int *, int *, int * ) ; void display ( int * ) ; int count ( int * ) ; void main( ) { int arr[MAX] ; int front, rear,ch,i, n,item; clrscr( ) ; /* initialises data members */ front = rear = -1 ; for ( i = 0 ; i < MAX ; i++ ) arr[i] = 0 ; printf("\n\t\t Program shows working of double ended queue"); do { printf("\n\t\t Menu"); printf("\n\t\t 1: insert at rear end"); printf("\n\t\t 2: insert at front end"); printf("\n\t\t 3: delete from front end"); printf("\n\t\t 4: delete from rear end"); printf("\n\t\t 5: count the number of elements in deque"); printf("\n\t\t 6: Exit"); printf("\n\t\t Enter choice :"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element to be inserted:"); scanf("%d",&item); addqatend(arr,item,&front,&rear); display(arr); break; case 2: printf("\nEnter the element to be inserted:"); scanf("%d",&item); addqatbeg(arr,item,&front,&rear); display(arr); break; case 3:delqatbeg(arr,&front,&rear);

CS 2208 - DATA STRUCTURES LAB MANUAL


23

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

display(arr); break; case 4:delqatend(arr,&front,&rear); display(arr); break; case 5: n = count ( arr ) ; printf ( "\nTotal number of elements in deque: %d", n ) ; break; case 6: exit(0); break; } }while(ch<7); getch( ) ; } /* adds an element at the beginning of a deque */ void addqatbeg ( int *arr, int item, int *pfront, int *prear ) { int i, k, c ; if ( *pfront == 0 && *prear == MAX - 1 ) { printf ( "\nDeque is full.\n" ) ; return ; } if ( *pfront == -1 ) { *pfront = *prear = 0 ; arr[*pfront] = item ; return ; } if ( *prear != MAX - 1 ) { c = count ( arr ) ; k = *prear + 1 ; for ( i = 1 ; i <= c ; i++ ) { arr[k] = arr[k - 1] ; k-- ; } arr[k] = item ; *pfront = k ; ( *prear )++ ; } else { ( *pfront )-- ; arr[*pfront] = item ; } } /* adds an element at the end of a deque */

CS 2208 - DATA STRUCTURES LAB MANUAL


24

R.M.D ENGG. COLLEGE void addqatend ( int *arr, int item, int *pfront, int *prear ) { int i, k ; if ( *pfront == 0 && *prear == MAX - 1 ) { printf ( "\nDeque is full.\n" ) ; return ; } if ( *pfront == -1 ) { *prear = *pfront = 0 ; arr[*prear] = item ; return ; } if ( *prear == MAX - 1 ) { k = *pfront - 1 ; for ( i = *pfront - 1 ; i < *prear ; i++ ) { k=i; if ( k == MAX - 1 ) arr[k] = 0 ; else arr[k] = arr[i + 1] ; } ( *prear )-- ; ( *pfront )-- ; } ( *prear )++ ; arr[*prear] = item ; } /* removes an element from the *pfront end of deque */ int delqatbeg ( int *arr, int *pfront, int *prear ) { int item ; if ( *pfront == -1 ) { printf ( "\nDeque is empty.\n" ) ; return 0 ; } item = arr[*pfront] ; arr[*pfront] = 0 ; if ( *pfront == *prear ) *pfront = *prear = -1 ; else ( *pfront )++ ; return item ;

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


25

R.M.D ENGG. COLLEGE } /* removes an element from the *prear end of the deque */ int delqatend ( int *arr, int *pfront, int *prear ) { int item ; if ( *pfront == -1 ) { printf ( "\nDeque is empty.\n" ) ; return 0 ; } item = arr[*prear] ; arr[*prear] = 0 ; ( *prear )-- ; if ( *prear == -1 ) *pfront = -1 ; return item ; } /* displays elements of a deque */ void display ( int *arr ) { int i ; printf ( "\n front->" ) ; for ( i = 0 ; i < MAX ; i++ ) printf ( "\t%d", arr[i] ) ; printf ( " <-rear" ) ; } /* counts the total number of elements in deque */ int count ( int *arr ) { int c = 0, i ; for ( i = 0 ; i < MAX ; i++ ) { if ( arr[i] != 0 ) c++ ; } return c ; }

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


26

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

OUTPUT:
Program shows working of double ended queue Menu 1: insert at rear end 3: delete from front end 5: count the number of elements in deque Enter choice :2 Enter the element to be inserted:10 front-> 10 0 0 0 0 0 0 Menu 1: insert at rear end 3: delete from front end 5: count the number of elements in deque Enter choice :1 Enter the element to be inserted:20 front-> 10 20 0 0 0 0 Menu 1: insert at rear end 3: delete from front end 5: count the number of elements in deque Enter choice :5 Total number of elements in deque: 2 Menu 1: insert at rear end 3: delete from front end 5: count the number of elements in deque Enter choice : 6

2: insert at front end 4: delete from rear end 6: Exit 0 0 0 <-rear

2: insert at front end 4: delete from rear end 6: Exit 0 0 0 0 <-rear

2: insert at front end 4: delete from rear end 6: Exit

2: insert at front end 4: delete from rear end 6: Exit

RESULT:
Thus the C program to implement double ended queue is executed

CS 2208 - DATA STRUCTURES LAB MANUAL


27

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX. NO: 5 :
IMPLEMENTATION OF AN EXPRESSION TREE AND TREE TRAVERSALS
AIM:
To write a C program to implement an expression tree and perform the various tree traversals on it.

ALGORITHM:
Step 1: Input the postfix expression. Step 2: Read one character at a time till end of expression is reached and do the following:Step 2.1 : If the character is an operand then create a one-node tree and push a pointer to it onto stack. the

Step 2.2: If the character is an operator ,then pop two pointers t1 and t2 from the stack and create a new tree with the operator as the root and make t1 as the right child and t2 as the left child respectively.Push a pointer to this new tree onto the stack. Step 3: Now print the tree in inorder, postorder and preorder traversals. Step 1: Process the left child Step2 : Process the root node Step 3: Process the right child

POSTORDER TRAVERSAL Step 1: Process the left child Step2 : Process the right child Step 3: Process the root node PREORDER TRAVERSAL Step 1: Process the root node Step2 : Process the left child

CS 2208 - DATA STRUCTURES LAB MANUAL


28

R.M.D ENGG. COLLEGE Step 3: Process the right child.

DEPARTMENT OF CSE

PROGRAM:
#include<stdio.h> #include<conio.h> #include<alloc.h> #include<ctype.h> #define size 20 typedef struct node { char data; struct node *left; struct node *right; }btree; btree *stack[size]; int top; void main() { btree *root; char exp[80];

CS 2208 - DATA STRUCTURES LAB MANUAL


29

R.M.D ENGG. COLLEGE btree *create(char exp[80]); void inorder(btree *root); void preorder(btree *root); void postorder(btree *root); clrscr(); printf("\nEnter the postfix expression:"); scanf("%s",exp); top=-1; root=create(exp); printf("\nThe tree is created"); printf("\nThe inorder traversal is :\n"); inorder(root); printf("\nThe preorder traversal is :\n"); preorder(root); printf("\nThe postorder traversal is :\n"); postorder(root); getch(); } btree *create(char exp[]) { btree *temp; int pos; char ch;

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


30

R.M.D ENGG. COLLEGE void push(btree *); btree *pop(); pos=0; ch=exp[pos]; while(ch!='\0') { temp=(btree*)malloc(sizeof(btree)); temp->left=temp->right=NULL; temp->data=ch; if(isalpha(ch)) push(temp); else if(ch=='+'||ch=='-'||ch=='*'||ch=='/') { temp->right=pop(); temp->left=pop(); push(temp); } else printf("Invalid character\n"); pos++; ch=exp[pos]; } temp=pop();

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


31

R.M.D ENGG. COLLEGE return(temp); } void push(btree *node) { if(top+1>=size) printf("\nstack is Full"); top++; stack[top]=node; } btree *pop() { btree *node; if(top==-1) printf("\nStack is empty"); node=stack[top]; top--; return(node); } void inorder(btree *root) { btree *temp; temp=root; if(temp!=NULL)

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


32

R.M.D ENGG. COLLEGE { inorder(temp->left); printf("%c",temp->data); inorder(temp->right); } } void preorder(btree *root) { btree *temp; temp=root; if(temp!=NULL) { printf("%c",temp->data); preorder(temp->left); preorder(temp->right); } } void postorder(btree *root) { btree *temp; temp=root; if(temp!=NULL) {

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


33

R.M.D ENGG. COLLEGE postorder(temp->left); postorder(temp->right); printf("%c",temp->data); }

DEPARTMENT OF CSE

OUTPUT:
Enter the postfix expression:abc+* The tree is created The inorder traversal is : The preorder traversal is : The postorder traversal is : a*b+c *a+bc abc+*

RESULT:
Thus the C program to print inorder,preorder and postorder traversal is executed.

CS 2208 - DATA STRUCTURES LAB MANUAL


34

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX. NO: 6 BINARY SEARCH TREE


AIM:
To write a C program to perform various operations on a binary search tree

ALGORITHM:
CREATION OF BINARY SEARCH TREE: Step 1: Enter the number of elements in the tree n Step 2: Enter the n elements into the binary search tree Step 3: As each element is put into the tree check whether tree is binary

SEARCH TREE PROPERTY IS MAINTAINED: Step 1: Enter the element to be inserted x Step 2: If x< root, Keep moving towards the left sub tree Step 3: If x>root, Keep moving towards right sub tree Step 4: Perform steps 2 and 3 for the sub trees until the exact location of insertion is Found Step 5: Insert at that location and display the new tree using inorder traversal

DELETION OF A NODE: Step 1: Enter the element to be deleted x Step 2: If X has no children then it can be deleted immediately

CS 2208 - DATA STRUCTURES LAB MANUAL


35

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

Step 3: If x has no child then adjust the parents pointer by passing the node Step 4: If the node has two children then replace the data in the node with the minimum Data in the right sub tree of that node and then delete the node recursively Step 5: Display the new tree using inorder traversal

FINDING THE MINIMUM AND MAXIMUM Step 1: More towards the left sub tree to find the minimum Step 2: More towards the right sub tree to find the maximum

PROGRAM :
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct treenode { int element; struct treenode *left; struct treenode *right; }; typedef struct treenode *position,*searchtree; searchtree insert(int x,searchtree t) { if(t==NULL) { t=(struct treenode*)malloc(sizeof(struct treenode)); if(t==NULL) exit(0); else { t->element=x; t->left=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; } position findmin(searchtree t) { if(t==NULL) return NULL;

CS 2208 - DATA STRUCTURES LAB MANUAL


36

R.M.D ENGG. COLLEGE else if(t->left==NULL) return t; else return findmin(t->left); } position findmax(searchtree t) { if(t==NULL) return NULL; else if(t->right==NULL) return t; else return findmax(t->right); }

DEPARTMENT OF CSE

searchtree rem(int x,searchtree t) { position temp; if(t==NULL) printf("\nElement not found"); else if(x<t->element) t->left=rem(x,t->left); else if(x>t->element) t->right=rem(x,t->right); else if(t->left&&t->right) { temp=findmin(t->right); t->element=temp->element; t->right=rem(t->element,t->right); } else { temp=t; if(t->left==NULL) t=t->right; else if(t->right==NULL) t=t->left; free(temp); } return t; } void intrav(searchtree head) { if(head==NULL) return; if(head->left!=NULL)

CS 2208 - DATA STRUCTURES LAB MANUAL


37

R.M.D ENGG. COLLEGE intrav(head->left); printf("%d\t",head->element); if(head->right!=NULL) intrav(head->right); } void main() { int n,i,dat,ch; searchtree t=NULL; position node; clrscr(); printf("Enter no of elements:\n"); scanf("%d",&n); printf("Enter the elements:\n"); for(i=1;i<=n;i++) { scanf("%d",&dat); t=insert(dat,t); } intrav(t); do { printf("\n\n"); printf("\n ****MENU****\n"); printf("\nEnter 1 -> Insert a node\n"); printf("\n 2 -> Delete a node\n"); printf("\n 3 -> Find Minimum\n"); printf("\n 4 -> Find Maximum\n"); printf("\n 5 -> Display(Inorder Traversal\n"); printf("\n 6 -> Exit\n"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1:printf("\nEnter the element to be inserted:"); scanf("%d",&dat); t=insert(dat,t); break; case 2:printf("\n Enter the node to be deleted:"); scanf("%d",&dat); t=rem(dat,t); break; case 3:node=findmin(t); printf("\nThe minimum element is %d",node->element); break; case 4:node=findmax(t); printf("\nThe maximum element is %d",node->element); break; case 5:intrav(t); break; case 6:exit(0); } }while(ch!=6); getch();

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


38

R.M.D ENGG. COLLEGE }

DEPARTMENT OF CSE

OUTPUT:
Enter no of elements: 3 Enter the elements: 10 6 2

10

****MENU**** Enter 1 -> Insert a node 2 -> Delete a node 3 -> Find Minimum 4 -> Find Maximum 5 -> Display(Inorder Traversal 6 -> Exit Enter your choice:3 The minimum element is 2 ****MENU**** Enter 1 -> Insert a node 2 -> Delete a node 3 -> Find Minimum 4 -> Find Maximum

CS 2208 - DATA STRUCTURES LAB MANUAL


39

R.M.D ENGG. COLLEGE 5 -> Display(Inorder Traversal 6 -> Exit Enter your choice:6

DEPARTMENT OF CSE

RESULT:
Thus the C program to implement binary search tree is executed.

EX. NO : 7 INSERTION IN AVL TREE


AIM : To write a C program to perform insertion in an AVL tree. ALGORITHM : Step 1: Read the elements to be inserted into an AVL tree one at a time. Step 2: Insert each element into an initially empty AVL tree based on the BST tree property. Step 3: After inserting an element check whether the balance property is maintained.If not then Step 3.1 :Identify the node at which the balance is lost. Step 3.2: Find the kind of insertion . If it is left-left or right right then perform a single rotation. Step 3.3: If it is left-right or right-left then perform a double rotation. Step 4: Repeat steps 2 and 3 till all the elements are inserted. Step 5: Display the elements present in the AVL tree according to inorder traversal. , .

PROGRAM:
#include<stdio.h> #include<stdlib.h> #include<conio.h> typedef struct node *pos; typedef struct node *avltree; avltree t; struct node

CS 2208 - DATA STRUCTURES LAB MANUAL


40

R.M.D ENGG. COLLEGE { int element; avltree left; avltree right; int height; }; avltree insert(int,avltree); int max(int,int); int height(pos p); pos singlerotatewithleft(pos); pos singlerotatewithright(pos); pos doublerotatewithleft(pos); pos doublerotatewithright(pos); void display(avltree); void main() { int ch,ele; clrscr(); t=NULL; do { printf("\nMENU\n"); printf("\n1. Insertion\n"); printf("\n2. Display\n"); printf("\n3. Exit\n"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch)

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


41

R.M.D ENGG. COLLEGE {

DEPARTMENT OF CSE

case 1: printf("\nEnter the element to be inserted:"); scanf("%d",&ele); t=insert(ele,t); printf("\nThe element is inserted"); break; case 2: printf("\nThe elements in the AVL TREE are:"); display(t); break; case 3: exit(0); break; } }while(ch<4); getch(); } avltree insert(int x,avltree t) { if(t==NULL) { t=(node *)malloc(sizeof(struct node)); if(t==NULL) exit(0); else { t->element=x;t->height=0; t->left=t->right=NULL; } }

CS 2208 - DATA STRUCTURES LAB MANUAL


42

R.M.D ENGG. COLLEGE 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->left->element) t=singlerotatewithright(t); else t=doublerotatewithright(t); } t->height=max(height(t->left),height(t->right))+1; return t; } int height(pos p) { if(p==NULL) return -1; else

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


43

R.M.D ENGG. COLLEGE return p->height; } int max(int p,int q) { if(p>q) return p; else return q; } pos singlerotatewithleft(pos k2) { pos 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; } pos singlerotatewithright(pos k1) { pos k2; k2=k1->right; k1->right=k2->left; k2->left=k1; k1->height=max(height(k1->left),height(k1->right))+1; k2->height=max(k1->height,height(k2->right))+1; return k2;

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


44

R.M.D ENGG. COLLEGE } pos doublerotatewithleft(pos k3) { k3->left=singlerotatewithright(k3->left); return singlerotatewithleft(k3); } pos doublerotatewithright(pos k1) { k1->right=singlerotatewithleft(k1->right); return singlerotatewithright(k1); } void display(avltree t) { if(t!=NULL) { display(t->left); printf("\n%d",t->element); display(t->right); } }

DEPARTMENT OF CSE

OUTPUT:
MENU 1. Insertion 2. Display 3. Exit Enter your choice:1 Enter the element to be inserted:10 The element is inserted MENU 1. Insertion

CS 2208 - DATA STRUCTURES LAB MANUAL


45

R.M.D ENGG. COLLEGE 2. Display 3. Exit Enter your choice:2 The elements in the AVL TREE are:

DEPARTMENT OF CSE

10

MENU 1. Insertion 2. Display 3. Exit Enter your choice:1 Enter the element to be inserted:20 The element is inserted MENU 1. Insertion 2. Display 3. Exit Enter your choice: 3

RESULT:
Thus the C program to implement AVL tree is executed.

CS 2208 - DATA STRUCTURES LAB MANUAL


46

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX. NO : 8 : IMPLEMENTATION OF PRIORITY QUEUE USING BINARY HEAPS


AIM :
To write a C program to implement a priority queue using binary heap.

ALGORITHM:
Step 1: Enter the maximum number of elements in the priority queue. Step 2: Initialize the priority queue as follows: Step 2.1 : Initialize heap size = 0. Step 2.2 : Initialize heap capacity = maxelements. Step 2.3 : Allocate space for the maximum number of elements in the priority queue. Step 3: After performing the required number of insertions and deletions , display the elements in the heap. Insertion Step 1: Enter the element to be inserted. Step 2: Insert the element in the respective hole by using the percolate up strategy. DeleteMin: Step 1: Delete the root of the heap and return it. Step 2: Percolate down the hole till the right place for the lastelement is found.

PROGRAM :
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<malloc.h> typedef struct heapstruct *pqueue; struct heapstruct { int capacity; int size; int *elements; };

CS 2208 - DATA STRUCTURES LAB MANUAL


47

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

void insert(int,pqueue); pqueue initialize(int); int deletemin(pqueue); int isfull(pqueue); int isempty(pqueue); void display(pqueue); void main() { pqueue heap; int i,max,ele,ch,t; clrscr(); printf("\nEnter the maximum no.of elements in the priority queue:"); scanf("%d",&max); heap=initialize(max); do { printf("\nMENU\n"); printf("\n1. Insertion\n"); printf("\n2.DeleteMin\n"); printf("\n3. Display\n"); printf("\n4. Exit\n"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element to be inserted:"); scanf("%d",&ele); insert(ele,heap); printf("\nThe element is inserted"); break; case 2: t=deletemin(heap); printf("\nThe minimum element %d is deleted\n",t); break; case 3: printf("\nThe elements in the HEAP are:"); display(heap); break; case 4: exit(0); break; } }while(ch<4); getch(); } pqueue initialize(int max) { pqueue h; if(max<3) { printf("\nPriority queue size is too small\n"); exit(0); } h=(heapstruct*)malloc(sizeof(struct heapstruct)); if(h==NULL) exit(0); h->elements=(int*)malloc(max+1*sizeof(int)); if(h->elements==NULL)

CS 2208 - DATA STRUCTURES LAB MANUAL


48

R.M.D ENGG. COLLEGE exit(0); h->capacity=max; h->size=0; return h;

DEPARTMENT OF CSE

} void insert(int x,pqueue h) { int i; if(isfull(h)) { printf("\nPriority queue is full"); return; } if(h->size==0) { h->elements[1]=x; h->size++; } else { for(i=++h->size;h->elements[i/2]>x;i/=2) h->elements[i]=h->elements[i/2]; h->elements[i]=x; } } int deletemin(pqueue h) { int i,child,minelement,lastelement; if(isempty(h)) { printf("\nPriority queue is empty"); exit(0); } minelement=h->elements[1]; lastelement=h->elements[h->size--]; for(i=1;i*2<=h->size;i=child) { child=i*2; if(child!=h->size&&h->elements[child+1]<h->elements[child]) child++; if(lastelement>h->elements[child]) h->elements[i]=h->elements[child]; else break; } h->elements[i]=lastelement; return minelement; } void display(pqueue h) { int i; for(i=1;i<=h->size;i++) printf("\n%d",h->elements[i]); } int isfull(pqueue h)

CS 2208 - DATA STRUCTURES LAB MANUAL


49

R.M.D ENGG. COLLEGE { if(h->size==h->capacity) return 1; else return 0; } int isempty(pqueue h) { if(h->size==0) return 1; else return 0; }

DEPARTMENT OF CSE

OUTPUT:
Enter the maximum no.of elements in the priority queue:5 MENU 1. Insertion 2.DeleteMin 3. Display 4. Exit Enter your choice:1 Enter the element to be inserted:40 The element is inserted MENU 1. Insertion 2.DeleteMin 3. Display 4. Exit Enter your choice:1 Enter the element to be inserted:30 The element is inserted MENU 1. Insertion 2.DeleteMin 3. Display 4. Exit Enter your choice:

RESULT:
Thus the C program to implement priority queue is executed.

CS 2208 - DATA STRUCTURES LAB MANUAL


50

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX. NO : 9 HASHING WITH OPEN ADDRESSING


AIM:
To write a C program to implement hashing with open addressing

ALGORITHM:
Step 1: Read the elements to be entered into the hash table one at a time. Step 2: Using create() function generate the hash key.The hash function used is number %10. Step 3: The linear_prob() function is used to handle the collision. If the location indicated by hash key is empty then place the element in the hash table.Otherwise move linearly down searching for an empty location and place the number at the empty location encountered. Step 4: The display() function is used to display the hash table.

PROGRAM:
#include<stdio.h> #include<conio.h> #include<stdlib.h> #deine MAX 10 void main() { int a[MAX],num,key,i; char ans; int create(int); void linear_prob(int[],int,int); void display(int[]); clrscr(); printf("\nCollision Handling by Linear Probing"); for(i=0;i<MAX;i++) a[i]=-1; do { printf("\nEnter the number:"); scanf("%d",&num); key=create(num); linear_prob(a,key,num); printf("\nDo you want to continue?");

CS 2208 - DATA STRUCTURES LAB MANUAL


51

R.M.D ENGG. COLLEGE ans=getch(); }while(ans=='y'); display(a); } int create(int num) { int key; key=num%10; return key; } void linear_prob(int a[MAX],int key,int num) { int flag,i,count=0; void display(int a[]); flag=0; if(a[key]==-1) a[key]=num; else { i=0; while(i<MAX) { if(a[i]!=-1) count++; i++; } if(count==MAX) { printf("\nHash Table is full"); display(a); getch(); exit(1); } for(i=key+1;i<MAX;i++) if(a[i]==-1) { a[i]=num; flag=1; break; } for(i=0;i<key&&flag==0;i++) if(a[i]==-1) { a[i]=num; flag=1; break; } } } void display(int a[MAX]) { int i; printf("\nThe Hash Table is ...\n"); for(i=0;i<MAX;i++) printf("\n %d %d",i,a[i]);

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


52

R.M.D ENGG. COLLEGE }

DEPARTMENT OF CSE

OUTPUT:
Collision Handling by Linear Probing Enter the number:10 Do you want to continue? Enter the number: 11 Do you want to continue? Enter the number:15 Do you want to continue? Enter the number:19 Do you want to continue? Enter the number:20 Do you want to continue? Enter the number:25 Do you want to continue? Enter the number:17 Do you want to continue? The Hash Table is ... 0 1 2 3 4 5 6 7 8 9 10 11 20 -1 -1 15 25 17 -1 19

RESULT :
Thus the c program to implement hashing with open addressing is executed.

CS 2208 - DATA STRUCTURES LAB MANUAL


53

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

EX. NO : 10 PRIMS ALGORITHM


AIM:
To write a C program to implement a minimum spanning tree using Prims Algorithm

ALGORITHM:
Step 1: To compute a minimum spanning tree grow the tree in successive stages. Step 2: In each stage, one node is picked as the root and we add an edge and thus an associated vertex to the tree. Step 3: At each stage, a new vertex is added to the tree by choosing the edge (u,v) such that the cost of (u,v) is the smallest among all edges where u is in the tree and v is not.

PROGRAM :
/*prims algorithm */ #include<stdio.h> #include<conio.h> #include<process.h> float lcost[100],a[100][100]; int closest[100],i,j,k,min,n,c=0; void get_mat() { printf("Enter the No of Vertices:"); scanf("%d",&n); printf("enter 1000 for no path \n");

CS 2208 - DATA STRUCTURES LAB MANUAL


54

R.M.D ENGG. COLLEGE printf("enter weighted matrix\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("cost between the edge\t%d,%d:\t",i,j); scanf("%f",&a[i][j]); }//inner for }//outer for }//fn void prim1() { { lcost[i]=a[1][i]; closest[i]=1; } printf("minimum cost spanning tree edges are \n"); for(i=2;i<=n;i++) { min=lcost[2]; k=2; for(j=3;j<=n;j++) { if(lcost[j]<min) { min=lcost[j]; k=j; } for(i=2;i<=n;i++)

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


55

R.M.D ENGG. COLLEGE } c=c+min; printf("(%d,%d)\tcost=%d\t",closest[k],k,min); lcost[k]=2000; for(j=2;j<=n;j++) if((a[k][j]<lcost[j])&&(lcost[j]<2000)) { lcost[j]=a[k][j]; closest[j]=k; } printf("\n"); }//outer for printf("\n\nWeight of minimum cost spanning tree :%d",c); getch(); } void main() {int ch; clrscr(); do {

DEPARTMENT OF CSE

printf("\n1.get\n2.find path with minimum cost\n3.exit\nenter ur choice\n"); scanf("%d",&ch); switch(ch) { case 1: get_mat(); break; case 2:

CS 2208 - DATA STRUCTURES LAB MANUAL


56

R.M.D ENGG. COLLEGE prim1(); break; case 3: exit(0); break; }//switch }while(ch<=3);//do getch(); }//main

DEPARTMENT OF CSE

OUTPUT:
1.get 3.exit enter ur choice 1 2.find path with minimum cost

Enter the No of Vertices:3 enter 1000 for no path enter weighted matrix cost between the edge cost between the edge cost between the edge cost between the edge cost between the edge 1.get 3.exit enter ur choice 2 1,1: 1,3: 2,2: 3,1: 3,3: 1000 2 1000 2 1000 cost between the edge cost between the edge cost between the edge cost between the edge 2.find path with minimum cost 1,2: 10 2,1: 10 2,3: 1 3,2: 1

minimum cost spanning tree edges are (1,3) cost=2 (3,2) cost=1 Weight of minimum cost spanning tree :3 1.get 3.exit enter ur choice 3 2.find path with minimum cost

RESULT:
Thus the c program to implement prims algorithm is executed.

CS 2208 - DATA STRUCTURES LAB MANUAL


57

R.M.D ENGG. COLLEGE

DEPARTMENT OF CSE

CS 2208 - DATA STRUCTURES LAB MANUAL


58

Vous aimerez peut-être aussi