Ex. No: 1 Date: ARRAY IMPLEMENTATION OF LIST ADT AIM: To develop a C program to implement list ADT using array. ALGORITHM: Step 1: Start the program Step 2: Declare the required variable and functions Step 3: Read the options using variable n Step 4: Check the while loop condition until the conditions becomes false. If a condition is true execute Step 5 and repeat the step 4. If a condition is false go to step6 and execute Step 5: Using switch case statement evaluate the n variable If case 1 is true execute the getdata functions and terminate the switch statement. If case 2 is true execute the insert functions and terminate the switch statement. If case 3 is true execute the functions display and terminate the switch statement. If case 4 is true execute the delete functions and terminate the switch statement. If case 5 is true execute the exit functions and terminate the program. Step 6: Stop the program GET DATA: Step 1: Start Step 2: Read the data by using the variable item Step 3: Assign the value of an variable item to a[count] Step 4: Increment the count value by 1 Step 5: Stop DISPLAY FUNCTION: Step 1: Start Step 2: Repeat step3 with initial value i equal to 0, every time increment 1 by one until i less than count Step 3: Read the value one by one by using the array variable a [] from keyboard Step 4: Stop INSERT FUNCTION: Step 1: Start Step 2: Declare the variables item, pos and i. Step 3: Read the variables by using the variable item Step 4: Read the position by using the variable pos. Step 5: Check whether the pos are less than or equal max-1 OR pos greater than count, if it is true then print enter the valid position. Step 6: Check whether the pos is equal to count, if it is true assign value of a variable tem to an array a [count]. Increment the value of a variable count by 1 and return the value to main Step 7: Check whether the pos value is less than count, if the condition is true assign the value of a variable count to i. assign the value of an array a[pos] to a[i] then assign the value of a variable item to a[pos], then increment value of an variable count by 1. Step 8: Stop 1 DELETE FUNCTION: Step 1: Declare required variables Step 2: Read the position by using the variable pos Step 3: Check whether the pos is less then zero OR pos is greater than count-1, if the condition is true delete the item from an array a[count] Step 4: If the above condition is false, assign the value of a variable pos to mov. Step 5: Check value of a variable mov is equal to count-1. If the condition is true assign the value a[mov+1] to a[mov], increment the value mov by 1. Step 6: Decrement the value of a variable count by 1. Step 1: Start the program Step 2: Declare the required variable and functions Step 3: Read the options using variable n Step 4: Check the while loop condition until the conditions becomes false. If a condition is true execute Step 5 and repeat the step 4. If a condition is false go to step6 and execute Step 5: Using switch case statement evaluate the n variable If case 1 is true execute the getdata functions and terminate the switch statement. If case 2 is true execute the insert functions and terminate the switch statement. If case 3 is true execute the functions display and terminate the switch statement. If case 4 is true execute the delete functions and terminate the switch statement. If case 5 is true execute the exit functions and terminate the program. Step 6: Stop the program PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<malloc.h> #define max 50 void getdata(); void insert(); void display(); void del(); int a[max],count=0,mov; void main() { int n; clrscr(); printf("\nARRAY IMPLEMENTATION OF LIST\n"); while(1) { printf("\n 1.getdata\n 2.insert\n 3.display\n 4.delete\n 5.exit"); printf("\nEnter your choice: "); scanf("%d",&n); switch(n) { case 1: getdata(); break; 2 case 2: insert(); break; case 3: display(); break; case 4: del(); break; case 5: exit(0); break; } } } void getdata() { int item; printf("\n Enter the Data :"); scanf("%d",&item); a[count]=item; count++; } void display() { int i; printf("\nThe list is : "); for(i=0;i<count;i++) printf("[%d]",a[i]); } void insert() { int item,pos,i; printf("\nEnter the data\n"); scanf("%d",&item); printf("Enter the position:"); scanf("%d",&pos); if(pos>=max-1||pos>count) { printf("Enter the valid position"); return; } if(pos==count) { a[count]=item; count++; return; } if(pos<count) { i=count; a[i]=a[pos]; 3 a[pos]=item; count++; } } void del() { int pos,mov; printf("\nEnter the position:"); scanf("%d",&pos); if(pos<0||pos>count-1) { printf("The deleted item is %d", a[count]); count++; return; } else { mov=pos; while(mov<count-1) { a[mov]=a[mov+1]; mov++; } count--; return; } } OUTPUT: 4 RESULT: Thus the array implementation of list abstract data type (ADT) program was written and output was verified. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 5 Ex. No: 2 Date: SINGLY LINKED LIST AIM: To write a C program to create singly linked list and perform operations on it. ALGORITHM: Step 1: Start the program Step 2: Create a Structure named as node and also create a corresponding structure variable named as info and next Step 3: Declare the functions with the name of create (), del (), display (), insert (), search () and totcount () Step 4: Read the choice value from the user using op variable Step 5: Create Switch case function Case 1: Create a singly linked list Case 2: Insert an element in singly linked list Case 3: Count total number of elements in list Case 4: Display the list elements Case 5: Delete the elements from the list Case 6: Search a given value in list Case 7: Exit Step 6: Create the list using create () method Step 7: Insert element in the list using insert () method Step 8: Delete element from the list using del () method Step 9: Count and display the total no. of the elements in the list using the totcount () method Step 10: Search the given element in the list using search () method Stop 11: Stop the program PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> void create(void); void del(void); void display(void); void insert(void); void search(void); void totcount(void); struct node { int info; struct node *next; }*head,*p,*q; void main() { int x,op; clrscr(); 6 while(1) { printf("\n1.create\n2.insert\n3.count\n4.display\n5.delete\n6.search\n7.exit\n"); printf("\nEnter the options : "); scanf("%d",&op); flushall(); switch(op) { case 1: create(); break; case 2: insert(); break; case 3: totcount(); break; case 4: display(); break; case 5: del(); break; case 6: search(); break; case 7: exit(0); } } } void create() { int item; printf("\n Enter item : "); scanf("%d",&item); head=NULL; p=(struct node*)malloc(sizeof(struct node)); p->info=item; p->next=NULL; head=p; } void insert() { int choice, item, pos, i; printf("\n1.begin \t 2.middle \t 3.end\n"); printf("\nEnter your option: "); scanf("%d", &choice); printf("\nEnter item: "); scanf("%d", &item); p=(struct node*)malloc(sizeof(struct node)); q=head; 7 if(choice==1) { p->info=item; p->next=head;; head=p; } if(choice==3) { while(q->next!=NULL) q = q->next; p->info=item; p->next=NULL; q->next=p; } if(choice==2) { printf("\nEnter position : "); scanf("%d",&pos); for(i=1;i<pos-1;i++) q=q->next; p->info=item; p->next=q->next; q->next=p; } } void display() { for(q=head;q->next!=NULL;q=q->next) printf("%d -> ",q->info); printf("%d",q->info); } void search() { int item; q=head; printf("enter the item which you want to search: "); scanf("%d",&item); while(q!=NULL) { if(q->info==item) { printf("\nGiven item is available") ; return; } q=q->next; } printf("\nThe item is not available\n"); } void totcount() { int cnt=0; for(q=head;q!=NULL;q=q->next) 8 cnt++; printf("\n Total nodes: %d",cnt); } void del() { int choice,i,pos; printf("\n 1.begin 2.middle 3.end\n"); scanf("%d",&choice); if(choice==1) { printf("\n Deleted item is %d",head->info); head=head->next; } if(choice==3) { for(q=head;(q->next)->next!=NULL;q=q->next); printf("\n The deleted item is %d",(q->next)->next->info); q->next=NULL; } if(choice==2) { p=head; scanf("%d",&pos); for(i=1;i<pos-1;i++) p=p->next; p->next=p->next->next; } } OUTPUT: 9 RESULT: Thus the linked list implementation of list abstract data type (ADT) program is executed. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 10 Ex. No: 3 POLYNOMIAL MANIPULATION Date: AIM: To write a C program to perform the polynomial addition using singly linked list. ALGORITHM: Step 1: Create a new node. Step 2: Create the poly1, poly2 and poly3 node and initialize to NULL. Step 3: Get the coefficient and power of two polynomial equations. Step 4: To perform the polynomial addition the following steps are followed Case 1: If power of polynomial1 is greater than power of polynomial2 Assign the polynomial1 to new node Case 2: Else check if polynomial2 is greater than power of polynomial1 Assign the polynomial2 to new node Case 3: Else if both doesn’t match, then the powers of two polynomial will be the same; do the following Case 4: Assign the polynomial1 power, and sum of polynomial1 co- efficient and polynomial2 co- efficient to new node Step 5: Stop the program. PROGRAM: #include<stdio.h> #include<malloc.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):"); scanf("%s",&ch); } while(ch=='y' || ch=='Y'); 11 } 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; 12 } 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("\nenter 1st number:"); create(poly1); printf("\nenter 2nd number:"); create(poly2); printf("\n1st Number:"); show(poly1); printf("\n2nd Number:"); show(poly2); polyadd(poly1,poly2,poly); printf("\nAdded polynomial:"); show(poly); printf("\n add two more numbers:"); scanf("%s",&ch); } while(ch=='y' || ch=='Y'); } OUTPUT: 13 RESULT: Thus the C program to perform the polynomial addition using singly linked list was written and executed successfully. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 14 Ex. No: 4a Date: ARRAY IMPLEMENTATION OF STACK AIM: To write a C program to implement stack using array data structure and perform the following stack operations 1. POP 2. PUSH 3. PEEP ALGORITHM: Step 1: Initialize stack, i, num Step 2: Add element in stack PUSH(S,TOP,X) Case 1: [Check overflow condition] If(TOP>=N) then Write(“Stack is full”) Case 2: [Insert element] [Increment TOP] TOP <- TOP+1 S[TOP]<- X Case 3: [Finish the process] Step 3: Delete element in stack POP(S,TOP) Case 1: [Check for underflow condition] If(TOP <- 0) then Write(“Stack is empty”) Case 2: [Delete element] [Decrement TOP] TOP<- TOP-1 Delete S[TOP+1] Case 3: [Finish the process] Step 4: Stop PROGRAM: #include<stdio.h> #include<conio.h> #define size 10 int stack[size],top=0,b; int res; void push(); void pop(); void display(); void main() { 15 int c; clrscr(); printf("\n1.Push\t2.Pop\t3.Display"); do { printf("\n\nEnter your Choice :: "); scanf("%d",&c); switch(c) { case 1: push(); break; case 2: pop(); break; case 3: printf("\n\nContents of stack is \t"); display(); break; default: printf("\nInvalid Choice......"); exit(0); } }while(c<4); getch(); } void push() { if(top>=size) { printf("\nStack Overflow"); return; } else { printf("\nEnter the number to be pushed into the stack :: "); scanf("%d",&b); top++; stack[top]=b; printf("\nNumber pushed is %d",stack[top]); return; } } void pop() { if(top==0) { printf("\nStack Underflow"); return; } else { 16 res=stack[top]; top--; printf("\nDeleted element is %d",res); return; } } void display() { int i; if(top==0) { printf("\nStack Underflow"); return; } for(i=top;i>0;i--) printf("%d< - ",stack[i]); } OUTPUT: 17 RESULT: Thus the array implementation of stack ADT has been implemented and executed. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 18 Ex. No: 4b Date: ARRAY IMPLEMENTATION OF QUEUE AIM: To write a C program to implement queue using array data structure and perform the following operations 1. Enqueue 2. Dequeue 3. Display ALGORITHM: Step 1: Start Step 2: Declare the required variable and functions Step 3: Read the options using variable op Step 4: Check the while loop condition until the conditions becomes false. if conditions is true execute Step 5 and repeat the step 4. If a condition is false go to step6 and execute Step 5: Using switch case statement evaluate the c variable If case 1 is true execute the insertions functions and terminate the switch statement. If case 2 is true execute the delete functions and terminate the switch statement. If case 3 is true execute the display functions and terminate the switch statement If case 4 is true execute the exit functions and terminate program. Step 6: Stop INSERTION FUNCTION Step 1: Start Step 2: If Rear is equal to max-1 Print Queue is overflow you can not insert Else Read item Queue [rear] =item Rear++ Step 3: Stop DELETE FUNCTION Step 1: Start Step 2: If Front is equal Rear or front is equal to -1 Print Queue is under flow you cannot delete Else Print Deleted data is Queue [front] Queue [Front] =0 Front++ Step 3: Stop 19 DISPLAY FUNCTION Step 1: Start Step 2: Initialize i value as zero. Step 3: Check i value is less than the 4. if condition is true executes step 4 and increment i value Repeat the step 3. If condition is false execute the step 5. Step 4: Print Queue[i] Step 5: Stop PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define max 20 void insert( ); void del( ); void display( ); int queue[max]; int rear= -1,front= 0; void main( ) { int op; clrscr( ); while(1) { printf("\n Array Implementation of Queue\n"); printf("\n 1.Insert\n 2.Delete\n 3.Display\n 4.Exit\n"); printf("\nEnter your option: "); scanf("%d",&op); switch(op) { case 1: insert( ); break; case 2: del( ); break; case 3: display( ); break; case 4: exit(0); } } getch(); } void insert( ) { int item; printf("\nEnter item : "); scanf("%d",&item); 20 if(rear==max-1) { printf("\nQueue is full"); return; } rear++; queue[rear]=item; } void del( ) { if( front>rear || front == -1 ) { printf("\nQueue is empty"); return; } printf("\nDeleted item is: %d", queue[front]); front++; } void display( ) { int i; printf("\nThe items are:\n "); for(i=front;i<=rear;i++) printf(" %d",queue[i]); } OUTPUT: 21 RESULT: Thus the array based implementation of queue ADT has been executed. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 22 Ex. No: 5.a LINKED LIST IMPLEMENTATIONS OF STACK ADT Date: AIM: To develop a C program using linked list to implement stack ADT. ALGORITHM: Step 1: Start the program Step 2: Declare variables and functions top=NULL, push (), pop () and display (). Step 3: Read the options from the variable op. Step 4: Using switch case statement evaluate the op variable If case 1 is true execute the push functions and terminate the switch statement. If case 2 is true execute the pop functions and terminate the switch statement. If case 3 is true execute the display and terminate the switch statement. If case 4 is true execute the exit functions and terminate the switch statement. Step 5: Stop the program PUSH FUNCTION: Step 1: Start Step 2: Read the data by using the variable item. Step 3: Create new node p and allocate memory to new node using malloc function Step 4: Read p->info Step 5: If top equal to NULL Top = p Else p->link=top top = p Step 6: Stop POP FUNCTION: Step 1: Start Step 2: Check whether the top is equal to NULL. if condition is true print stack is empty. Step 3: If the above condition is false delete top node Step 4: Assign top link field into the variable top Step 5: Stop DISPLAY FUNCTION: Step 1: Start Step 2: Create a Temp node named p and assign p =Top Step 3: Check p is not equal to NULL. If condition is true execute the step 4 until the conditions become False. If a condition is false go to step 5. Step 4: Print the p->info p = p->link Step 5: Stop 23 PROGRAM: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct stack { int info; struct stack *next; }*top=NULL, *p; void push(); void pop(); void display(); void main() { int n; clrscr(); while(1) { printf("\n----------STACK USING LINKED LIST------------"); printf("\n\n\n 1.push\n 2.pop\n 3.display\n 4.exit\n"); printf("\nEnter the option: "); scanf("%d",&n); switch(n) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); } } getch(); } void push() { int item; printf("\nEnter item : "); scanf("%d",&item); p=(struct stack*)malloc(sizeof(struct stack)); p->info=item; p->next=top; top=p; } void pop() { if(top==NULL) 24 { printf("\nstack is empty"); return; } printf("\nDeleted item is: %d",top->info); top=top->next; } void display() { printf("\nThe items are : \n"); for(p=top;p->next!=NULL;p=p->next) printf("%d -> ", p->info); printf("%d",p->info); } OUTPUT: RESULT: Thus the linked list implementation stack program was written and output was verified. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 25 Ex. No: 5.b LINKED LIST IMPLEMENTATION OF QUEUE ADT Date: AIM: To write a C program to implement the linked list implementation of Queue ADT. ALGORITHM: Step 1: Declare and initialize the Queue header q. Step 2: Define the structure for node. Step 3: To enqueue an element into the queue Case 1: Assign first node as p. Case 2: Traverse the p till last node. Case 3: Create a new node and save the value Case 4 : Assign the new node to the previous node’s next pointer Step 4: To dequeue an element from the queue Check whether the header’s next pointer is NULL, Case 1: If yes, print QUEUE IS EMPTY Case 2: Else, assign header to temp variable and temp’s next to header Case 3: Deallocate the memory of temp variable using free(). Step 5: Stop the program. PROGRAM: #include<stdio.h> #include<stdlib.h> struct node; typedef struct node* queue; typedef struct node* position; struct node { int element; position next; }; void enqueue(int x,queue q) { position tempcell,p; p=q; while(p->next!=NULL) p=p->next; tempcell=malloc(sizeof(struct node)); tempcell->element=x; tempcell->next=p->next; p->next=tempcell; } void dequeue(queue q) { position temp; if(q->next==NULL) 26 printf("Queue is empty\n"); else { temp=q->next; q->next=temp->next; free(temp); } } void display(queue q) { position p; p=q->next; while(p!=NULL) { printf("%d->",p->element); p=p->next; } } void main() { int ch,ele; queue q=malloc(sizeof(struct node)); printf("----------------------------------\n"); printf(" QUEUE \n"); printf("----------------------------------\n"); q->next=NULL; do { printf("1.ENQUEUE\n"); printf("2.DEQUEUE\n"); printf("3.Display\n"); printf("4.Exit\n"); printf("Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the element to be insert:"); scanf("%d",&ele); enqueue(ele,q); break; case 2: dequeue(q); break; case 3: display(q); break; default: break; } }while(ch<4); } 27 OUTPUT: RESULT: Thus the C program to implement the linked list implementation of Queue ADT was written and executed successfully. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 28 Ex. No: 6 Date: CONVERSION OF INFIX TO POSTFIX EXPRESSION AIM: To write a C program to convert the given infix expression into postfix expression using Stack ADT. ALGORITHM: Step 1: Declare an array and initialize top=-1. Step 2: Get the infix expression from the user. Step 3: Read the expression character by character until the end Step 4: If the expression contains character, append it to the postfix expression. Step 5: If the expression contains operator, insert it into the stack using push().If the precedence of the operator at the top of the stack is greater than the precedence of the input operator, then pop out the top element from the stack and append it to the postfix expression. Step 6: If the character is an “(“, push it onto the stack. Step 7: If the character is an “)”, pop out all the operators from the stack until an “(“is found. Step 8: Pop out all the operators from the stack to postfix expression until the stack is empty. Step 9: Stop the program. PROGRAM: #include<stdio.h> #define SIZE 50 #include <ctype.h> char s[SIZE]; int top = -1; push(char elem) { s[++top] = elem; } char pop() { return (s[top--]); } int pr(char elem) { switch (elem) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*': 29 case '/': return 3; } } void main() { char infx[50], pofx[50], ch, elem; int i = 0, k = 0; printf("\n\nRead the Infix Expression ? "); scanf("%s",infx); push('#'); while ((ch = infx[i++]) != '\0') { if (ch == '(') push(ch); else if (isalnum(ch)) pofx[k++] = ch; else if (ch == ')') { while (s[top] != '(') pofx[k++] = pop(); elem = pop(); } else { while (pr(s[top]) >= pr(ch)) pofx[k++] = pop(); push(ch); } } while (s[top] != '#') pofx[k++] = pop(); pofx[k] = '\0'; printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx); getch(); } OUTPUT: 30 RESULT: Thus the C program to convert the given infix expression into postfix expression using Stack ADT has been executed successfully. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 31 Ex. No: 7 IMPLEMENTATION OF BINARY TREES Date: AIM: To write a C program to implement binary tree and traverse through the binary tree. ALGORITHM: Step 1: Start the program Step 2: Create Structure named as node, structure members’ data, left and right Step 3: Declare and define the following functions insert(), inorder(), preorder(), postorder(). Step 4: Using switch case statement evaluate the c variable Case 1: If it is true execute the method inorder Case 2: If it is true invoke and execute the method preorder Case 3: If it is true execute the method postorder Case 4: If it is true Exit from the Switch case Step 5: Stop the program Preorder Function: Step 1: Repeat steps 2 to 4 while tree != null Step 2: Write tree->data Step 3: preorder(tree->left) Step 4: preorder(tree->right) [End of loop] Step 5: End Inorder Function: Step 1: Repeat steps 2 to 4 while tree != null Step 2: inorder(tree->left) Step 3: Write tree->data Step 4: inorder(tree->right) [End of loop] Step 5: End Postorder Function: Step 1: Repeat steps 2 to 4 while tree != null Step 2: postorder(tree->left) Step 3: postorder(tree->right) Step 4: Write tree->data [End of loop] Step 5: End 32 PROGRAM: #include<stdio.h> #include<stdlib.h> typedef struct tree *node; node insert(int,node T); 1. void inorder(node T); void preorder(node T); void postorder(node T); struct tree { int data; struct tree *right,*left; }*root; int main() { node T=NULL; int data,ch,i=0,n; printf("\n Enter the no of elements in the tree \n"); scanf("%d",&n); printf("\n The elements are: \n"); while(i<n) { scanf("%d",&data); T=insert(data,T); i++; } printf("\n 1.Inorder \n 2.Preorder \n 3.Postorder \n 4.Exit \n"); do { printf("\n Enter the choice \n"); 33 scanf("%d",&ch); switch(ch) { case 1: printf("\n Inorder traversal \n"); inorder(T); break; case 2: printf("\n Preorder traversal \n"); preorder(T); break; case 3: printf("\n Postorder traversal \n"); postorder(T); break; case 4: exit(0); break; } } while(ch<=4); } node insert(int x,node T) { struct tree *newnode; newnode=malloc(sizeof(struct tree)); if(T==NULL) { newnode->data=x; newnode->left=NULL; newnode->right=NULL; T=newnode; } 34 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) { 35 if(T!=NULL) { postorder(T->left); postorder(T->right); printf("%d \t",T->data); } } OUTPUT: 36 RESULT: Thus the C program to implement the binary tree and its traversals was successfully executed. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty Ex. No: 8 Date: IMPLEMENTATION OF BINARY SEARCH TREES AIM: To develop a C program to construct binary search tree and implement it using linked list with possible operations. ALGORITHM: Step 1: Start the program Step 2: Create Structure named as node, structure members’ reg, left and right Step 3: Declare and define the following functions insert(), delete(), search(), view tree(), min() and max(). Step 4: Using switch case statement evaluate the c variable Case 1: If it is true execute the method insert and display the created tree Case 2: If it is true invoke and execute the method deleteNode and display the tree after deletion Case 3: If it is true execute the method search Case 4: If it is true execute the view tree method to view the tree Case 5: If it is true Exit from the Switch case Step 5: Stop the program PROGRAM: #include<stdio.h> #include<conio.h> 37 struct node { int reg; struct node *left,*right; }; typedef struct node Tree; Tree *tree; int d=0,Loc=0; Tree* findMin(Tree *); Tree* findMax(Tree *); /* Create New Node */ Tree* makeNode(int val) { Tree *t=(Tree *)malloc(sizeof(Tree)); t->reg=val; t->left=NULL; t->right=NULL; return t; } Tree* insert(Tree *L) { Tree *t1,*t2; if(L==NULL) { printf("\nEnter Data:"); scanf("%d",&d); L=makeNode(d); } else { printf("\nEnter Data:"); scanf("%d",&d); t1=L; t2=L; while(d!=t1->reg&&t2!=NULL) { t1=t2; if(d<t1->reg) t2=t1->left; if(d>t1->reg) t2=t1->right; } if(d==t1->reg) { printf("\n Data Already There\n"); } else { if(d<t1->reg) t1->left=makeNode(d); if(d>t1->reg) t1->right=makeNode(d); 38 } } return L; } Tree* deleteNode(Tree *L,int d) { Tree *q; if(L==NULL) { printf("\nDelete Element Not Found !!!\n\n"); } else { if(d<L->reg) L->left=deleteNode(L->left,d); else if(d>L->reg) L->right=deleteNode(L->right,d); else if(L->left&&L->right) { q=findMin(L->right); L->reg=q->reg; L->right=deleteNode(L->right,L->reg); } else { q=L; if(L->left==NULL) L=L->right; if(L->right==NULL) L=L->left; free(q); printf("\nElement Deleted Successfully\n\n"); } } return L; } int Find(Tree *L,int d) { if(L!=NULL) { loc++; if(d<L->reg) Find(L->left,d); else if(d>L->reg) Find(L->right,d); else if(d==L->reg) printf("\nData Found At:%d",Loc); return 0; } printf("\nData Not Found"); return 0; } 39 void display(Tree *L) { if(L!=NULL) { display(L->left); printf("\t%d",L->reg); display(L->right); } } void viewtree(struct node *t,int x,int y) { if(t==NULL)return; gotoxy(x,y); printf("%d",t->reg); viewtree(t->left,x-5,y+1); viewtree(t->right,x+5,y+1); } void freetree(struct node *t) { if(t==NULL)return; if(t->left!=NULL)freetree(t->left); if(t->right!=NULL)freetree(t->right); t->right=NULL;t->left=NULL;t=NULL;t->reg=NULL; free(t); } void main() { int c,d; Tree *temp; clrscr(); tree=(Tree *)malloc(sizeof(Tree)); tree=NULL; while(1) { printf("\n1.Insert\n2.Delete\n3.Search\n4.View Tree"); printf("\n5.Exit\n\nEnter Your Choice:"); scanf("%d",&c); switch(c) { case 1: tree=insert(tree); printf("\n"); display(tree); break; case 2: printf("\nEnter Data to be deleted:"); deleteNode(tree,d); display(tree); break; case 3: printf("\nEnter Data to be searched:"); scanf("%d",&d); 40 loc=0; find(tree,d); break; case 4: clrscr(); printf("\nTree Structure\n"); viewtree(tree,40,5); break; case 5: exit(0); } getch(); clrscr(); } } Tree* findMin(Tree *L) { if(L->left==NULL)return L; L->left=findMin(L->left); } /* To be find Maximum Value */ Tree * findMax(Tree *L) { if(L->right==NULL)return L; L->right=findMax(L->right); } OUTPUT: 41 RESULT: Thus the C program for Binary search tree implementation using linked list was constructed and executed successfully. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty Ex. No: 9 IMPLEMENTATION OF AVL TREES Date: AIM: To write a C program to construct an AVL tree and implement the operations of the tree. ALGORITHM: Step 1: Start the program. Step 2: The height of the left and right sub trees can differ by almost 1. A balance factor is the height of the left sub tree minus height of the right sub tree. Step 3: If the balance factor of any node in an AVL tree becomes less than -1 or greater than 1, the tree has to be balanced either single or double rotations. Step 4: If(T==NULL) , then create the size of the tree using malloc(sizeof(node) otherwise insert the nodes in the tree using *insert(node *,int); Step 5: If(x > T->data), then insert the data into the right subtree otherwise insert the data into the left sub tree otherwise it delete the node in the left subtree. Step 6: *Delete(node *,int) is used to delete the element form the tree. if(x > T->data) it matches, it delete the node in the right subtree. Step 7: Rotateright(node *) is used to rotate the element to the right position and rotateleft(node *) is used to rotate the element to the left position . RR means right rotation, LL means left rotation, LR means left rotation, RL means right rotation. Step 8: Display the elements in 2 ways such as preorder and inorder using preorder(node *), inorder(node *); Step 9: Stop the program. 42 PROGRAM: #include<stdio.h> typedef struct node { int data; struct node *left,*right; int ht; }node; node *insert(node *,int); node *Delete(node *,int); void preorder(node *); void inorder(node *); int height( node *); node *rotateright(node *); node *rotateleft(node *); node *RR(node *); node *LL(node *); node *LR(node *); node *RL(node *); int BF(node *); int main() { node *root=NULL; int x,n,i,op; do { printf("\n1)Create:"); printf("\n2)Insert:"); printf("\n3)Delete:"); printf("\n4)Print:"); printf("\n5)Quit:"); printf("\n\nEnter Your Choice:"); scanf("%d",&op); switch(op) { case 1: printf("\nEnter no. of elements:"); scanf("%d",&n); printf("\nEnter tree data:"); root=NULL; for(i=0;i<n;i++) { scanf("%d",&x); root=insert(root,x); } break; case 2: printf("\nEnter a data:"); scanf("%d",&x); root=insert(root,x); break; case 3: printf("\nEnter a data:"); scanf("%d",&x); 43 root=Delete(root,x); break; case 4: printf("\nPreorder sequence:\n"); preorder(root); printf("\n\nInorder sequence:\n"); inorder(root); printf("\n"); break; } }while(op!=5); return 0; } node * insert(node *T,int x) { if(T==NULL) { T=(node*)malloc(sizeof(node)); T->data=x; T->left=NULL; T->right=NULL; } else if(x > T->data) // insert in right subtree { T->right=insert(T->right,x); if(BF(T)==-2) if(x>T->right->data) T=RR(T); else T=RL(T); } else if(x<T->data) { T->left=insert(T->left,x); if(BF(T)==2) if(x < T->left->data) T=LL(T); else T=LR(T); } T->ht=height(T); return(T); } node * Delete(node *T,int x) { node *p; if(T==NULL) { return NULL; } 44 else if(x > T->data) // insert in right subtree { T->right=Delete(T->right,x); if(BF(T)==2) if(BF(T->left)>=0) T=LL(T); else T=LR(T); } else if(x<T->data) { T->left=Delete(T->left,x); if(BF(T)==-2) //Rebalance during windup if(BF(T->right)<=0) T=RR(T); else T=RL(T); } else { //data to be deleted is found if(T->right!=NULL) { //delete its inorder succesor p=T->right; while(p->left!= NULL) p=p->left; T->data=p->data; T->right=Delete(T->right,p->data); if(BF(T)==2)//Rebalance during windup if(BF(T->left)>=0) T=LL(T); else T=LR(T);\ } else return(T->left); } T->ht=height(T); return(T); } int height(node *T) { int lh,rh; if(T==NULL) return(0); if(T->left==NULL) lh=0; else lh=1+T->left->ht; 45 if(T->right==NULL) rh=0; else rh=1+T->right->ht; if(lh>rh) return(lh); return(rh); } node * rotateright(node *x) { node *y; y=x->left; x->left=y->right; y->right=x; x->ht=height(x); y->ht=height(y); return(y); } node * rotateleft(node *x) { node *y; y=x->right; x->right=y->left; y->left=x; x->ht=height(x); y->ht=height(y); return(y); } node * RR(node *T) { T=rotateleft(T); return(T); } node * LL(node *T) { T=rotateright(T); return(T); } node * LR(node *T) { T->left=rotateleft(T->left); T=rotateright(T); return(T); } node * RL(node *T) { T->right=rotateright(T->right); T=rotateleft(T); return(T); } 46 int BF(node *T) { int lh,rh; if(T==NULL) return(0); if(T->left==NULL) lh=0; else lh=1+T->left->ht; if(T->right==NULL) rh=0; else rh=1+T->right->ht; return(lh-rh); } void preorder(node *T) { if(T!=NULL) { printf("%d(Bf=%d)",T->data,BF(T)); preorder(T->left); preorder(T->right); } } void inorder(node *T) { if(T!=NULL) { inorder(T->left); printf("%d(Bf=%d)",T->data,BF(T)); inorder(T->right); } } OUTPUT: 47 RESULT: Thus the AVL tree has been constructed and implemented using C. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty Ex. No: 10 Date: IMPLEMENTATION OF HEAPS USING PRIORITY QUEUES AIM: To write a C program to implement priority queue using binary heaps. ALGORITHM: Step 1: Start the process. Step 2: Initialize all necessary variables and functions. Step 3: Read the choices. Step 4: For insertion, read the element to be inserted. Step 5: If root is NULL, assign the given element as root. Step 6: If the element is equal to the root, print “Duplicatevalue”. Step 7: Else insert right side of the root. Step 8: For deletion, get the priority for maximum or minimum. Step 9: If maximum, it deletes the root and rearranges the tree. Step 10: If minimum, it deletes the leaf. Step 11: End of the program. PROGRAM: #include<stdio.h> #include<conio.h> #include <stdlib.h> enum {FALSE=0,TRUE=-1}; struct Node { 48 struct Node *Previous; int Data; struct Node *Next; }Current; struct Node *head; struct Node *ptr; static int NumOfNodes; int PriorityQueue(void); int Maximum(void); int Minimum(void); void Insert(int); int Delete(int); void Display(void); int Search (int); void main() { int choice; int DT; clrscr(); PriorityQueue(); while(1) { printf("\nEnter ur Choice:"); printf("\n1.Insert\n2.Display\n3.Delete\n4.Search\n5.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter a data to enter Queue"); scanf("%d",&DT); Insert(DT); break; case 2: Display(); break; case 3: { int choice,DataDel; printf("\nEnter ur choice:"); printf("\n1.Maximum Priority queue\n2.Minimum priorityQueue\n"); scanf("%d",&choice); switch(choice) { case 1:DataDel=Maximum(); Delete(DataDel); printf("\n%d is deleted\n",DataDel); break; case 2:DataDel=Minimum(); Delete(DataDel); printf("\n%d is deleted\n",DataDel); break; default: 49 printf("\nSorry Not a correct Choice\n"); }} break; case 4:printf("\nEnter a data to Search in Queue:"); scanf("%d",&DT); if(Search(DT)!=FALSE) printf("\n %d is present in queue",DT); else printf("\n%d is not present in queue",DT); break; case 5: exit(0); default: printf("\nCannot process ur choice\n"); }}} int PriorityQueue(void) { Current.Previous=NULL; printf("\nEnter first element of Queue:"); scanf("%d",&Current.Data); Current.Next=NULL; head=&Current;ptr=head; NumOfNodes++; return; } int Maximum(void) { int Temp; ptr=head; Temp=ptr->Data; while(ptr->Next!=NULL) { if(ptr->Data>Temp) Temp=ptr->Data; ptr=ptr->Next; } if(ptr->Next==NULL && ptr->Data>Temp) Temp=ptr->Data; return(Temp); } int Minimum(void) { int Temp ;ptr=head; Temp=ptr->Data; while(ptr->Next!=NULL) { if(ptr->Data<Temp) Temp=ptr->Data ;ptr=ptr->Next; } if(ptr->Next==NULL && ptr->Data<Temp) 50 Temp=ptr->Data; return(Temp); } void Insert(int DT) { struct Node *newnode; newnode=(struct Node *)malloc(sizeof(struct Node)); newnode->Next=NULL; newnode->Data=DT; while(ptr->Next!=NULL)ptr=ptr->Next; if(ptr->Next==NULL){newnode->Next=ptr->Next; ptr->Next=newnode;}NumOfNodes++; } int Delete(int DataDel) { struct Node *mynode,*temp; ptr=head; if(ptr->Data==DataDel) { temp=ptr; ptr=ptr->Next; ptr->Previous=NULL; head=ptr; NumOfNodes--; return(TRUE); } else { while(ptr->Next->Next!=NULL) { if(ptr->Next->Data==DataDel) { mynode=ptr; temp=ptr->Next; mynode->Next=mynode->Next->Next; mynode->Next->Previous=ptr; free(temp); NumOfNodes--; return(TRUE); } ptr=ptr->Next; } if(ptr->Next->Next==NULL && ptr->Next->Data==DataDel) { temp=ptr->Next; free(temp);ptr->Next=NULL; NumOfNodes--; return (TRUE); }} return(FALSE); } int Search(int DataSearch) 51 { ptr=head; while(ptr->Next!=NULL) { if(ptr->Data==DataSearch)return ptr->Data; ptr=ptr->Next; } if(ptr->Next==NULL && ptr->Data==DataSearch)return ptr->Data;return(FALSE); } void Display(void) { ptr=head; printf("\nPriority Queue is as Follows:-\n"); while(ptr!=NULL) { printf("\t\t%d",ptr->Data); ptr=ptr->Next; } } OUTPUT: 52 RESULT: Thus the C program to implement priority queue using binary heaps has been executed successfully. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty Ex. No: 11 Date: GRAPH REPRESENTATION AND TRAVERSAL ALGORITHMS – BFS AND DFS AIM: To write a C program to implement the graph traversal algorithms using breadth first search and depth first search. ALGORITHM: BFS: Step 1: SET STATUS = 1 (ready state) for each node in G Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state) Step 3: Repeat Steps 4 and 5 until QUEUE is empty Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state). Step 5: Enqueue all the neighbors of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state) [End of loop] Step 6: End DFS: Step 1: SET STATUS = 1 (ready state) for each node in G Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state) Step 3: Repeat Steps 4 and 5 until STACK is empty Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state) Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state) 53 [End of loop] Step 6: End PROGRAM: #include<conio.h> #include<stdio.h> # define MAX 20 int q[ MAX ], top = -1, front = -1, rear = -1, a[ MAX ][ MAX ], vis[ MAX ], stack[ MAX ]; int delete(); void add ( int item ); void bfs( int s, int n ); void dfs( int s, int n ); void push( int item ); int pop(); int main() { int n, i, s, ch, j; char c; char dummy; clrscr(); printf( "Enter the number of vertices: "); scanf( "%d", &n ); printf( "Enter Adjacency Matrix: \n"); for ( i = 0;i < n;i++ ) { for ( j = 0;j < n;j++ ) { scanf( "%d",&a[i][j]); } } printf( "The Matrix is "); for ( i = 0;i < n;i++ ) { printf("\n"); for ( j = 0;j < n;j++ ) { printf( " %d", a[ i ][ j ] ); } } do { for ( i = 1;i <= n;i++ ) { vis[ i ] = 0; } printf( "\nMENU" ); printf( "\n1.BFS\n2.DFS" ); printf( "\nEnter Your Choice: " ); scanf( "%d", &ch ); printf( "Enter the source vertex: " ); scanf( "%d", &s ); 54 switch ( ch ) { case 1: bfs( s, n ); break; case 2: dfs( s, n ); break; } printf( "\nDo You Want To Continue? (Y/N)" ); scanf( "%c", &dummy ); scanf( "%c", &c ); } while ( (c == 'y' ) ); return 0; } void bfs( int s, int n ) { int p, i; add(s); vis[s] = 1; p = delete(); if ( p != 0 ) { printf( " %d", p ); } while ( p != 0 ) { for ( i = 1;i <= n;i++ ) if ( ( a[ p ][ i ] == 1 ) && ( vis[ i ] == 0 ) ) { add( i ); vis[ i ] = 1; } p = delete(); if ( p != 0 ) printf( " %d ", p ); } for ( i = 1;i <= n;i++ ) if ( vis[ i ] == 0 ) bfs( i, n ); } void add(int item) { if ( rear == MAX-1 ) printf( "\nQUEUE FULL" ); else { if ( rear == -1 ) { q[ ++rear ] = item; 55 front++; } else q[ ++rear ] = item; } } int delete() { int k; if ( ( front > rear ) || ( front == -1 ) ) return ( 0 ); else { k = q[ front++ ]; return ( k ); } } void dfs( int s, int n ) { int i, k; push( s ); vis[ s ] = 1; k = pop(); if ( k != 0 ) printf( " %d ", k ); while ( k != 0 ) { for ( i = 1;i <= n;i++ ) if ( ( a[ k ][ i ] == 1 ) && ( vis[ i ] == 0 ) ) { push( i ); vis[ i ] = 1; } k = pop(); if ( k != 0 ) printf( " %d ", k ); } for ( i = 1;i <= n;i++ ) { if ( vis[ i ] == 0 ) dfs( i, n ); } } void push( int item ) { if ( top == MAX-1 ) printf( "Stack overflow " ); else 56 stack[++top] = item; } int pop() { int k; if ( top == -1 ) { return ( 0 ); } else { k = stack[top--]; return(k); } } OUTPUT: 57 RESULT: Thus the graph traversal algorithms breadth-first search and depth-first search has been implemented and executed. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty Ex. No: 12 Date: APPLICATIONS OF GRAPH – DIJKSTRA’S ALGORITHM AIM: To write a C program to find the shortest path in a graph using Dijikstra’s algorithm. ALGORITHM: Step 1: Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes. Step 2: Mark all nodes unvisited. Set the initial node as current. Create a set of the unvisited nodes called the unvisited set consisting of all the nodes except the initial node. Step 3: For the current node, consider all of its unvisited neighbors and calculate their tentative distances. Step 4: After considering all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again. Step 5: If the destination node has been marked visited or if the smallest tentative distance among the nodes in the unvisited set is infinity then stop. The algorithm has finished. Step 6: Select the unvisited node that is marked with the smallest tentative distance, and set it as the new “current node” then go back to step 3. PROGRAM: #include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> 58 #include<math.h> #define IN 99 #define N 6 int dijkstra(int cost[][N], int source, int target); int dijsktra(int cost[][N],int source,int target) { int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j; char path[N]; for(i=1;i< N;i++) { dist[i] = IN; prev[i] = -1; } start = source; selected[start]=1; dist[start] = 0; while(selected[target] ==0) { min = IN; m = 0; for(i=1;i< N;i++) { d = dist[start] +cost[start][i]; if(d< dist[i]&&selected[i]==0) { dist[i] = d; prev[i] = start; } if(min>dist[i] && selected[i]==0) { min = dist[i]; m = i; } } start = m; selected[start] = 1; } start = target; j = 0; while(start != -1) { path[j++] = start+65; start = prev[start]; } path[j]='\0'; strrev(path); printf("%s", path); return dist[target]; } int main() { int cost[N][N],i,j,w,ch,co; 59 int source, target,x,y; printf("\tShortest Path Algorithm(DIJKSRTRA's ALGORITHM\n\n"); for(i=1;i< N;i++) for(j=1;j< N;j++) cost[i][j] = IN; for(x=1;x< N;x++) { for(y=x+1;y< N;y++) { printf("Enter the weight of the path between node %d and %d: ",x,y); scanf("%d",&w); cost [x][y] = cost[y][x] = w; } printf("\n"); } printf("\nEnter The Source:"); scanf("%d", &source); printf("\nEnter The target"); scanf("%d", &target); co = dijsktra(cost,source,target); printf("\nShortest Path: %d",co); getch(); } OUTPUT: 60 RESULT: Thus the C program to find the shortest path using Dijkstra’s algorithm had been executed successfully. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty Ex. No: 13a Date: BINARY SEARCH AIM: To write a C program to search for the given element in an array using binary search. ALGORITHM: Step 1: Read the elements of the array. Step 2: Initialize the first and last integers to array[0] and array[n-1]. Step 3: Compute the middle value. Step 4: Get the element to search in the array. Step 5: Check whether the search element is in the middle. If yes, print the middle position and exit. Else go to Step 6. Step 6: Check if search element is lesser than the middle element. If yes, then change the last to middle-1 and go to Step 3. Else go to Step 7. Step 7: Check if search element is greater than the middle value. If yes, then change the first value to middle+1 and go to Step 3. PROGRAM: #include<stdio.h> #include<conio.h> void main() { int i, first, last, middle, n, search, array[100]; clrscr(); printf("\nEnter number of elements:"); scanf("%d",&n); 61 printf("\nEnter %d integers in sorted order:", n); for ( i = 0 ; i < n ; i++ ) scanf("%d",&array[i]); printf("\nEnter value to search:"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if(first > last ) printf("Not found! %d is not present in the list.\n", search); getch(); } OUTPUT: RESULT: Thus the C program to search for the given element using binary search technique had been implemented successfully. 62 MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty Ex. No: 13b Date: SHELL SORT AIM: To write a C program to sort the given elements using shell sort algorithm. ALGORITHM: Step 1: Read the number of elements for the array. Step 2: Read the elements of the array. Step 3: Select the increment value as 3 initially. Step 4: Starting with first element, consider only those elements that come under multiples of increment value. Step 5: Sort these elements and put them back in to their respective places in the original array. Step 6: Decrement the value of increment by 1 and go to Step 4. Step 7: Display the sorted elements. PROGRAM: #include<stdio.h> #include<conio.h> void shell(int a[],int n); int i,j,k,n,temp,array[25]; void main() { clrscr(); printf("\n SHELL SORT"); 63 printf("\n Enter the limit:"); scanf("%d",&n); printf("\n Enter the elements:\n\n"); for(i=0;i<n;i++) scanf("%d",&array[i]); shell(array,n); printf("\n Sorted list:"); for(i=0;i<n;i++) printf("\n %d",array[i]); getch(); } void shell(int a[],int n) { for(i=(n+1)/2;i>=1;i/=2) { for(j=i;j<=n-1;j++) { temp=a[j]; k=j-i; while(k>=0&&temp<a[k]) { a[k+i]=a[k]; k=k-i; } a[k+i]=temp; } } } OUTPUT: RESULT: 64 Thus the C program for implementing shell sort had been executed successfully. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty Ex. No: 14 IMPLEMENTATION OF HASHING – COLLISION RESOLUTION Date: TECHNIQUES AIM: To write a C program to implement hash functions and the collision resolution techniques such as linear probing and quadratic probing. ALGORITHM: Step1: Initialize all necessary variables and functions. Step2: Read the number to be inserted in the hash table. Step3: Find the location to insert using num % size. Step4: If any value is present at that position then find the next unoccupied location. Step5: Else if key value is greater than the maximum table size then print “Hash Table is full”. Step6: Repeat the steps 1 to 5 to insert more values. Step7: End of the program. PROGRAM: #include <stdio.h> #include <conio.h> int tsize; int hasht(int key) { int i ; 65 i = key%tsize ; return i; } //-------LINEAR PROBING------int rehashl(int key) { int i ; i = (key+1)%tsize ; return i ; } //-------QUADRATIC PROBING------int rehashq(int key, int j) { int i ; i = (key+(j*j))%tsize ; return i ; } void main() { int key,arr[20],hash[20],i,n,s,op,j,k ; clrscr() ; printf ("Enter the size of the hash table: "); scanf ("%d",&tsize); printf ("\nEnter the number of elements: "); scanf ("%d",&n); for (i=0;i<tsize;i++) hash[i]=-1 ; printf ("Enter Elements: "); for (i=0;i<n;i++) { scanf("%d",&arr[i]); } do { printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option: "); scanf("%d",&op); switch(op) { case 1: for (i=0;i<tsize;i++) hash[i]=-1 ; for(k=0;k<n;k++) { key=arr[k] ; i = hasht(key); while (hash[i]!=-1) { 66 i = rehashl(i); } hash[i]=key ; } printf("\nThe elements in the array are: "); for (i=0;i<tsize;i++) { printf("\n Element at position %d: %d",i,hash[i]); } break ; case 2: for (i=0;i<tsize;i++) hash[i]=-1 ; for(k=0;k<n;k++) { j=1; key=arr[k] ; i = hasht(key); while (hash[i]!=-1) { i = rehashq(i,j); j++ ; } hash[i]=key ; } printf("\nThe elements in the array are: "); for (i=0;i<tsize;i++) { printf("\n Element at position %d: %d",i,hash[i]); } break ; } }while(op!=3); getch() ; } OUTPUT: 67 RESULT: Thus the C program to implement hash functions and collision resolution techniques had been executed successfully. MARKS ALLOCATION Marks Marks Allotted Awarded Details Preparation 20 Program 30 Execution & Results 40 Viva 10 Total 100 Signature of the Faculty 68