We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53
1) Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array)
to represent 7 days of a week. Each Element of the array is a structure having three fields. The first field is the name of the Day (A dynamically allocated String), The second field is the date of the Day (A integer), the third field is the description of the activity for a particular day (A dynamically allocated String). b) Write functions create(), read() and display(); to create the calendar, to read the data from the keyboard and to print weeks activity details report on screen. Solution #include<stdio.h> #include<stdlib.h> #include<string.h> int n = 7; typedef struct { char *Dname; int dt; char *act; } calendar; calendar *cal; void create() { cal = (calendar*)malloc(n * sizeof(calendar)); } void read() { int i; for (i = 0; i < n; i++) { cal[i].Dname = (char*)malloc(n * sizeof(char)); // Assuming a maximum length of 100 characters printf("Enter the day="); scanf("%s", cal[i].Dname); printf("Enter the date="); scanf("%d", &cal[i].dt); fflush(stdin); cal[i].act = (char*)malloc(n * sizeof(char)); // Assuming a maximum length of 100 characters printf("Enter the activity of the day="); scanf("%s", cal[i].act); printf("\n"); } } void display() { int i;for (i = 0; i < n; i++) { printf("Day=%s\n Date=%d\n activity=%s\n\n", cal[i].Dname, cal[i].dt, cal[i].act); } } int main() { create(); read(); display(); return 0; } Output Enter the day=monday Enter the date=1 Enter the activity of the day=reading Enter the day=tuesday Enter the date=2 Enter the activity of the day=writing Enter the day=wednesday Enter the date=3 Enter the activity of the day=singing Enter the day=thursday Enter the date=4 Enter the activity of the day=party Enter the day=friday Enter the date=5 Enter the activity of the day=revision Enter the day=saturday Enter the date=6 Enter the activity of the day=movie Enter the day=sunday Enter the date=7 Enter the activity of the day=outing Day=monday Date=1 activity=reading Day=tuesday Date=2 activity=writing Day=wednesdayDate=3 activity=singing Day=thursday Date=4 activity=party Day=friday Date=5 activity=revision Day=saturday Date=6 activity=movie Day=sunday Date=7 activity=outing2. Develop a Program in C for the following operations on Strings. a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP) b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR Support the program with functions for each of the above operations. Don't use Built-in functions. Solution #include <stdio.h> char str[100], pat[100], rep[100], ans[100]; int i, j, c, m, k, flag = 0; void StringMatch() { i = m = c = j = 0; while (str[c]!= '\0') { if (str[m] == pat[i]) { i++; m++; if (pat[i] == '\0') { flag = 1; for (k = 0; rep[k] != '\0'; k++, j++) { ans[j] = rep[k]; } i = 0; c = m; } } else { ans[j] = str[c]; j++; c++; m = c; i = 0; } ans[j] = '\0'; }} int main(){ printf("Enter the main string="); gets(str); printf("Enter the Pattern string="); gets(pat); printf("Enter the replace string="); gets(rep); StringMatch(); if (flag == 0) { printf("Pattern not found"); } else { printf("The resultant string is=%s", ans); } return 0; } Output Enter the main string=abcdef Enter the Pattern string=ef Enter the replace string=EF The resultant string is=abcdEF3. Develop a menu driven Program in C for the following operations on STACK of Integers (Array Implementation of Stack with maximum size MAX) a. Push an Element on to Stack b. Pop an Element from Stack c. Demonstrate how Stack can be used to check Palindrome d. Demonstrate Overflow and Underflow situations on Stack e. Display the status of Stack f. Exit Support the program with appropriate functions for each of the above operations Solution #include<stdio.h> #define MAX 5 int top=-1, Stack[MAX]; void push(){ int item; if(top==(MAX-1)){ printf("Stack Overflow\n"); } else{ printf("Enter the elemnt to be pushed= "); scanf("%d",&item); Stack[++top]=item; } } void pop(){ if(top==-1){ printf("Stack underflow\n"); } else{ printf("Poped elemnt is=%d", Stack[top--]); } } void display(){ int i; if(top==-1){ printf("Stack Underflow\n"); } else{printf("The element of the stack are=\n"); for(i=top;i>=0;i--){ printf("Stack[%d]=%d \n",i,Stack[i]); } } } void palindrome(){ int i,count=0; for(i=0;i<=(top/2);i++){ if(Stack[i]==Stack[top-i]){ count=count+1; } } if((top/2+1)==count){ printf("Stack Content are palindrome\n"); } else{ printf("Stack content are not palindrome\n"); } } int main(){ int ch; do{ printf("Stack Menu"); printf("\n 1.push \n 2.pop\n 3.display\n 4.palindrome\n 5.exit\n"); printf("Enter your choice(1-5)="); scanf("%d", &ch); switch(ch){ case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: palindrome(); break; case 5: return 0;default:printf("Wrong choice"); } }while(ch!=4); return 0; } Output Stack Menu 1.push 2.pop 3.display 4. palindrome 5.exit Enter your choice(1-5)=1 Enter the elemnt to be pushed= 1 Stack Menu 1.push 2.pop 3.display 4.palindrome 5.exit Enter your choice(1-5)=1 Enter the elemnt to be pushed= 2 Stack Menu 1.push 2.pop 3.display 4.palindrome 5.exit Enter your choice(1-5)=1 Enter the elemnt to be pushed= 3 Stack Menu 1.push 2.pop 3.display 4.palindrome 5.exit Enter your choice(1-5)=1 Enter the elemnt to be pushed= 2 Stack Menu 1. push2. pop 3.display 4.palindrome 5.exit Enter your choice(1-5)=1 Enter the elemnt to be pushed= 1 Stack Menu 1.push 2.pop 3.display 4.palindrome 5.exit Enter your choice(1-5)=3 The element of the stack are= Stack[4]=1 Stack[3]=2 Stack[2]=3 Stack[1]=2 Stack[0]=1 Stack Menu 1.push 2.pop 3.display 4.palindrome 5.exit Enter your choice(1-5)=4 Stack Content are palindrome4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should support for both parenthesized and free parenthesized expressions with the operators: +, - , *, /, % (Remainder), ^ (Power) and alphanumeric operands Solution #include<stdio.h> #include<stdlib.h> #include<ctype.h> #define MAX_SIZE 30 char s[MAX_SIZE]; int top=-1; void push(char c){ s[++top]=c; } char pop(){ return s[top--]; } int prc(char c){ switch (c) { case '+': case '-':return 1; case '*': case '/': case '%':return 2; case '^':return 3; case '(':return 0; case '#':return -1; } } int main(){ char infix[30],c,postfix[50]; int i=0,k=0; push('#'); printf("Enter the expression="); scanf("%s", infix); while(infix[i]!='\0'){ c=infix[i]; if(c=='('){ push(c); } else if(isalnum(c)){postfix[k++]=c; } else if(c==')'){ while(s[top]!='(') postfix[k++] = pop(); pop(); } else{ if(c=='^' && s[top]=='^'){ push(c); } else{ while(prc(c)<=prc(s[top])){ postfix[k++]=pop(); } push(c); } } i++; } while(s[top]!='#'){ postfix[k++]=pop(); postfix[k]='\0'; printf("postfix expression=%s", postfix); return 0; } } Output Enter the expression=(a+b+c)*(a^b)*(a/b) postfix expression=ab+c+ab^*ab/*5. Develop a Program in C for the following Stack Applications a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^ Solution #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<math.h> #define MAX_SIZE 30 float s[MAX_SIZE]; int top=-1; void push(float c){ s[++top]=c; } float pop(){ return s[top--]; } int main(){ char c,postfix[50]; int i=0; float op1,op2; printf("Enter the expression="); scanf("%s", postfix); while(postfix[i]!='\0'){ c=postfix[i]; if(isdigit(c)){ push(c-48); } else { op2=pop(); op1=pop(); switch (c) { case '+': push(op1+op2); break; case '-': push(op1-op2); break; case '*':push(op1*op2); break; case '/': if(op2==0){ printf("error"); break; } push(op1/op2); break; case '^': push(pow(op1,op2)); break; default: break; } } i++; } printf("postfix expression=%s = evaluation=%f \n", postfix,pop()); return 0; } Output Enter the expression=23+6*7/34-5 postfix expression=23+6*7/34-5 = evaluation=5.000000 b. Solving Tower of Hanoi problem with n disks Solution #include <stdio.h> void tower(int n,int source, int temp, int destination){ if(n==0) return; tower(n-1, source, destination, temp); printf("\n Move disc %d from %c to %c", n, source,destination); tower(n-1,temp,source, destination);} void main(){ int n; printf("Enter the number of disc:\n"); scanf("%d",&n); tower(n,'A','B','C'); } Output Enter the number of disc: 3 Move disc 1 from A to C Move disc 2 from A to B Move disc 1 from C to B Move disc 3 from A to C Move disc 1 from B to A Move disc 2 from B to C Move disc 1 from A to C 6. Develop a menu driven Program in C for the following operations on Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX) a. Insert an Element on to Circular QUEUE b. Delete an Element from Circular QUEUE c. Demonstrate Overflow and Underflow situations on Circular QUEUE d. Display the status of Circular QUEUE e. Exit Support the program with appropriate functions for each of the above operations Solution:#include<stdio.h> #include<stdbool.h> #include<string.h> # define MAX_SIZE 4 int cqueue[MAX_SIZE], front=0,rear=-1,count=0; int cqfull(){ if(count==MAX_SIZE) return 1; else return 0; } int cqempty(){ if(count==0) return 1; else return 0; } void cinsert(int item){ if(cqfull()){ printf("Overflow\n"); } else{ rear=(rear+1)%MAX_SIZE; cqueue[rear]=item; count++; } } void cdelete() {if(cqempty()){ printf("Queue is empty\n"); } else{ printf("deleted element is=%d\n", cqueue[front]); front=(front+1)%MAX_SIZE; count--; } } void display() { int i=front,k; printf("The content are:"); if(cqempty()){ printf("queue is empty"); } else{ for(k=0;k<count;k++){ printf("\n-------\n %d |", cqueue[i]); printf("\n"); i=(i+1)%MAX_SIZE; } } } void main(){ int item,ch; do{ printf("\n\n--------MAIN MENU------\n"); printf("1.Insert the element\n"); printf("2.Delete the element\n");printf("3.Overflow \n"); printf("4.qempty \n"); printf("5.display\n"); printf("6.exit\n"); printf("Enter your choice ="); scanf("%d", &ch); switch(ch){ case 1:{ printf("enter the element to inserted="); scanf("%d",&item); cinsert(item); break; } case 2: cdelete(); break; case 3: if(cqfull()) printf("Cqueue is full"); break; case 4: if(cqempty()) printf("Cqueue is empty"); break; case 5: display(); break; case 6: printf("exiting the program.\n");break; default: printf("invalid choice please enter a valid option.\n"); } }while(ch!=6); return 0; }Output --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =1 enter the element to inserted=1 --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =1 enter the element to inserted=2 --------MAIN MENU------ 1. Insert the element2. Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =1 enter the element to inserted=3 --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =1 enter the element to inserted=4 --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =1 enter the element to inserted=5 Overflow --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =5 The content are:1 | 2| 3| 4| --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =2 deleted element is=1 --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =2 deleted element is=2 --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =2deleted element is=3 --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =2 deleted element is=4 --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =2 Queue is empty --------MAIN MENU------ 1.Insert the element 2.Delete the element 3.Overflow 4.qempty 5.display 6.exit Enter your choice =6 exiting the program.7. Develop a menu driven Program in C for the following operations on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Programme, Sem, PhNo a. Create a SLL of N Students Data by using front insertion. b. Display the status of SLL and count the number of nodes in it c. Perform Insertion / Deletion at End of SLL d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack) e. Exit Solution #include <stdio.h> #include <stdlib.h> struct node { char usn[25], name[25], branch[25]; int sem; long int phone; struct node* link; }; typedef struct node* NODE; NODE start = NULL; int count = 0; NODE create() { NODE snode; snode = (NODE)malloc(sizeof(struct node)); if (snode == NULL) { printf("\nMemory is not available"); exit(1); } printf("\nEnter the usn, Name, Branch, sem, PhoneNo of the student:"); scanf("%s %s %s %d %ld", snode->usn, snode->name, snode->branch, &snode->sem, &snode->phone); snode->link = NULL; count++; return snode; } NODE insertfront() { NODE temp; temp = create(); if (start == NULL) { return temp; } temp->link = start; return temp; } NODE deletefront() {NODE temp; if (start == NULL) { printf("\nLinked list is empty"); return NULL; } if (start->link == NULL) { printf("\nThe Student node with usn:%s is deleted ", start->usn); count--; free(start); return NULL; } temp = start; start = start->link; printf("\nThe Student node with usn:%s is deleted", temp->usn); count--; free(temp); return start; } NODE insertend() { NODE cur, temp; temp = create(); if (start == NULL) { return temp; } cur = start; while (cur->link != NULL) { cur = cur->link; } cur->link = temp; return start; } NODE deleteend() { NODE cur, prev; if (start == NULL) { printf("\nLinked List is empty"); return NULL; } if (start->link == NULL) { printf("\nThe student node with the usn:%s is deleted", start->usn); free(start);count--; return NULL; } prev = NULL; cur = start; while (cur->link != NULL) { prev = cur; cur = cur->link; } printf("\nThe student node with the usn:%s is deleted", cur->usn); free(cur); prev->link = NULL; count--; return start; } void display() { NODE cur; int num = 1; if (start == NULL) { printf("\nNo Contents to display in SLL \n"); return; } printf("\nThe contents of SLL: \n"); cur = start; while (cur != NULL) { printf("\n||%d|| USN:%s| Name:%s| Branch:%s| Sem:%d| Ph:%ld|", num, cur->usn, cur->name, cur- >branch, cur->sem, cur->phone); cur = cur->link; num++; } printf("\n No of student nodes is %d \n", count); } void stackdemo() { int ch; while (1) { printf("\n~~Stack Demo using SLL~~\n"); printf("\n1: Push operation \n2: Pop operation \n3: Display \n4: Exit \n"); printf("\nEnter your choice for stack demo"); scanf("%d", &ch); switch (ch) { case 1: start = insertfront(); break;case 2: start = deletefront(); break; case 3: display(); break; default: return; } } return; } int main() { int ch, i, n; while (1) { printf("\n~~Menu~~"); printf("\nEnter your choice for SLL operation \n"); printf("\n1: Create SLL of Student Nodes"); printf("\n2: DisplayStatus"); printf("\n3: InsertAtEnd"); printf("\n4: DeleteAtEnd"); printf("\n5: Stack Demo using SLL(Insertion and Deletion at Front)"); printf("\n6: Exit \n"); printf("\nEnter your choice:"); scanf("%d", &ch); switch (ch) { case 1: printf("\nEnter the no of students: "); scanf("%d", &n); for (i = 1; i <= n; i++) start = insertfront(); break; case 2: display(); break; case 3: start = insertend(); break; case 4: start = deleteend(); break; case 5: stackdemo(); break; case 6:exit(0); default: printf("\nPlease enter the valid choice"); } } } Output 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=0 Enter how many nodes to be created=2 Enter the Usn, Name,Program,Sem,Phone number= 1 XYZ CSE 3 9876 Enter the Usn, Name,Program,Sem,Phone number= 2 ABC ECE 3 8797 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=2 Enter the Usn, Name,Program,Sem,Phone number= 3PQR IS 3 9988 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=1 LinkedList=Usn=3 Name=PQR Program=IS Sem=3 Phone Number=9988 Usn=2 Name=ABC Program=ECE Sem=3 Phone Number=8797 Usn=1 Name=XYZ Program=CSE Sem=3 Phone Number=9876 No nodes in linked list=3 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.ExitEnter the choice=3 Enter the Usn, Name,Program,Sem,Phone number=4 STU AIML 3 6655 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=1 LinkedList=Usn=3 Name=PQR Program=IS Sem=3 Phone Number=9988 Usn=2 Name=ABC Program=ECE Sem=3 Phone Number=8797 Usn=1 Name=XYZ Program=CSE Sem=3 Phone Number=9876 Usn=4 Name=STU Program=AIML Sem=3 Phone Number=6655 No nodes in linked list=40.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=4 3 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=68. Develop a menu driven Program in C for the following operations on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo a. Create a DLL of N Employees Data by using end insertion. b. Display the status of DLL and count the number of nodes in it c. Perform Insertion and Deletion at End of DLL d. Perform Insertion and Deletion at Front of DLL e. Demonstrate how this DLL can be used as Double Ended Queue. f. Exit Solution #include <stdio.h> #include <stdlib.h> struct node { char ssn[25], name[25], dept[10], designation[25]; int sal; long int phone; struct node* llink; struct node* rlink; }; typedef struct node* NODE; NODE first = NULL; int count = 0; NODE create() { NODE enode; enode = (NODE)malloc(sizeof(struct node)); if (enode == NULL) { printf("\nRunning out of memory"); exit(0); } printf("\nEnter the ssn, Name, Department, Designation, Salary, PhoneNo of the employee: \n"); scanf("%s %s %s %s %d %ld", enode->ssn, enode->name, enode- >dept, enode->designation, &enode->sal, &enode->phone); enode->llink = NULL; enode->rlink = NULL; count++; return enode; } NODE insertfront() { NODE temp; temp = create(); if (first == NULL) { return temp; } temp->rlink = first; first->llink = temp;return temp; } void display() { NODE cur; int nodeno = 1; cur = first; if (cur == NULL) printf("\nNo Contents to display in DLL"); while (cur != NULL) { printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Designation: %s|Salary:%d|Phone no:%ld", nodeno, cur->ssn, cur->name, cur- >dept, cur->designation, cur->sal, cur->phone); cur = cur->rlink; nodeno++; } printf("\nNo of employee nodes are %d", count); } NODE deletefront() { NODE temp; if (first == NULL) { printf("\nDoubly Linked List is empty"); return NULL; } if (first->rlink == NULL) { printf("\nThe employee node with the ssn:%s is deleted", first- >ssn); free(first); count--; return NULL; } temp = first; first = first->rlink; temp->rlink = NULL; first->llink = NULL; printf("\nThe employee node with the ssn:%s is deleted", temp- >ssn); free(temp); count--; return first; } NODE insertend() { NODE cur, temp; temp = create(); if (first == NULL) { return temp;} cur = first; while (cur->rlink != NULL) { cur = cur->rlink; } cur->rlink = temp; temp->llink = cur; return first; } NODE deleteend() { NODE prev, cur; if (first == NULL) { printf("\nDoubly Linked List is empty"); return NULL; } if (first->rlink == NULL) { printf("\nThe employee node with the ssn:%s is deleted", first- >ssn); free(first); count--; return NULL; } prev = NULL; cur = first; while (cur->rlink != NULL) { prev = cur; cur = cur->rlink; } cur->llink = NULL; printf("\nThe employee node with the ssn:%s is deleted", cur->ssn); free(cur); prev->rlink = NULL; count--; return first; } void deqdemo() { int ch; while (1) { printf("\nDemo Double Ended Queue Operation"); printf("\n1: InsertQueueFront\n 2: DeleteQueueFront\n 3: InsertQueueRear\n 4: DeleteQueueRear\n 5: DisplayStatus\n 6: Exit \n"); scanf("%d", &ch); switch (ch) { case 1: first = insertfront();break; case 2: first = deletefront(); break; case 3: first = insertend(); break; case 4: first = deleteend(); break; case 5: display(); break; default: return; } } } void main() { int ch, i, n; while (1) { printf("\n\n~~~Menu~~~"); printf("\n1: Create DLL of Employee Nodes"); printf("\n2: DisplayStatus"); printf("\n3: InsertAtEnd"); printf("\n4: DeleteAtEnd"); printf("\n5: InsertAtFront"); printf("\n6: DeleteAtFront"); printf("\n7: Double Ended Queue Demo using DLL"); printf("\n8: Exit \n"); printf("\nPlease enter your choice: "); scanf("%d", &ch); switch (ch) { case 1: printf("\nEnter the no of Employees: "); scanf("%d", &n); for (i = 1; i <= n; i++) first = insertend(); break; case 2: display(); break; case 3: first = insertend(); break; case 4:first = deleteend(); break; case 5: first = insertfront(); break; case 6: first = deletefront(); break; case 7: deqdemo(); break; case 8: exit(0); default: printf("\nPlease Enter the valid choice"); } }Output 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=0 Enter how many employee to be added=2 ssn,name,department,designation,phone number,salary= 1 ABC PORGRAMMER manager 986 50000 2 XYZ Coder ssn,name,department,designation,phone number,salary= ceo 8158 10000 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=2 ssn,name,department,designation,phone number,salary= 3 Hii Worker head 6677 250000.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=3 ssn,name,department,designation,phone number,salary=4 STU Owner Owner 569 900000 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=1 3 Hii Worker head 6677 25000.000000 1 ABC PORGRAMMER manager 986 50000.000000 2 XYZCoder ceo 8158 10000.000000 4 STU Owner Owner 569 900000.000000 No of nodes=4 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=4 Employee details deleted:ssn3 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=5. Employee details deleted:ssn4 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=11 ABC PORGRAMMER manager 986 50000.000000 2 XYZ Coder ceo 8158 10000.000000 No of nodes=2 0.Create 1.display 2.To insert node at begging 3.To insert node at End 4.To delete the beginning node 5.To delete the end node 6.Exit Enter the choice=69. Develop a Program in C for the following operationson Singly Circular Linked List (SCLL) with header nodes a. Represent and Evaluate a Polynomial P(x,y,z) = 6x 2 y 2 z-4yz 5 +3x 3 yz+2xy 5 z-2xyz 3 b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in POLYSUM(x,y,z) Support the program with appropriate functions for each of the above operations Solution: #include<stdio.h> #include<stdlib.h> #include<math.h> #define COMPARE(x, y) ( (x == y) ? 0 : (x > y) ? 1 : -1) struct node { int coef; int xexp, yexp, zexp; struct node *link; }; typedef struct node *NODE; NODE getnode(){ NODE x; x = (NODE) malloc(sizeof(struct node)); if(x == NULL) { printf("Running out of memory \n"); return NULL; } return x; } NODE attach(int coef, int xexp, int yexp, int zexp, NODE head){ NODE temp, cur; temp = getnode(); temp->coef = coef; temp->xexp = xexp; temp->yexp = yexp; temp->zexp = zexp; cur = head->link; while(cur->link != head) { cur = cur->link; } cur->link = temp; temp->link = head; return head; } NODE read_poly(NODE head){int i, j, coef, xexp, yexp, zexp, n; printf("\nEnter the no of terms in the polynomial: "); scanf("%d", &n); for(i=1; i<=n; i++) { printf("\n\tEnter the %d term: ",i); printf("\n\t\tCoef = "); scanf("%d", &coef); printf("\n\t\tEnter Pow(x) Pow(y) and Pow(z): "); scanf("%d", &xexp); scanf("%d", &yexp); scanf("%d", &zexp); head = attach(coef, xexp, yexp, zexp, head); } return head; } void display(NODE head){ NODE temp; if(head->link == head) { printf("\nPolynomial does not exist."); return; } temp = head->link; while(temp != head) { printf("%dx^%dy^%dz^%d", temp->coef, temp->xexp, temp->yexp, temp->zexp); temp = temp->link; if(temp != head) printf(" + "); } } int poly_evaluate(NODE head){ int x, y, z, sum = 0; NODE poly; printf("\nEnter the value of x,y and z: "); scanf("%d %d %d", &x, &y, &z); poly = head->link; while(poly != head) { sum += poly->coef * pow(x,poly->xexp)* pow(y,poly->yexp) * pow(z,poly->zexp); poly = poly->link; } return sum; } NODE poly_sum(NODE head1, NODE head2, NODE head3){NODE a, b; int coef; a = head1->link; b = head2->link; while(a!=head1 && b!=head2) { while(1) { if(a->xexp == b->xexp && a->yexp == b->yexp && a->zexp == b->zexp) { coef = a->coef + b->coef; head3 = attach(coef,a->xexp,a->yexp,a->zexp,head3); a = a->link; b = b->link; break; } //if ends here if(a->xexp!=0 || b->xexp!=0) { switch(COMPARE(a->xexp, b->xexp)) { case -1 : head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3); b = b->link; break; case 0 : if(a->yexp > b->yexp) { head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3); a = a->link; break; } else if(a->yexp < b->yexp) { head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3); b = b->link; break; } else if(a->zexp > b->zexp) { head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3); a = a->link; break; } else if(a->zexp < b->zexp) { head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3); b = b->link; break; } case 1 : head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3); a = a->link; break; } //switch ends herebreak; } //if ends here if(a->yexp!=0 || b->yexp!=0) { switch(COMPARE(a->yexp, b->yexp)) { case -1 : head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3); b = b->link; break; case 0 : if(a->zexp > b->zexp) { head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3); a = a->link; break; } else if(a->zexp < b->zexp) { head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3); b = b->link; break; } case 1 : a = a->link; break; }//end switch break; head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3); } if(a->zexp!=0 || b->zexp!=0) { switch(COMPARE(a->zexp,b->zexp)) { case -1 : head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3); b = b->link; break; case 1 : head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3); a = a->link; break; } break; } } } while(a!= head1) { head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);a = a->link; } while(b!= head2) { head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3); b = b->link; } return head3; } void main(){ NODE head, head1, head2, head3; int res, ch; head = getnode(); /* For polynomial evalaution */ head1 = getnode(); /* To hold POLY1 */ head2 = getnode(); /* To hold POLY2 */ head3 = getnode(); /* To hold POLYSUM */ head->link=head; head1->link=head1; head2->link=head2; head3->link= head3; while(1) { printf("\n~~~Menu~~~"); printf("\n1.Represent and Evaluate a Polynomial P(x,y,z)"); printf("\n2.Find the sum of two polynomials POLY1(x,y,z)"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\n~~~~Polynomial evaluation P(x,y,z)~~~\n"); head = read_poly(head); printf("\nRepresentation of Polynomial for evaluation: \n"); display(head); res = poly_evaluate(head); printf("\nResult of polynomial evaluation is : %d \n", res); break; case 2: printf("\nEnter the POLY1(x,y,z): \n"); head1 = read_poly(head1); printf("\nPolynomial 1 is: \n"); display(head1); printf("\nEnter the POLY2(x,y,z): \n");head2 = read_poly(head2); printf("\nPolynomial 2 is: \n"); display(head2); printf("\nPolynomial addition result: \n"); head3 = poly_sum(head1,head2,head3); display(head3); break; case 3: exit(0); } } } Output: ~~~Menu~~~ 1.Represent and Evaluate a Polynomial P(x,y,z) 2.Find the sum of two polynomials POLY1(x,y,z) Enter your choice:1 ~~~~Polynomial evaluation P(x,y,z)~~~ Enter the no of terms in the polynomial: 5 Enter the 1 term: Coef = 6 Enter Pow(x) Pow(y) and Pow(z): 2 2 1 Enter the 2 term: Coef = -4 Enter Pow(x) Pow(y) and Pow(z): 0 1 5 Enter the 3 term: Coef = 3 Enter Pow(x) Pow(y) and Pow(z): 3 1 1 Enter the 4 term: Coef = 2Enter Pow(x) Pow(y) and Pow(z): 1 1 5 Enter the 5 term: Coef = -2 Enter Pow(x) Pow(y) and Pow(z): 1 1 3 Representation of Polynomial for evaluation: 6x^2y^2z^1 + -4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^1z^5 + -2x^1y^1z^3 Enter the value of x,y and z: 1 2 3 Result of polynomial evaluation is : -990 ~~~Menu~~~ 1.Represent and Evaluate a Polynomial P(x,y,z) 2.Find the sum of two polynomials POLY1(x,y,z) Enter your choice:2 Enter the POLY1(x,y,z): Enter the no of terms in the polynomial: 2 Enter the 1 term: Coef = 5 Enter Pow(x) Pow(y) and Pow(z): 1 2 6 Enter the 2 term: Coef = -6 Enter Pow(x) Pow(y) and Pow(z): 3 4 0 Polynomial 1 is: 5x^1y^2z^6 + -6x^3y^4z^0 Enter the POLY2(x,y,z): Enter the no of terms in the polynomial: 2 Enter the 1 term: Coef = 10 Enter Pow(x) Pow(y) and Pow(z): 1 2 6Enter the 2 term: Coef = 8 Enter Pow(x) Pow(y) and Pow(z): 1 4 5 Polynomial 2 is: 10x^1y^2z^6 + 8x^1y^4z^5 Polynomial addition result: 15x^1y^2z^6 + -6x^3y^4z^0 + 8x^1y^4z^510. Develop a menu driven Program in C for the following operations on Binary Search Tree (BST) of Integers . a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2 b. Traverse the BST in Inorder, Preorder and Post Order c. Search the BST for a given element (KEY) and report the appropriate message d. Exit Solution: #include <stdio.h> #include <stdlib.h> struct node{ int data; struct node *left, *right; }; typedef struct node NODE; NODE *root = NULL; NODE *create(int data){ NODE *temp; temp = (NODE *)malloc(sizeof(NODE)); temp->data = data; temp->left = NULL; temp->right = NULL; return temp; } NODE *insert(NODE *root, int data){ if (root == NULL) return create(data); else if (data < root->data) root->left = insert(root->left, data); else if (data > root->data) root->right = insert(root->right, data); else printf("Duplicate output\n"); return root; } void Inorder(NODE *root){ if (root != NULL){ Inorder(root->left); printf("%d ", root->data); Inorder(root->right); } }void Preorder(NODE *root){ if (root != NULL){ printf("%d ", root->data); Preorder(root->left); Preorder(root->right); } } void Postorder(NODE *root){ if (root != NULL){ Postorder(root->left); Postorder(root->right); printf("%d ", root->data); } } NODE *search(NODE *root, int key){ if (root == NULL || root->data == key) return root; else if (key < root->data) return search(root->left, key); else return search(root->right, key); } int main(){ int data, choice, key; NODE *temp; while (1){ printf("1. Create Tree\n"); printf("2. Inorder\n"); printf("3. Preorder\n"); printf("4. Postorder\n"); printf("5. Search\n"); printf("6. Exit\n"); printf("Enter the choice="); scanf("%d", &choice); switch (choice){ case 1: printf("Enter the element="); scanf("%d", &data);root = insert(root, data); break; case 2: Inorder(root); printf("\n"); break; case 3: Preorder(root); printf("\n"); break; case 4: Postorder(root); printf("\n"); break; case 5: printf("Enter the search element="); scanf("%d", &key); temp = search(root, key); if (temp == NULL) printf("No data found\n"); else printf("Data found\n"); break; case 6: exit(0); default: printf("Invalid choice\n"); break; } } printf("\n"); return 0; } Output: 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=61. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=9 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=5 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=2 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=8 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search6. Exit Enter the choice=1 Enter the element=15 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=24 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=14 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=7 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=8 Duplicate output 1. Create Tree2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=5 Duplicate output 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=1 Enter the element=2 Duplicate output 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=2 2 5 6 7 8 9 14 15 24 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=3 6 5 2 9 8 7 15 14 24 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search6. Exit Enter the choice=4 2 5 7 8 14 24 15 9 6 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=5 Enter the search element=15 Data found 1. Create Tree 2. Inorder 3. Preorder 4. Postorder 5. Search 6. Exit Enter the choice=611. Develop a Program in C for the following operations on Graph(G) of Cities b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method Solution: #include <stdio.h> int a[10][10], n, m, i, j, source, s[10], b[10]; int visited[10]; void create() { printf("\nEnter the number of vertices of the digraph: "); scanf("%d", &n); printf("\nEnter the adjacency matrix of the graph:\n"); for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) scanf("%d", &a[i][j]); } void bfs() { int q[10], u, front = 0, rear = -1; printf("\nEnter the source vertex to find other nodes reachable or not: "); scanf("%d", &source); q[++rear] = source; visited[source] = 1; printf("\nThe reachable vertices are: "); while (front <= rear) { u = q[front++]; for (i = 1; i <= n; i++) { if (a[u][i] == 1 && visited[i] == 0) { q[++rear] = i; visited[i] = 1; printf("\n%d", i); } } } } void dfs(int source) { int v, top = -1; s[++top] = 1; b[source] = 1; for (v = 1; v <= n; v++) { if (a[source][v] == 1 && b[v] == 0) { printf("\n%d -> %d", source, v); dfs(v); } } } void main() {int ch; while (1) { printf("\n1. Create Graph\n2. BFS\n3. Check graph connected or not (DFS)\n4. Exit"); printf("\nEnter your choice: "); scanf("%d", &ch); switch (ch) { case 1: create(); break; case 2: bfs(); for (i = 1; i <= n; i++) if (visited[i] == 0) printf("\nThe vertex that is not reachable %d", i); break; case 3: printf("\nEnter the source vertex to find the connectivity: "); scanf("%d", &source); m = 1; dfs(source); for (i = 1; i <= n; i++) { if (b[i] == 0) m = 0; } if (m == 1) printf("\nGraph is Connected"); else printf("\nGraph is not Connected"); break; default: exit(0); } } } /* Sample Output1 1. Create Graph 2. BFS 3. Check graph connected or not (DFS) 4. Exit Enter your choice: 1 Enter the number of vertices of the graph: 5 Enter the adjacency matrix of the graph: 01100 1 0 1 1 01 1 0 1 1 01101 00110 1. Create Graph 2. BFS 3. Check graph connected or not (DFS) 4. Exit Enter your choice: 2 Enter the source vertex to find other nodes reachable or not: 1 The reachable vertices are: 2 3 4 5 1. Create Graph 2. BFS 3. Check graph connected or not (DFS) 4. Exit Enter your choice: 3 Enter the source vertex to find the connectivity: 1 1 -> 2 2 -> 3 3 -> 4 4 -> 5 Graph is Connected 1. Create Graph 2. BFS 3. Check graph connected or not (DFS) 4. Exit Enter your choice: 4 ====================================== Sample Output2: 1. Create Graph 2. BFS 3. Check graph connected or not (DFS) 4. Exit Enter your choice: 1 Enter the number of vertices of the graph: 6 Enter the adjacency matrix of the graph: 010000 100000 000100 001000 000001 000010 1. Create Graph2. BFS 3. Check graph connected or not (DFS) 4. Exit Enter your choice: 2 Enter the source vertex to find other nodes reachable or not: 1 The reachable vertices are: 2 1. Create Graph 2. BFS 3. Check graph connected or not (DFS) 4. Exit Enter your choice: 3 Enter the source vertex to find the connectivity: 1 1 -> 2 Graph is not Connected 1. Create Graph 2. BFS 3. Check graph connected or not (DFS) 4. Exit Enter your choice: 4 */ 12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L are Integers. Develop a Program in C that uses Hash function H: K →L as H(K)=K mod m (remainder method), and implement hashing technique to map a given key K to the address space L. Resolve the collision (if any) using linear probing Solution: #include<stdio.h> #include<stdlib.h> #define MAX_ADDR 5 struct employee { int emp_id, emp_age; char emp_name[25]; }emp[MAX_ADD R]; void main() { int i, ch, count = 0,index, haddr, id, flag = 0; //clrscr(); for(;;) { printf("Enter 1 to insert record \n 2. to search record\n"); scanf("%d", &ch); switch(ch) { case 1: if(count == MAX_ADDR) { printf("No free addres space\n"); break; } printf("Enter employee id\n"); scanf("%d", &id); haddr = hash(id); printf("Home address is %d\n", haddr); for(i=0; i<MAX_ADDR; i++) { index = (haddr+i)%MAX_ ADDR; if(emp[index].emp _id == 0) { emp[index].emp_i d = id; printf("Enter the employee name\n"); scanf("%s", emp[index].emp_name); printf("Enter the employee age\n"); scanf("%d", &emp[index].emp _age); count++; printf("Successful y inserted at Actual Address %d:\n\n", index); break; } } break; case 2: printf("?Enter employee id to be searched\n"); scanf("%d",&id); haddr = hash(id); for(i=0; i<MAX_ADDR; i++) { index = (haddr+i)%MAX_ ADDR; if(emp[index].emp _id == 0) { flag = 1; break; } else if(emp[index].emp _id == id) { printf ("Employee id is%d\n", emp[index].emp_i d); printf("Employeename is %s\n", emp[index].emp_n ame); printf("Employee age is %d\n", emp[index].emp_a ge); printf("Search Length is: %d\n", ++i); break; } } if(flag == 1 || i == MAX_ADDR) { printf("Key not present\n"); } break; default:exit(0); } } } int hash (int key) { return key % MAX_ADDR; } Output: Enter 1 to insert record 2. to search record 1 Enter employee id 213 Home address is 3 Enter the employee name prathi Enter the employee age 45 Successfuly inserted at Actual Address 3: Enter 1 to insert record2. to search record 1 Enter employee id 6 Home address is 1 Enter the employee name mani Enter the employee age 89 Successfuly inserted at Actual Address 1: Enter 1 to insert record 2. to search record 2 Enter employee id to be searched 6 Employee id is6 Employee name is mani Employee age is 89 Search Length is: 1 Enter 1 to insert record 2. to search record 1 Enter employee id 6 Home address is 1 Enter the employee name Phanindra Enter the employee age 75 Successfuly inserted at Actual Address 2: Enter 1 to insert record 2. to search record 2 Enter employee id to be searched 6 Employee id is6 Employee name is mani Employee age is 89 Search Length is: 1 Enter 1 to insert record 2. to search recordOutput: 1.Insert 2.Search 3.Display 4.Exit Enter the choice 1 Enter Employee id, name, designation=16 XYZ CEO 1.Insert 2.Search 3.Display 4.Exit Enter the choice 2 Enter employee key to be searched: 16 Employee found 1.Insert 2.Search 3.Display 4.Exit Enter the choice 3 id=16, Name=XYZ Des=CEO 1.Insert 2.Search 3.Display 4.Exit Enter the choice 4