0% found this document useful (0 votes)
10 views

Data Structures Lab programs

The document contains multiple C programs demonstrating various data structures and algorithms, including binary and linear search, bubble sort, insertion and selection sort, singly linked list operations, linear queue operations, and circular queue operations. Each section provides code snippets along with sample outputs for user interaction, showcasing how to insert, delete, and display elements in these data structures. The programs are designed to illustrate fundamental concepts in data structures using C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views

Data Structures Lab programs

The document contains multiple C programs demonstrating various data structures and algorithms, including binary and linear search, bubble sort, insertion and selection sort, singly linked list operations, linear queue operations, and circular queue operations. Each section provides code snippets along with sample outputs for user interaction, showcasing how to insert, delete, and display elements in these data structures. The programs are designed to illustrate fundamental concepts in data structures using C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 52
APP Data : NDIX-A ‘tructures Lab . Write a program to searci 1. acts ch for an element in an array using binary and linear 7 Binary and Linear Search */ #include int main() { int i,x.n.ch,z=0; int a[10],mid,high,low; printf (“\n Binary and Li ay printf (“1 Binary Search Soe a printf (“2.Linear Search \n"); printf (“\n Enter your choice:\n”); scanf(“od”&ch); switch(ch) { case 1: printf(“Enter total number of elements \n”); scanf(‘*ed”,&n); printf(“Enter elements in ascending order \n”); for(i=O;icnsi++) seanf(‘*ed”, Salil); printi(“Enter an element to be search \n"); scanf(“Iod” 8x); Jow=0; high=n-1; while(low<=high) { mid=(low+high)/2s if(xalmid]) low=mid+1; else t intf(“\n The element “od present at position od” x,mid+1); prin zal; break; on ‘Dara Structures Usine © printi(\n Search element not present in the set”); break; case 2: printf(“Enter total number of elements\n”); scanf(“ed”,&n); printf(“Enter the elements \n"); seanf(‘*od” fx); fort { iffali]==x) { nji++) printf(“\n The element ‘ed present at position *d”,x,i+1); zl; break; i } if printf(“\n Element not present in the set”); break; default: printf(“Invalid Choice”); } return 0; } Output Binary and Linear Search Binary and Linear Search 1 Binary Search 1 Binary Search 2.Linear Search 2.Linear Search Enter your choice: Enter your choice: 1 D Enter total number of elements 5 Enter elements in ascending order 10 20 30 40 50 Enter an element to be search 40 ‘The element 40 present at position 4 Enter total number of elements 5 Enter the elements 10 20 30 40 50 Enter an element to be search 30 The element 30 present at position 3 int main() ( int a{10],i,j,t.n; printf Bubble Sort \n”); Printf(" ---n---eon~ \n”); printf( er total number of elements \n” - 7 Z y seanf(““d",&n); a inter the elements \n"); ialj+1)) printf(“\n The sorted elements are \n”); for(i=0;i #define n 8 int main() 7 int aln]={75,8,1,16,48,3,7,0}; int ij,t.ch,min,loc; printf(“\n Input elements are: \n”); for(i=O;i=13j--) { if(alj]>alj-1)) printf(“\n Descending order using Insertion Sort \n”); for(i=0;i #include #include struct slist { int data; struct slist *next: ee struct slist nodes Cc Dara Structures Usine C node *head=NULL.*ptr,*new mode; node *createnode(); void insert(); void del(); void display; int ch,n=0,x=0,f=0,i; int main) { while(1) { printf(“\n --------Singly Linked List -~ printf(“\n 1 Insert”); printf(“\n 2.Delete”); printf(“\n 3.Display”); printf(“\n 4.Exit”); printf(‘“‘\n Enter your choice:”); scanf(“od”,&-ch); switch(ch) { case 1: if(n>=4) printf(“\n Given elements are Inserted”); else { insert(); printf(“\n List created successfully”); } break; case 2: del; break; case 3: display; break; default: return 0; ptr=head; —_ ILL) pir=ptr->next; } ptr->next=newnode; } node *createnode() { newnode = (node *) malloc(sizeof(node)); printf(“\n Enter an element to be insert :”); scanf(‘““fod”, S&newnode->data); newnode->next=NULL; return newnode; } void del() { node *prev; ptr=head; if(head=: { printf(“\n List is Empty”); } else { ”); printf(“\n Enter an element to be delete: % scanf(‘“%od”,&n); if(head->data==n) { head=head->next; free(ptr); ly”); printfa Element deleted suecessfull” } else { while((ptr->datal NULL) en) && (ptt! NULL)) Dara Structures Usine C pir=ptr->next; } if (pt==NULL) printf(“\n Given element not found in the list “); else { prev->next=ptr->next; free(ptr); printf(“\n Element deleted sucessfuly”); wy void display() { ptr=head; printf(“\n The contents of Singly Linked List are: printf(“\n Empty List”); } else { while (pt->next!=NULL) { printf(““ed->”, ptr->data); ptr=ptr->next; } printf(‘“od->” ptr->data); } printf(“NULL”); } Output ~-Singly Linked List -~ Lnsert 2.Delete 3.Display 4.Exit Enter your choice:1 Enter an element to be insert :61 List created successfully —-Singly Linked List ~ Lnsert 2.Delete 3.Display es eh pea ria a oo Sea Ee Ap a> \\ 4.Exit Enter your choice:3 The contents of Sin; 61->NULL Singly Linked List L Insert 2.Delete 3.Display 4.Exit Enter your choice: 1 Enter an element to be insert :16 List created successfully Singly Linked List Lnsert 2.Delete 3.Display 4.Exit Enter your choice:3 ‘The contents of Singly Linked List are: 61->16->NULL -Singly Linked List - Lnsert 2.Delete 3.Display 4.Exit Enter your choice: 1 Enter an element to be insert :8 List created successfully -Singly Linked List ~ Insert 2.Delete 3.Display 4.Exit . Enter your choice: = The epicais of Singly Linked List are: 61->16->8->NULL igly Linked List are: 2.Delete 3.Display 4.Exit Entei your choice: Enter an element to be i insert :27 LV nnn eee List created successfully -Singly Linked List ——-- LInsert 2.Delete 3.Display 4.Exit Enter your choice:3 The contents of Singly Linked List are: 61->16->8->27->NULL -Singly Linked List - Lnsert 2.Delete 3.Display 4.Exit Enter your choice:2 Enter an element to be delete :8 Element deleted sucessfuly —-Singly Linked List ~ L Insert 2.Delete 3.Display 4.Bxit Enter your choi <3 The contents of Singly Linked List are: 61->16->27->NULL 3.Display 4.Exit Enter your choice:2 Enter an element to be delete :61 Element deleted successfully 2.Delete 3.Display 4.Exit Enter your choice:3 The contents of Singly Linked List are: NULL 5, Write a program to insert the elements (45, 34, 10, 63, 3} into linear queue and 3.Display A.Exit Enter your choice:2 Enter an element to be di lelete : Element deleted sucessfuly a -Singly Linked List — 2.Delete 3.Display 4.Exit Enter your choice:3 The contents of Singly Linked List are: 16->NULL aa 2.Delete 3.Display 4.Exit Enter your choice:4 delete three elements from the list. Display your list after each insertion and deletion. /* Linear Queue Operations using Linked List */ #include #include #include #define size 5 struct node { int info; struct node *link; }*head,*front,*reat,* PU void insert); int del(); void display(): int ch,item,n=0,i; int main) { while(1) it - | printf(“n Queve Ope! rations’ ee printf(“\n 2.Delete”); printf(“\n 3.Display”); printf(“\n 4.Exit”); printf(“\n Enter your choice :”); scanf(“led” ech); switch(ch) { case 1: if (n>=size) printf(‘“Queue is Full”); else { insert0; printf(“\n List created successfully”); } break; case 2: del(); break; case 3: display; default: return 0; } } void insert() { printf(*\n Enter an element to be insert: scanf(““od” éeitem); pir=(struct node *) malloc(sizeof(struct node)); ptr->info=item; ptr>link-NULL; if(rear==NULL) { front=ptr; natty } int del() if(front==NULL) printf("\n Queue is Empty \n"); else if(front==rear) front=rear=NULL; else front=front->link; n=ptr->info; free(ptr); printf(“\n Deleted element is:%d \n”,n); return(0); } void display() { ptr=front; printf(“\n Queue elements are: \n”); if(front==NULL) printf(“\n Queue is Empty \n"); while(ptr!=NULL) { printf(“‘Sed->”,ptr->info); pir=ptr>link; } printf(*NULL"); D Output J Queue Operations —— 2.Delete 3.Display 4.Exit Enter your choice :3 Queue elements are: 45->NULL Queue Operations L Insert 2.Delete 3.Display 4Exit Enter your choice :1 Enter an element to be insert:34 List created successfully Queue Operations 1 Insert 2.Delete 3.Display 4.Exit Enter your choice :3 Queue elements are: 45->34->NULL Queue Operations 1.Insert 2.Delete 3.Display 4.Exit Enter your choice :1 Enter an element to be insert:10 List created successfully Queue Operations Data Structures Usinc C) Enter your choice :3 Queue elements are: 45->34->10->NULL Queue Operations LInsert 2.Delete 3.Display 4.Exit Enter your choice :1 Enter an element to be insert:63 List created successfully Queue Operations Lnsert 2.Delete 3.Display 4.Exit Enter your choice :3 Queue elements are: 45->34->10->63->NULL Queue Operations L Insert 2.Delete 3.Display 4.Exit Enter your choice :1 Enter an element to be insert:3 List created successfully Queue Operations L Insert 2.Delete 3.Display 4.Exit Enter your choice :3 Queue elements are: 45->34-> 10->63->3->NULL Queue Operations t SS ee LInsert 2.Delete 3.Display 4.Exit Enter your choice :1 Queue is Full ‘Queue Operations L Insert 2.Delete 3.Display 4.Exit Enter your choice :2 Deleted element is:45, Queue Operations LAnsert 2.Delete 3.Display 4.Exit Enter your choice :3 Queue elements are: 34->10->63->3->NULL ‘Queue Operations Lnsert 2Delete 3.Display 4.Exit Enter your choice :2 Deleted element is:34 Queue Operations Lnsert 2.Delete 3.Display 4.Exit Enter your choice :3 Queue elements are: 10->63->3->NULL ‘Queue Operations Las ManvaL 1 Insert 2.Delete 3.Display 4.Exit Enter your choice :2 Deleted element is:10 Queue Operations 1 Insert 2.Delete 3.Display 4.Exit 63->3->NULL, Queue Operations LInsert 2.Delete 3.Display 4.Exit Enter your choice :4 6. Write a program to simulate the working of Circular queue using an array. /*Circular Queue Operations using Array*/ #include #include #include #define max 5 void add(int n); void del(int n); void display(); int afmax]; int ch,i,items int main() { while(1) Pantene Circular Queue Coe SY Para Structures Usino ©) printf(“\n 3. Display”); printf(“\n 4.Exit”); printf(“"\n Enter Your choice :”); scanf(“%ed”,&ch); switch(ch) { case 1: add(max); break; case 2: del(max); break; case 3: display(); break; default: return 0; } } } void add(int n) if(cear+1)>=n) printf(‘\n Queue is Full”); else printf(‘\n Enter the Element to be insert :”); scanf(‘““od” &item); if(rear==-1) front=rear=0; else rear=(reart 1) Ion; afrear]=item; printf(“\n Rear="d : Front="ed \n” rear,front); } } void del(int n) { int x; if(front==-1) { printf(“\n Queue is Empty”); } x=alfront]; if(front front=(front+1)¢on; Printf(’\n Deleted element is:sed x) printf(“\n Rear=%d : Front= ; + Front=%od \n” rear,front); } void display() if(front==-1) printf("\n Queue is empty”); else f { printf(“\n Queue Elements are\n"); forli=front;i<=rear:it-+) printf(“\t%Ied" afi); } } Output Circular Queue Operations LInsertion 2.Deletion 3.Display 4.Exit Enter Your choice :1 Enter the Element to be insert :10 Rear=0 : Front=0 Circular Queue Operations 1 Insertion 2.Deletion 3 Display 4.Exit Enter Your choice :1 Enter the Element to be insert :20 Rear=1 : Front=0 ee Queue Operations ai” - ‘Dara Structures Usinc © 1 Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :1 Enter the Element to be insert :30 Rear=2 : Front=0 Circular Queue Operations L Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :1 Enter the Element to be insert :40 Rear=3 : Front=0 Circular Queue Operations 1 Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :1 Enter the Element to be insert :50 Rear=4 : Front=0 Circular Queue Operations 1.Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :1 Queue is Full Circular Queue Operations L Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :3 Queue Elements are 10 20 30 40 50 Circular Queue Operations a L Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :3 Queue Elements are 20 30 40 50 Circular Queue Operations L Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :2 Deleted element is:20 Rear=4 : Front=2 Circular Queue Operations 1 Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :3 Queue Elements are 30 40 50 Circular Queue Operations I Insertion 2.Deletion 3.Display 4.Exit Enter Your choice :4 Process exited after 40.05 seconds Press any key to continue ~~ with return value 0 a _ _D Deletion 3.Display 4.Exit Enter Your choice -2 Deleted element is:19 Rear=4 : Front=1 Circular Queue Operations Dara Structures Usine © gram to insert the elements {61,16,8,27) into ordered singly linked list Write a prot a i. i aie tn g.61,27 from the list. Display your list after each insertion and deletion, "and delete 8, (Refer. Program: 4) Write a program for Tower of Honoi problem using recursion. /* Towers of Hanoi using Recursion */ #include void towers(int n, char sr, char ax, char dt); int main() { int n; printf(“Towers of Hanoi \n”); printf“ --\n”); printf(“Enter number of disks \n”); scanf(“%od”,&n); “S', ‘A’, ‘D); void towers(int n, char sr, char ax, char dt) i if (n==1) printf(“\n Move disk 1 from tower ‘oc to tower “oc”, sr, dt); else if towers(n-I, sr, dt, ax); printf(“\n Move disk ed from tower %ec to tower %oc”, n, sr, dt); towers(n-1, ax, st, dt); , } Output Towers of Hanoi Enter number of disks 3 Move disk 1 from tower $ to tower D Move disk 2 from tower S to tower A Move disk 1 from tower D to tower A Move disk 3 from tower $ to tower D Move disk 1 from tower A to tower S Move disk 2 from tower A to tower D Move disk 1 from tower $ to tower D 2d GCD of 3 numbe . Be ree ee Es ta us (Ge Mawn, ~ ‘Ny My 9, Write recursive program to f 7" GCD of three Nui #include int gcd(int a, int by; int main) { int a,b,c,d; printf(“ GCD of Three printf - printf(“Enter three number. \y> scani(“Tod Tod Fed” da, Hye i d=ged(a,gcd(b,c)); é printf(“\n GCD of ed sf se Numbers \n’); Sod and Ged is Sod?”,a,b,c,d); return 0; } int ged(int a, int b) { if (b== return(a); else if (a #include #define max 5 struct node { int info; struct node “link; }*top,*ptr; jid push(int num); It pop0): RT I RE NT TT Dav Structures Usine 6) void peek(); void display( { while(1) { printf(“\n Stack Operations”); printf("\n ys printf(\n 1.Push”); printf(‘\n 2.Pop"); printf(“\n 3.Peek”); printf(“\n 4.Display”); printf("\n 5.Exit”); printf(‘\n Enter your choice : scanf(‘“od”” Sch); switch(ch) case 1: if (x<=max) { printf(“\n Enter an element to be insert:”); scanf(“%d”,&data); push(data); xe } else printf(“\n Stack Full”); break; case 2: pop(); break; case 3: peek(); break; case 4: printf(“\n Elements of the stack are: \n”); display); break; default: return 0; i} i } void push(int num) { ptr=malloc(sizeof(struct node)); ptr->info=num: if(pu==NULL) { printf(“\n Stack is empty \n”); return 0; } top=top->link; n=ptr->info; printf(“\n Popped element is od \n”,n); free(ptr); return; } void peek() { ptr=top; if(ptr==NULL) ( 2 printf(“\n Stack is empty”); return; else printf( “ptr->info); «ynTop element of the Stack is “ed } void display() { ptr=top; if(pt==NULL) { or). printf(“\n stack is empty"): return; "Data Structures Usine ©) else { while(ptr!=NULL) { printf(“\n%d “,ptr->info); ptr=ptr->link; } } } Output Stack Operations L.Push 2.Pop 3.Peek 4.Display S.Exit Enter your choice : 1 Enter an element to be insert:10 Stack Operations 1.Push 2.Pop 3.Peek 4,Display S.Exit Enter your choice : 1 Enter an element to be insert:20 Stack Operations 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter your choice : 1 Enter an element to be insert:30 Stack Operations 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter your choice : 1 Enter an element to be i insert: Stack Operations sert:40 1.Push, 2.Pop 3.Peek 4.Display S.Exit Enter your choice : 1 Enter an element to be insert:50 Stack Operations 1.Push 2.Pop. 3.Peek 4.Display 5.Exit Enter your choice : 1 Stack Full Stack Operations 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter your choice = 4 Elements of the stack are: 50. 40 30 20 10 Stack Operations = sahmnannnmuiset Dara Srructunzs Usina ©) 4.Display S.Exit Enter your choice : 3 Top element of the Stack is 50 ‘Stack Operations 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter your choice : 2 Popped element is 50 Stack Operations 1.Push 2.Pop 3.Peek 4,Display S.Exit Enter your choice : 2 Popped element is 40 ‘Stack Operations 1.Push 2.Pop 3.Peek 4.Display 5.Exit Enter your choice : 4 Elements of the stack are: 30 20 10 Stack Operations 1.Push 2.Pop 3.Peek 4.Display S.Exit Enter your choice : 5 . Writea program to convert /* Infix to Postfix C #include #include char stack[20}; int toy . char push(char e); char pop0; int pr(char e); int main() { char post[20],ch; char infix[]="x‘y/(5*z)+2"; int i=0,k=0; printf(“ Infix Expression: “); printf(‘“es” infix); push(“#"); while ((ch=infix[i++])!="\0") push(ch); else if(isalnum(ch)) post[k++]= else if(ch== *)’) { while (stack[top]!="(*) post[k++]=pop(); } else { while (pr(stack{top])>=Pr(ch)) post{k++]=pop0)s push(ch); } fi ae while (stack{top]!="#) post{k++]=pop0; \="\0"; ‘ a s arian Pos Expression: si" POS) } char push(char €) i stack[++top]=¢: }+2 to its postfix expression, 12. return ) char pop() { return(stack[top--]); int pr(char e) { switch(e) i case ‘#: return 0; case ‘(return 1; case +7: case “': return 2; case “%o": return 3; } return 1; } Dara Structures Usine © + Output Infix Expression: xy/(5*2)+2 Postfix Expression: xy5z*2+(/* Write a program to evaluate a postfix expression 53 +82 - * /* Evaluate a postfix expression */ #include #include int stack[20}; int top=-1; int push(int x); int pop(); int main) { char exp[8]="53+82-*"; char *e; int n1,n2,n3,num; printf(“Input the expression: “); printf(“os” exp); e=exp; while(*e!="\0") { if(isdigit(*e)) push(num); } else { nl=pop(); n2=pop0); switch(*e) push(n3); } ett; ) ” printf(\n The result of expression="ed”,pop()); return 0; } int push(int x) { stack[++top]=x; return 0; } int pop() { return stack[top—-]; Output Input the expression: 53+82-* The result of expression=48 eam Data Structures Usine © e elements {18,15,40,50,30,17,41} it create a binary tree with the 13. Write a program to create a binary Se ee after creation insert 45 and 19 into tree and delete 15,17 a the tree on each insertion and deletion operation. * Binary Tree Operations */ #include #include #include typedef struct node { int data; Struct node* left; struct node* right; } node; node *create(int data); void insert(node** root, int data); node *getrightnode(node* root); void deleterightnode(node* root, node* dnode); void del (node** root, int data); void inorder(node* root); int main) { node* root = NULL; int key; insert(&root,18); insert(&root,15); insert(&root,40); insert(&root,50); insert(&root,30); insert(&root,17); insert(&root,41); printf(“Inorder traversal of the given Binary Tree is: “); inorder(root); printf(‘\n”); key = 45; insert(&root, key); printf(“After Insertion of ‘od: “, key); inorder(root); printf(“\n”); key = 19; insert(&root, key); printf(“After Insertion of ‘ed: “, key); inorder(root); wy en Ni, A intf("\n"); pri key = 15; del (&root, key); printf(“After Deletio kk inorder(root); OF ed, key), printf(“\n”); key = 17; del (&root, key); printf(“After Deletion of %d: « inorder(root); EY printf(‘\n”); key = 41; del (&root, key); printf(“After Deletion of %od: “ r inorder(root); ee printf(“\n"); return 0; } node* create(int data) { node* newnode = (node*)malloc(sizeof(node)); newnode->data = data; newnode->left = NULL; newnode->right = NULL; return newnode; void insert(node** root, int data) node* newnode = create(data); node* temp; node* q{100}; intf=-l,r= | if Groot NULL) mm |{ *root = newnode; temp (temp->left == NULL) a Dara StRucruREs Using G) temp->left = newnode; return; } else { ql++1] = temp->left; a, if (emp->right == NULL) { temp->right = newnode; return; } else { g[++1] = temp->right; } } node* getrightnode(node* root) { node* temp; node* q[100); int f=-1,r=-1; [+41] = root; while (f != 1) { temp = q[++f]; if (temp->left |= NULL) [+41] = temp->! I if (temp->right != NULL) { [+41] = temp->right; } } return temp; } void deleterightnode(node* root, node* dnode) { node* temp; node* q{100]; int f=-1,r=-1; gl++1] = root; 7 wi { temp = q[++f]; if (temp == dnode) { temp = NULL; free(dnode), return; if (temp->right != NULL) { if (temp->right == dnode) { temp->right = NULL; free(dnode); return; } else { q[++r] = temp->right; } } if (temp->left != NULL) { if (temp->left == dnode) { temp->left = NULL; free(dnode); return; } else gl++1] = temp->left; } } } } void del (node** root, { node* temp; node* keynode = NULL: node* q[100) intf=-1, int data) printf(“Tree is empty.\n” return; } if (*root)->left == NULL && (*root)->right == NULL) if ((*root)->data == data) free(*root); *root = NULL; return; } else { printf(‘“‘node not found.\n”); return; } } while (f !=1) { temp = q[++f] if (temp->data { keynode = temp; } if (temp->left != NULL) { q[++1] = temp->left; i if (temp->right != NULL) { q[++1] = temp->right; } } if (keynode != NULL) node* deepestnode = getrightnode(*root); keynode->data = deepestnode->data; deleterightnode(*root, deepestnode); } else 4 __—_ mor initio nae printf(“node not foundn } void inorder(node* root) { if (root == NULL) { return; } inorder(root->left); printf(““ed “, root->data); inorder(root->right); } a ; Output inorder traversal of the giv: i After Insertion of 45: Le Eee Ba oe After Insertion of 19: 45 50 19 15 30 18 1740 41 After Deletion of 15: 45 50 19 30 18 17 40 41 After Deletion of 17: 50 19 30 18 45 40 41 After Deletion of 41: 50 19 30 18 45 40. 14, Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and perform inorder, preorder and postorder traversal. /* Binary Search Tree Traversal */ #include #include struct btnode { int data; struct btnode *left; struct binode *right; as )*root=NULL,"temp=NULL.*2, tl; void create(); void search(struct btnode *t); void inorder(struct binode a); void preorder(struct btnode 8 void postorder(struct binode *t); int main() { int ch; printf( “\n Binary Search Tree”); Tree”): printf(“\n 3.Preorder Traversal”); printf(‘“\n 4.Postorder Traversal”); printf(“\n 5.Exit”); while(1) { printf(“\n Enter your choice : scanf(‘““od”, &ch); switch (ch) { case 1: create(); break; case 2: printf(“ Inorder \n”); printf(“ -------\n"); inorder(root); break; ; case 3: : printf(“ Preorder \n”); printf(“ --------\n”); preorder(root); break; case 4: printf(“ Postorder \n”); printf(“ ---------\n"); postorder(root); break; default: return 0; } } 1 } void create() { int i,a{7]={2,5,1,3,9,0,6}; forli=Oxidata = ali; temp->left=temp->right=NULL; (Gis Hare rT else search(root); printf((“Tree created successfy } void search (struct btnode *t) { if ((temp->data > t->data) && search(t->right); else if (temp->data > t->data) && (t- t->right=temp; else if ((temp->data < t->data) && tesleft |= search(t->left); ee debLee) else if ((temp->data < t->data) && (t->left == NULL)) t->left=temp; } void inorder (struct btnode *t) lly”); (t->right != NULL)) >right == NULL)) i if (root==NULL) { printf(“\n No elements in a tree to display”); return; } if (t->left!=NULL) inorder(t->left); printf(“\t od”, t->data); if (t->right != NULL) inorder(t->right); void preorder (struct btnode *t) { if (root==NULL) Sra No elements in a tree to display”); Decree od”, t->data); if (t->left!=NULL) preorder(t->left); if (->right |= NULL) preorder(t->right); 2 void postorder(struct btnode *t) Uw if (root==NULL) { printf(“\n No elements in a tree to display"); return; } if (t->left!=NULL) postorder(t->left); if (->right!=NULL) postorder(t->right); printf(At od”, t->data); } Output Binary Search Tree 1.Create Binary Tree 2.Jnorder Traversal 3.Preorder Traversal 4.Postorder Traversal S.Exit Enter your choice : 1 Tree created successfully Enter your choice : 2 Inorder OMmtlieen2 a3. 5. 6 8 Enter your choice : 3 Preorder Peete es Seg 3 9 6 Enter your choice : 4 Postorder (Oumietpey $3596) = 95 352, Enter your choice :5 15, Write a program to Sort the following elements using heap sort (9.16,32,8,4,1,5,8,0. /* Heap Sort */ #include void heap(int af], int n, int i); void heapsort(int af], int n); int main() { printf(‘“\n Input elemen ts are s\n"); for (i=0;ia[max]) max = left; if (righta{max]) max=right; if (max!=i) { t=ali); a[iJ=a[max]; a[max]=t; heap(a, n, max); } ) + void heapsort(int af], int n) i inti,t; for (i=n/2-1;i>=0:i--) heap(a,n,i); for (i=n-1;i>0;i--) { t=a[0]; a[0]=alil; alii heap(a,i,0); a EEE = ue. oe, am ‘Data Structures Use © ‘Output 7 Input elements are : SG ite ts) 3 Sorted elements are: i Se 16. Given S1={“Flowers”} ; S2=(“are beautiful”} I. Find the length of SL II. Concatenate S1 and S2 IIL. Extract the substring “low” from S1. IV. Find “are” in S2 and replace it with “is”. /* String Operations */ #include #include yoid length(char *str); void concat(char *strl, char *str2); void extract(char *str,int pos, int e); void replace(char *str, char *rstr, int pos); int main() { int len,ch,pos,e; while(1) { char s1[20]="Flowers”; char s2[20]=” are beautiful”; printf(“ es\n””,$1,82); printf(‘\n sl = %s_ printf(“\n String Operations “); printf(“\n: z printf(“\n 1.Length”); printf(“\n 2.Concatenate”); printf(“\n 3.Substring”); printf(‘\n 4.Replace”); printf(“\n 5.Exit”); printf(“\n Enter your choice:”); scanf(““bd” ch); switch(ch) ; { case 1: Iength(s1); break; case 2: concat(s1,s2); break; case 4: replace(s2,"is “0, break; default: return 0; } } } void length(char *str) i int i=0, len=0; while(strfi]!="\0") f Ten++; its } printf(“\n Length of %s = ‘ed”,str,len); } void concat(char str! [20], char str2[20]) itt; while(str2{j}!="\0") { strl[iJ=str2{j]; ints j++ } strl[i]="\0"; : ar printf(“\n Concatenated string = %os”,str1); } + 5 void extract(char *str,int pos, int e) { int i=0,j=0; char substr[10]; for(i=posyi<=esit+) substr[j, is {i ) substr{j]=\0"s printf(“\n Substring = %s” substr); } void replace(char *str, char *rstr, int pos) { char baal foci istrlen(str)i++) ae { for(k=pos;k ee a 2.Concatenate 3.Substring 4.Replace S.Exit Enter your choice:2 Concatenated strin; lowers are beautiful sl = Flowers s2 String Operations are beautiful 1.Length 2.Concatenate 3.Substring 4.Replace S.Exit Enter your choice:3 Substring = low sl =Flowers s2= are beautiful String Operations 1.Length 2.Concatenate 3.Substring 4.Replace S.Exit Enter your choice:4 is beatiful Output $1 = Flowers s2= are beautiful String Operations 1.Length 2.Concatenate 3.Substring 4.Replace S.Exit Enter your choice:5 17, Write a program to implement adjacency matrix of @ graph. Graph: 7 ; iD | o— . 7* Adjacency Matrix Representation of Graph */ #include #define v 5 void init(int af][v); | void addedge(int a{}[v},int src, int dest); void display(int a[][v]); int main() { int blv][v]; init(b); addedge(b,0,1); addedge(b,0,2); addedge(b,0,3); addedge(b,1,3); addedge(b,1,4); addedge(b,2,3); addedge(b,3,4); display(b); return 0; } void init(int a[][v}) } void addedge(int a[][v],int sre, int dest) { a[sre]{dest] = 1; } void display(int af][v)) { printf(“\n Adjacency Matri printf("\ ane Siven graph is:\n”); for(j = 0; j < v; j++) { printf(“od\t “, afi}[j}); } printf(‘\n”); } J ‘Adjacency Matrix of Given graph is: Output eeooeor 1 0 0 0 0 eooor corer orore 18. Write a program to insert/retrieve an entry into hash/ from a hash table with open addressing using linear probing. /* Hash Table */ #include #include #include struct item { int key; int value; hs struct hashtable { int flag; struct item *data; hs struct hashtable *4s int size = 0; | int max = 10; void init_a(); int hashcode(int key); int insert(int key, int value); void del(int key); int size_of_hashtable(; void display); int main() { int ch, key, value, n, c,i; ae(struct hashtable) malloc(max * sizeof{struct hashtable*)); while (1) { printf(“Hash Table with Linear Probing’); printf(’ "yy printf(“\n 1 Inserting item to the Hash Table”); printf(“\n 2.Removing item from the Hash Table”); printf(“\n 3.Check the size of Hash table”); printf(‘“‘\n 4.Display Hash table”); printf(“\n 5.Exit”); printf(“\n Enter your choice:”) scanf(“%od”,&ch); switch(ch) { case 1: printf(“Enter key and value-\t");, scanf(“od %d”, &key, &value); insert(key, value); break; case 2: printf(‘“\n Enter the key to delete:”); scanf(“‘od”, &key); del(key); break; case 3: n=size_of_hashtable(); printf(“Size of Hash table is-:Jod\n”, n); break; case 4: display0; break; default: return 0; | rt ———— } } void init_a() a{i].data=NULL; printf (“OK”); } } int hashcode(int key) { return (key‘lmax); } int insert(int key, int value) int index=hashcode(key); int i=index; struct item *new_item = (struct item*) malloc(sizeof(struct item)); new_item->key=key; new_item->value=value; while (ali].flag==1) { if (a[i].data->key==key) ( Hi! on printf(“\n Key already exists, hence updating its value \n”); afi].data->value=values return 0; } i=(i+1)%omax; if (i==index) vane Hash table is full, cannot insert any more item \n”); return 0; } } afij.flag = 1; afi.data = new_item; size++; Data Structures Usine C) printi(“\n Key (“ed) has been inserted \n”, key); return 0; } void del(int key) { int index = hashcode(key); int i= index; while (a[i].flag != 0) { if (ali).flag == 1 && ali].data->key { afij.flag = 2; afi].data = NULL; size; printf(“\n Key (“lod) has been removed \n”, key); return; } key) i + 1) % max; if (( == index) break; } printf(“\n This key does not exist \n”); } void display() { inti; for (i = 0; i < max; i++) Ht struct item “current = (struct item*) ali].data; if (current == NULL) { printf(“‘\n Array[%d] has no elements \n”, i); } ‘else { printf(“\n Array[%d] has elements -:\n %d (key) and %d(value) “i, current- >key, current->value); } } 3 int size_of _hashtable() return size; ) Output Hash Table with Linear Probing {Inserting item to the Hash Tabl 2.Removing item fro: ash T 3.Check the size of eae “ad 4.Display Hash table 5.Exit Enter your choice:1 Enter key and value-: 120 Key (1) has been inserted Hash Table with Linear Probing, {Inserting item to the Hash Table 2.Removing item from the Hash Table 3, Check the size of Hash table 4.Display Hash table 5.Exit Enter your choice:1 Enter key and value: 2.25 Key (2) has been inserted Hash Table with Linear Probing [inserting item to the Hash Table + Removing item from the Hash Table 3.Check the size of Hash table 4,Display Hash table 5.Exit Enter your choice:3 2D Size of Hash table is~ Hash Table with Linear Probing to the Hash Table from the Hash Table sh table | Inserting item 2.Removing item 3.Check the size of Ha 4.Display Hash table S.Exit Enter your choice:4 Array[0] has elements -: 9989232 (key) and O(value) Array[1] has elements -: 1 (key) and 20(value) Array[2] has elements -: 2 (key) and 25(value).

You might also like