BinarySearch.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <stdio.h> void main(){ int A[50],i,low,mid,high,n,key; printf("Enter number of elements"); scanf("%d",&n); printf("Enter elements in array"); for (i=0;i<n;i++){ scanf("%d",&A[i]); } printf("Enter value to find"); scanf("%d",&key); low = 0; high = n-1; mid = (low+high)/2; while (low<=high){ mid = (low+high)/2; if (A[mid] < key){ low=mid+1; } else if(A[mid]==key){ printf("Element found!"); break; } else{ high = mid-1; } if (low>high){ printf("Not found"); } } }

CircularQueue.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #include <stdio.h> #define MAX 3 int queue[MAX]; int front = -1; int rear = -1; void enqueue(int element){ if (front == -1 && rear == -1){ front = 0; rear = 0; queue[rear] = element; } else if((rear+1)%MAX == front){ printf("\nQueue overflow\n"); } else{ rear = (rear+1)%MAX; queue[rear] = element; } } int dequeue(){ if (front == -1 && rear == -1){ printf("\nQueue underflow\n"); } else if(front == rear){ printf("The dequeued element is %d",queue[front]); front = -1; rear = -1; } else{ printf("\n The dequeued element is %d",queue[front]); front = (front+1)%MAX; } } int display(){ int i = front; if (front == -1 && rear ==-1){ printf("\nQueue is empty\n"); } else if((rear+1)%MAX == front){ i = 0; while(i<MAX){ printf("\n%d",queue[i]); i = i+1; } } else{ printf("\n===========================\n"); printf("Elements in the queue are"); while(i<=rear){ printf("\n%d",queue[i]); i = (i+1)%(MAX+front); } printf("\n===========================\n"); } } void main(){ int choice = 1,x; while(choice < 4 && choice!=0){ printf("\n===========================\n"); printf("1.Insert into queue\n"); printf("2.Delete from queue\n"); printf("3.Display from queue\n"); printf("4.Exit\n"); printf("\n===========================\n"); printf("Enter your choice"); scanf("%d",&choice); switch(choice){ case 1 : printf("Enter the element to be inserted"); scanf("%d",&x); enqueue(x); break; case 2 : dequeue(); break; case 3 : display(); break; } } }

Dequeue.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 #include <stdio.h> #define MAX 10 int queue[MAX]; int front = -1, rear = -1; void insert_rearend(); void insert_frontend(); void delete_rearend(); void delete_frontend(); void display(); void main(){ int choice; do{ printf("\n--------------------\n"); printf("\n1.Insert at rear end\n"); printf("\n2.Insert at front end\n"); printf("\n3.Delete at rear end\n"); printf("\n4.Delete at front end\n"); printf("\n5.Display\n"); printf("\n6.Exit\n"); printf("Enter your choice"); scanf("%d",&choice); switch(choice){ case 1: insert_rearend(); break; case 2: insert_frontend(); break; case 3: delete_rearend(); break; case 4: delete_frontend(); break; case 5: display(); break; } }while (choice!=6); } void insert_rearend(){ int val; printf("Enter value to be added"); scanf("%d",&val); if ((front == 0 && rear == MAX-1) || (front == rear+1)) printf("OverFlow"); if (front == -1){ front = 0; rear = 0; } else{ if (rear == MAX-1) rear = 0; else rear = rear+1; } queue[rear] = val; } void insert_frontend(){ int val; printf("Enter value to be added"); scanf("%d",&val); if ((front == 0 && rear == MAX-1) || (front == rear+1)) printf("OverFlow"); if (front == -1){ front = 0; rear = 0; } else{ if (front == 0) front = MAX-1; else front = front -1; } queue[front] = val; } void delete_rearend(){ if(front == -1){ printf("Underflow"); return; } printf("Deleted Element is %d\n",queue[rear]); if (front ==rear){ front = -1; rear = -1; } else{ rear = rear -1; } } void delete_frontend(){ if(front == -1){ printf("Underflow"); return; } printf("Deleted Element is %d\n",queue[front]); if (front ==rear){ front = -1; rear = -1; } else{ if (front == MAX -1) front = 0; else front = front+1; } } void display(){ if (front == -1){ printf("\n queue is empty\n"); return; } printf("\n The elements in queue are...\n"); if(front <=rear){ int tempFront = front; while(tempFront<=rear){ printf("%d\t",queue[tempFront]); tempFront++; } } else{ int tempFront = front; while(tempFront<=MAX-1){ printf("%d\t",queue[tempFront]); tempFront++; } tempFront = 0; while(tempFront<=rear){ printf("%d\t",queue[tempFront]); tempFront++; } } printf("\n"); }

LinearSearch.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <stdio.h> void main(){ int A[50],len,i,j,key,flag=0; printf("Enter length of the array"); scanf("%d",&len); printf("Enter elements in the array"); for (i=0;i<len;i++){ scanf("%d",&A[i]); } printf("Enter the element to be searched"); scanf("%d",&key); for (i=0;i<len;i++){ if (A[i]==key){ flag=1; break; } } if (flag==1){ printf("Element found!"); } else{ printf("Element not found"); } }

Polynomials.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include <stdio.h> void main(){ int A[50][50],B[50],i,j; int polyno,polydegree; int maxpolydegree; printf("Enter number of polynomials\n"); scanf("%d",&polyno); for(i=0;i<polyno;i++){ printf("Enter Polynomial no %d\n",i+1); printf("Enter the degree of the polynomial\n"); scanf("%d",&polydegree); if (polydegree>maxpolydegree) maxpolydegree = polydegree; for(j=0;j<=polydegree;j++){ if (j==0){ printf("Enter the constant\n"); scanf("%d",&A[i][j]);} else{ printf("Enter the coefficient of x^%d\n",j); scanf("%d",&A[i][j]); } } } printf("Given polynomial data"); for(i=0;i<polyno;i++){ printf("\n"); for(j=0;j<=maxpolydegree;j++){ printf("%d \t",A[i][j]); } } for(i=0;i<=maxpolydegree;i++){ for(j=0;j<=polyno;j++){ B[i] += A[j][i]; } } printf("\nSum of the polynomials\n"); for(i=maxpolydegree;i>=0;i--){ if (i==0) printf("%d \t",B[i]); else printf("%d x^%d + ",B[i],i); } printf("\n"); }

PriorityQueue.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 #include<stdio.h> #include<limits.h> #define MAX 100 int idx = -1; int pqVal[MAX]; int pqPriority[MAX]; int isEmpty() { return idx == -1; } int isFull() { return idx == MAX - 1; } void enqueue(int data, int priority) { if(!isFull()){ idx++; pqVal[idx] = data; pqPriority[idx] = priority; } } int peek() { int maxPriority = INT_MIN; int indexPos = -1; for (int i = 0; i <= idx; i++) { if (maxPriority == pqPriority[i] && indexPos > -1 && pqVal[indexPos] < pqVal[i]){ maxPriority = pqPriority[i]; indexPos = i; } else if (maxPriority < pqPriority[i]) { maxPriority = pqPriority[i]; indexPos = i; } } return indexPos; } void dequeue() { if(!isEmpty()){ int indexPos = peek(); for (int i = indexPos; i < idx; i++){ pqVal[i] = pqVal[i + 1]; pqPriority[i] = pqPriority[i + 1]; } idx--; } } void display() { for (int i = 0; i <= idx; i++) { printf("(%d, %d)\n",pqVal[i], pqPriority[i]); } } void main() { int x,y,ch; do { printf("\t==== Menu ====\n"); printf("\t 1. Enqueue\n"); printf("\t 2. Dequeue\n"); printf("\t 3. Display\n"); printf("\t 0. Exit\n"); printf("Your Choice :- \t"); scanf("%d",&ch); switch(ch){ case 1: printf("Enter value, its priority :- \n"); scanf("%d%d",&x,&y); enqueue(x,y); break; case 2: dequeue(); break; case 3: display(); break; case 0: break; default: printf("Invalid Choice .....\n"); } } while(ch != 0); }

Queue.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #include <stdio.h> #include <stdlib.h> #define MAX 5 int queue_array[MAX]; int rear = -1; int front = -1; void insert(){ int item; if(rear == MAX-1) printf("\nQueue Overflow\n"); else{ if(front == -1) front = 0; printf("\nInsert element in queue\n"); scanf("%d",&item); rear = rear + 1; queue_array[rear] = item; } } void delete(){ if(front == -1 || front > rear){ printf("\nQueue underflow\n"); } else{ printf("\nElement deleted from queue is %d",queue_array[front]); front = front + 1; } } void display(){ int i; if (front == -1){ printf("\nQueue is empty\n"); } else{ printf("\nQueue is..\n"); for(i=front;i<=rear;i++){ printf("%d",queue_array[i]); printf("\n"); } } } int main(){ int choice; while(1){ printf("\n1.Insert\n2.Delete\n3.Display\n4.Quit"); printf("\nEnter your choice"); scanf("%d",&choice); switch(choice){ case 1:insert(); break; case 2:delete(); break; case 3:display(); break; case 4:exit(0); break; } } }

SparseMatrix.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include <stdio.h> void main(){ int a[50][50],n,m,i,j,count=0,k=0,b[3][20],countn=0; printf("Enter the no of row and columns"); scanf("%d%d",&n,&m); printf("Enter elements in matrix"); for(i=0;i<n;i++){ for(j=0;j<m;j++){ scanf("%d",&a[i][j]); } } for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(a[i][j]==0) count = count+1; else countn++; } } for(i=0;i<n;i++){ printf("\n"); for(j=0;j<m;j++){ printf("%d \t",a[i][j]); } } if (count>(m*n)/2){ printf("\n Its a sparse matrix"); } else{ printf("\n Its not a sparse matrix"); } while(k!=countn){ for(i=0;i<3;i++){ for(j=0;j<m;j++){ if(a[i][j]!=0){ k++; b[0][k-1]=i; b[1][k-1]=j; b[2][k-1] = a[i][j]; } } } } for(i=0;i<3;i++){ printf("\n"); for(j=0;j<countn;j++){ printf("%d\t",b[i][j]); } } }

Stack.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include <stdio.h> #include <stdlib.h> #define size 4 int top=-1,array[size]; void push(){ int x; if (top==size-1) printf("\n Overflow!\n"); else{ printf("\n Enter element to be inserted to stack"); scanf("%d",&x); top=top+1; array[top] = x; } } void pop(){ if (top == -1) printf("\nUnderflow\n"); else{ printf("Popped element %d",array[top]); top = top-1; } } void show(){ if (top == -1) printf("\nUnderflow\n"); else{ printf("\n Elements present in stack \n"); for(int i=top;i>=0;--i) printf("%d \n",array[i]); } } void main(){ int choice; while(1){ printf("Operations performed by stack\n"); printf("1.Push\n2.Pop\n3.Show\n4.End"); printf("Enter the choice"); scanf("%d",&choice); switch(choice){ case 1:push(); break; case 2:pop(); break; case 3:show(); break; case 4:exit(0); } } }

binarytreelinkedlist.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 #include<stdio.h> #include<stdlib.h> typedef struct bin { int data; struct bin *left; struct bin *right; } node; void insert(node *, node *); void inorder(node *); void preorder(node *); void postorder(node *); int height(node *root); void getLastNodeAndItsParent(node *root, int level, node *parent); void deleteNode(node *root); node *get_node(); node *lastNode, *parentOfLastNode; void main() { int choice; char ans = 'n'; node *new, *root; root = NULL; do{ printf("\n\t==== MENU ====\n"); printf("\t 1. Create\n"); printf("\t 2. Inorder\n"); printf("\t 3. Pre-order\n"); printf("\t 4. Post-Order\n"); printf("\t 5. Delete A Node\n"); printf("\t 6. Exit\n"); printf("\nYour Choice :- \t"); scanf("%d",&choice); switch(choice) { case 1: new = get_node(); printf("\t Enter the element :- \t"); scanf("%d",&new->data); if(root == NULL) root = new; else insert(root,new); break; case 2: if(root == NULL) printf("Tree is not created, It is empty.\n"); else inorder(root); break; case 3: if(root == NULL) printf("Tree is not created, It is empty.\n"); else preorder(root); break; case 4: if(root == NULL) printf("Tree is not created, It is empty.\n"); else postorder(root); break; case 5: if(root == NULL) printf("Tree is Empty, Cannot perform Deletion..\n"); else deleteNode(root); break; case 6: break; default: printf("Invalid Choice....\n"); } } while(choice != 6); } node *get_node(){ node *temp; temp = (node*) malloc(sizeof(node)); temp->left = NULL; temp->right = NULL; return temp; } int height(node *root){ int maximum, lheight, rheight; if (root == NULL) return 0; lheight = height(root->left) + 1; rheight = height(root->right) + 1; maximum = (lheight>rheight)?lheight:rheight; return maximum; } void getLastNodeAndItsParent(node *root, int level, node *parent) { if (root == NULL) return; if (level == 1){ lastNode = root; parentOfLastNode = parent; } getLastNodeAndItsParent(root->left, level - 1, root); getLastNodeAndItsParent(root->right, level - 1, root); } void insert(node *root, node *new){ int ch; printf("\t Where to insert left/right of %d ? (L - 1/R - 2) :- \t",root->data); scanf("%d",&ch); if(ch == 2){ if(root->right == NULL){ root->right = new; } else{ insert(root->right, new); } } else{ if(root->left == NULL){ root->left = new; } else{ insert(root->left, new); } } } void deleteNode(node *root){ int levelOfLastNode = height(root); getLastNodeAndItsParent(root, levelOfLastNode, NULL); if (lastNode && parentOfLastNode){ if (parentOfLastNode->right){ printf("Deleted Node is %d\n",(parentOfLastNode->right)->data); parentOfLastNode->right = NULL; } else{ printf("Deleted Node is %d\n",(parentOfLastNode->left)->data); parentOfLastNode->left = NULL; } } else printf("\tEmpty Tree..\n"); } void inorder(node *temp) { if(temp != NULL){ inorder(temp->left); printf("%d\t",temp->data); inorder(temp->right); } } void preorder(node *temp) { if(temp != NULL){ printf("%d\t",temp->data); inorder(temp->left); inorder(temp->right); } } void postorder(node *temp) { if(temp != NULL){ inorder(temp->left); inorder(temp->right); printf("%d\t",temp->data); } }

infixtopostfixandeval.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 #define SIZE 50 #include <ctype.h> #include <stdio.h> #include <math.h> char s[SIZE]; int top = -1; void push(char elem){ s[++top] = elem; } char pop(){ return (s[top--]); } int priority(char elem) { switch (elem) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*': case '/': return 3; } } void infix_to_postfix(char infix[], char postfix[]){ char ch, elem; int i = 0, k = 0; push('#'); while ((ch = infix[i++]) != '\n'){ if (ch == '(') push(ch); else if (isalnum(ch)) postfix[k++] = ch; else if (ch == ')'){ while (s[top] != '(') postfix[k++] = pop(); elem = pop(); } else{ while (priority(s[top]) >= priority(ch)) postfix[k++] = pop(); push(ch); } } while (s[top] != '#') postfix[k++] = pop(); postfix[k] = 0; } int eval_postfix(char *postfix) { char ch; int i = 0, op1, op2; while((ch = postfix[i++]) != 0) { if(isdigit(ch)) push(ch-'0'); else { op2 = pop(); op1 = pop(); switch(ch) { case '+' : push(op1+op2); break; case '-' : push(op1-op2); break; case '*' : push(op1*op2); break; case '/' : push(op1/op2); break; case '^': push(pow(op1,op2)); break; } } } return s[top]; } char infx[50], pofx[50]; void main() { printf("\nInput the infix expression: "); fgets(infx, 50, stdin); infix_to_postfix(infx, pofx); printf("\nGiven Infix Expression: %sPostfix Expression: %s", infx, pofx); top = -1; printf("\nResult of evaluation of postfix expression : %d\n", eval_postfix(pofx)); }

polynomialAddnMult.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 #include <stdio.h> #include <stdlib.h> struct poly{ int coefficient; int exponential; struct poly *ptr; }; struct poly *p =NULL,*q=NULL,*r=NULL; struct poly *pptr,*qptr; struct poly *add(struct poly *header,int coeff,int expo){ struct poly *newnode,*temp=header; newnode = (struct poly*)malloc(sizeof(struct poly)); newnode->coefficient = coeff; newnode->exponential = expo; newnode->ptr = NULL; if (header==NULL){ header = newnode; } else{ while(temp->ptr!=NULL){ temp = temp -> ptr; } temp -> ptr = newnode; return header; } } void display(struct poly *header){ struct poly *current = header; if(header==NULL) printf("list is empty"); else{ while(current!=NULL){ if(current->ptr!=NULL){ printf(" %d x^%d +",current->coefficient,current->exponential); } else{ printf(" %d x^%d",current->coefficient,current->exponential); } current = current->ptr; } } } void addition(struct poly *p,struct poly *q){ int result; pptr= p,qptr = q; if(pptr==NULL){ printf("pptr is null"); } while((pptr!=NULL)&&(qptr!=NULL)){ if (pptr->exponential > qptr->exponential){ printf("abc"); r = add(r,pptr->coefficient,pptr->exponential); pptr = pptr -> ptr; } else if(pptr->exponential < qptr->exponential){ printf("abc"); r = add(r,qptr->coefficient,qptr->exponential); qptr = qptr->ptr; } else{ result = qptr->coefficient,qptr->coefficient; r= add(r,pptr->coefficient+qptr->coefficient,pptr -> exponential); qptr = qptr->ptr; pptr = pptr->ptr; } } while(pptr!=NULL){ r=add(r,pptr->coefficient,pptr->exponential); pptr = pptr->ptr; } while(qptr!=NULL){ r=add(r,qptr->coefficient,qptr -> exponential); qptr = qptr->ptr; } } struct poly *sort_add(struct poly *header,int coeff,int expo){ struct poly *next = header,*prev = NULL; struct poly *newnode; newnode = (struct poly *) malloc(sizeof(struct poly*)); newnode -> coefficient = coeff; newnode -> exponential = expo; newnode -> ptr=NULL; if(header == NULL){ header = newnode; } else{ while(next!=NULL && next -> exponential > expo){ prev = next; next = next ->ptr; } if(prev == NULL){ if (next ->exponential ==expo){ next -> coefficient = coeff+ next->coefficient; free(newnode); } else{ newnode -> ptr = next; header = newnode; } } else{ if((next!=NULL) && (next ->exponential ==expo)){ next -> coefficient = next -> coefficient +coeff; free(newnode); } else{ prev -> ptr = newnode; newnode -> ptr = next; } } return header; } } void multiplication(struct poly *p,struct poly *q){ int coeff,expo; pptr=p; qptr = q; while(pptr!=NULL){ qptr = q; while(qptr!=NULL){ coeff = pptr->coefficient * qptr->coefficient; expo = pptr->exponential + qptr->exponential; r= sort_add(r,coeff,expo); qptr = qptr -> ptr; } pptr = pptr->ptr; } } void main(){ int a,n,coeff,expo; //printf("polynomial representation"); printf("Enter number of elements in 1st polynomial"); scanf("%d",&n); for(int i=0;i<n;i++){ printf("Enter coefficient "); scanf("%d",&coeff); printf("Enter exponential "); scanf("%d",&expo); p = add(p,coeff,expo); printf("\n"); } printf("Enter no of elements in 2nd polynomial"); scanf("%d",&n); for(int i=0;i<n;i++){ printf("Enter the coefficient"); scanf("%d",&coeff); printf("Enter exponential"); scanf("%d",&expo); q = add(q,coeff,expo); printf("\n"); } printf("Polynomial 1 is "); display(p); printf("\n"); printf("Polynomial 2 is "); display(q); printf("\n 1.Addition \n 2. Multiplication "); scanf("%d",&a); switch(a){ case 1: addition(p,q); printf("The result is"); display(r); break; case 2: multiplication(p,q); printf("The result is"); display(r); break; default: printf("Invalid Choice"); break; } }

singlylinkedlist.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 #include<stdlib.h> #include<stdio.h> void display(); void insert_begin(); void insert_end(); void insert_pos(); void delete_begin(); void delete_end(); void delete_pos(); struct node{ int info; struct node *next; }; struct node *start = NULL; int main(){ int choice; printf("\n Menu \n"); printf("\n1.Display"); printf("\n2.Insert at the beginning"); printf("\n3.Insert at the end"); printf("\n4.Insert at specified position"); printf("\n5.Delete from beginning"); printf("\n6.Delete from the end"); printf("\n7.Delete from specific position"); printf("\n8.Exit"); while(1){ printf("\n Enter your choice"); scanf("%d",&choice); switch(choice){ case 1: display(); break; case 2: insert_begin(); break; case 3: insert_end(); break; case 4: insert_pos(); break; case 5: delete_begin(); break; case 6: delete_end(); break; case 7: delete_pos(); break; case 8: exit(0); break; default: printf("\n Wrong choice"); } } return 0; } void display(){ struct node *ptr; if(start==NULL){ printf("\n List is empty"); return; } else{ ptr=start; printf("In the list elements are\n"); while(ptr!=NULL){ printf("%d\t",ptr->info); ptr=ptr->next; } } } void insert_begin(){ struct node *temp; temp = (struct node*)malloc(sizeof(struct node)); if(temp==NULL){ printf("\n Overflow"); return; } printf("\n Enter the data value of the node \t"); scanf("%d",&temp->info); temp->next = NULL; if(start==NULL) start = temp; else{ temp->next=start; start = temp; } } void insert_end(){ struct node *temp,*ptr; temp = (struct node*)malloc(sizeof(struct node)); if(temp==NULL){ printf("\n Overflow"); return; } printf("\n Enter the data value of the node \t"); scanf("%d",&temp->info); temp->next = NULL; if(start==NULL) start = temp; else{ ptr=start; while(ptr->next!=NULL){ ptr=ptr->next; } ptr->next = temp; } } void insert_pos(){ struct node *ptr,*temp; int i,pos; temp = (struct node*)malloc(sizeof(struct node)); if(temp==NULL){ printf("\n Overflow"); return; } printf("\n Enter the position for the new node to be inserted \t"); scanf("%d",&pos); printf("\n Enter the data value of the node \t"); scanf("%d",&temp->info); temp->next = NULL; if(pos==0){ temp->next=start; start = temp; } else{ for(i=0,ptr=start;i<pos-1;i++){ ptr = ptr->next; if(ptr==NULL){ printf("\n Position not found"); return; } } temp->next = ptr->next; ptr->next = temp; } } void delete_begin(){ struct node *ptr; if(start==NULL){ printf("\n Underflow"); return; } else{ printf("1"); ptr=start; printf("2"); start = start->next; printf("\n The deleted element is %d \t",ptr->info); free(ptr); } } void delete_end(){ struct node *temp, *ptr; if(start==NULL){ printf("\n Underflow"); return; } else if(start->next==NULL){ ptr=start; start=NULL; printf("\n The deleted element is %d",ptr->info); free(ptr); } else{ ptr=start; while(ptr->next!=NULL){ temp = ptr; ptr=ptr->next; } temp->next = NULL; printf("\n The deleted element is %d \t",ptr->info); free(ptr); } } void delete_pos(){ int i,pos; struct node *ptr, *temp; if(start==NULL){ printf("\n Underflow"); exit(0); } else{ printf("\n Enter the position to be deleted"); scanf("%d",&pos); if(pos == 0){ ptr = start; start = start->next; printf("\n The deleted element is %d \t",ptr->info); free(ptr); } else{ ptr = start; for(i=0;i<pos;i++){ temp = ptr; ptr = ptr->next; if(ptr == NULL){ printf("\n Position not found"); return; } } temp->next = ptr->next; printf("\n The deleted element is %d \t",ptr->info); free(ptr); } } }

Sorting Programs

BinarySearch.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <stdio.h> void main(){ int A[50],i,low,mid,high,n,key; printf("Enter number of elements"); scanf("%d",&n); printf("Enter elements in array"); for (i=0;i<n;i++){ scanf("%d",&A[i]); } printf("Enter value to find"); scanf("%d",&key); low = 0; high = n-1; mid = (low+high)/2; while (low<=high){ mid = (low+high)/2; if (A[mid] < key){ low=mid+1; } else if(A[mid]==key){ printf("Element found!"); break; } else{ high = mid-1; } if (low>high){ printf("Not found"); } } }

CircularQueue.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #include <stdio.h> #define MAX 3 int queue[MAX]; int front = -1; int rear = -1; void enqueue(int element){ if (front == -1 && rear == -1){ front = 0; rear = 0; queue[rear] = element; } else if((rear+1)%MAX == front){ printf("\nQueue overflow\n"); } else{ rear = (rear+1)%MAX; queue[rear] = element; } } int dequeue(){ if (front == -1 && rear == -1){ printf("\nQueue underflow\n"); } else if(front == rear){ printf("The dequeued element is %d",queue[front]); front = -1; rear = -1; } else{ printf("\n The dequeued element is %d",queue[front]); front = (front+1)%MAX; } } int display(){ int i = front; if (front == -1 && rear ==-1){ printf("\nQueue is empty\n"); } else if((rear+1)%MAX == front){ i = 0; while(i<MAX){ printf("\n%d",queue[i]); i = i+1; } } else{ printf("\n===========================\n"); printf("Elements in the queue are"); while(i<=rear){ printf("\n%d",queue[i]); i = (i+1)%(MAX+front); } printf("\n===========================\n"); } } void main(){ int choice = 1,x; while(choice < 4 && choice!=0){ printf("\n===========================\n"); printf("1.Insert into queue\n"); printf("2.Delete from queue\n"); printf("3.Display from queue\n"); printf("4.Exit\n"); printf("\n===========================\n"); printf("Enter your choice"); scanf("%d",&choice); switch(choice){ case 1 : printf("Enter the element to be inserted"); scanf("%d",&x); enqueue(x); break; case 2 : dequeue(); break; case 3 : display(); break; } } }

Dequeue.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 #include <stdio.h> #define MAX 10 int queue[MAX]; int front = -1, rear = -1; void insert_rearend(); void insert_frontend(); void delete_rearend(); void delete_frontend(); void display(); void main(){ int choice; do{ printf("\n--------------------\n"); printf("\n1.Insert at rear end\n"); printf("\n2.Insert at front end\n"); printf("\n3.Delete at rear end\n"); printf("\n4.Delete at front end\n"); printf("\n5.Display\n"); printf("\n6.Exit\n"); printf("Enter your choice"); scanf("%d",&choice); switch(choice){ case 1: insert_rearend(); break; case 2: insert_frontend(); break; case 3: delete_rearend(); break; case 4: delete_frontend(); break; case 5: display(); break; } }while (choice!=6); } void insert_rearend(){ int val; printf("Enter value to be added"); scanf("%d",&val); if ((front == 0 && rear == MAX-1) || (front == rear+1)) printf("OverFlow"); if (front == -1){ front = 0; rear = 0; } else{ if (rear == MAX-1) rear = 0; else rear = rear+1; } queue[rear] = val; } void insert_frontend(){ int val; printf("Enter value to be added"); scanf("%d",&val); if ((front == 0 && rear == MAX-1) || (front == rear+1)) printf("OverFlow"); if (front == -1){ front = 0; rear = 0; } else{ if (front == 0) front = MAX-1; else front = front -1; } queue[front] = val; } void delete_rearend(){ if(front == -1){ printf("Underflow"); return; } printf("Deleted Element is %d\n",queue[rear]); if (front ==rear){ front = -1; rear = -1; } else{ rear = rear -1; } } void delete_frontend(){ if(front == -1){ printf("Underflow"); return; } printf("Deleted Element is %d\n",queue[front]); if (front ==rear){ front = -1; rear = -1; } else{ if (front == MAX -1) front = 0; else front = front+1; } } void display(){ if (front == -1){ printf("\n queue is empty\n"); return; } printf("\n The elements in queue are...\n"); if(front <=rear){ int tempFront = front; while(tempFront<=rear){ printf("%d\t",queue[tempFront]); tempFront++; } } else{ int tempFront = front; while(tempFront<=MAX-1){ printf("%d\t",queue[tempFront]); tempFront++; } tempFront = 0; while(tempFront<=rear){ printf("%d\t",queue[tempFront]); tempFront++; } } printf("\n"); }

LinearSearch.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <stdio.h> void main(){ int A[50],len,i,j,key,flag=0; printf("Enter length of the array"); scanf("%d",&len); printf("Enter elements in the array"); for (i=0;i<len;i++){ scanf("%d",&A[i]); } printf("Enter the element to be searched"); scanf("%d",&key); for (i=0;i<len;i++){ if (A[i]==key){ flag=1; break; } } if (flag==1){ printf("Element found!"); } else{ printf("Element not found"); } }

Polynomials.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include <stdio.h> void main(){ int A[50][50],B[50],i,j; int polyno,polydegree; int maxpolydegree; printf("Enter number of polynomials\n"); scanf("%d",&polyno); for(i=0;i<polyno;i++){ printf("Enter Polynomial no %d\n",i+1); printf("Enter the degree of the polynomial\n"); scanf("%d",&polydegree); if (polydegree>maxpolydegree) maxpolydegree = polydegree; for(j=0;j<=polydegree;j++){ if (j==0){ printf("Enter the constant\n"); scanf("%d",&A[i][j]);} else{ printf("Enter the coefficient of x^%d\n",j); scanf("%d",&A[i][j]); } } } printf("Given polynomial data"); for(i=0;i<polyno;i++){ printf("\n"); for(j=0;j<=maxpolydegree;j++){ printf("%d \t",A[i][j]); } } for(i=0;i<=maxpolydegree;i++){ for(j=0;j<=polyno;j++){ B[i] += A[j][i]; } } printf("\nSum of the polynomials\n"); for(i=maxpolydegree;i>=0;i--){ if (i==0) printf("%d \t",B[i]); else printf("%d x^%d + ",B[i],i); } printf("\n"); }

PriorityQueue.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 #include<stdio.h> #include<limits.h> #define MAX 100 int idx = -1; int pqVal[MAX]; int pqPriority[MAX]; int isEmpty() { return idx == -1; } int isFull() { return idx == MAX - 1; } void enqueue(int data, int priority) { if(!isFull()){ idx++; pqVal[idx] = data; pqPriority[idx] = priority; } } int peek() { int maxPriority = INT_MIN; int indexPos = -1; for (int i = 0; i <= idx; i++) { if (maxPriority == pqPriority[i] && indexPos > -1 && pqVal[indexPos] < pqVal[i]){ maxPriority = pqPriority[i]; indexPos = i; } else if (maxPriority < pqPriority[i]) { maxPriority = pqPriority[i]; indexPos = i; } } return indexPos; } void dequeue() { if(!isEmpty()){ int indexPos = peek(); for (int i = indexPos; i < idx; i++){ pqVal[i] = pqVal[i + 1]; pqPriority[i] = pqPriority[i + 1]; } idx--; } } void display() { for (int i = 0; i <= idx; i++) { printf("(%d, %d)\n",pqVal[i], pqPriority[i]); } } void main() { int x,y,ch; do { printf("\t==== Menu ====\n"); printf("\t 1. Enqueue\n"); printf("\t 2. Dequeue\n"); printf("\t 3. Display\n"); printf("\t 0. Exit\n"); printf("Your Choice :- \t"); scanf("%d",&ch); switch(ch){ case 1: printf("Enter value, its priority :- \n"); scanf("%d%d",&x,&y); enqueue(x,y); break; case 2: dequeue(); break; case 3: display(); break; case 0: break; default: printf("Invalid Choice .....\n"); } } while(ch != 0); }

Queue.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #include <stdio.h> #include <stdlib.h> #define MAX 5 int queue_array[MAX]; int rear = -1; int front = -1; void insert(){ int item; if(rear == MAX-1) printf("\nQueue Overflow\n"); else{ if(front == -1) front = 0; printf("\nInsert element in queue\n"); scanf("%d",&item); rear = rear + 1; queue_array[rear] = item; } } void delete(){ if(front == -1 || front > rear){ printf("\nQueue underflow\n"); } else{ printf("\nElement deleted from queue is %d",queue_array[front]); front = front + 1; } } void display(){ int i; if (front == -1){ printf("\nQueue is empty\n"); } else{ printf("\nQueue is..\n"); for(i=front;i<=rear;i++){ printf("%d",queue_array[i]); printf("\n"); } } } int main(){ int choice; while(1){ printf("\n1.Insert\n2.Delete\n3.Display\n4.Quit"); printf("\nEnter your choice"); scanf("%d",&choice); switch(choice){ case 1:insert(); break; case 2:delete(); break; case 3:display(); break; case 4:exit(0); break; } } }

SparseMatrix.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include <stdio.h> void main(){ int a[50][50],n,m,i,j,count=0,k=0,b[3][20],countn=0; printf("Enter the no of row and columns"); scanf("%d%d",&n,&m); printf("Enter elements in matrix"); for(i=0;i<n;i++){ for(j=0;j<m;j++){ scanf("%d",&a[i][j]); } } for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(a[i][j]==0) count = count+1; else countn++; } } for(i=0;i<n;i++){ printf("\n"); for(j=0;j<m;j++){ printf("%d \t",a[i][j]); } } if (count>(m*n)/2){ printf("\n Its a sparse matrix"); } else{ printf("\n Its not a sparse matrix"); } while(k!=countn){ for(i=0;i<3;i++){ for(j=0;j<m;j++){ if(a[i][j]!=0){ k++; b[0][k-1]=i; b[1][k-1]=j; b[2][k-1] = a[i][j]; } } } } for(i=0;i<3;i++){ printf("\n"); for(j=0;j<countn;j++){ printf("%d\t",b[i][j]); } } }

Stack.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include <stdio.h> #include <stdlib.h> #define size 4 int top=-1,array[size]; void push(){ int x; if (top==size-1) printf("\n Overflow!\n"); else{ printf("\n Enter element to be inserted to stack"); scanf("%d",&x); top=top+1; array[top] = x; } } void pop(){ if (top == -1) printf("\nUnderflow\n"); else{ printf("Popped element %d",array[top]); top = top-1; } } void show(){ if (top == -1) printf("\nUnderflow\n"); else{ printf("\n Elements present in stack \n"); for(int i=top;i>=0;--i) printf("%d \n",array[i]); } } void main(){ int choice; while(1){ printf("Operations performed by stack\n"); printf("1.Push\n2.Pop\n3.Show\n4.End"); printf("Enter the choice"); scanf("%d",&choice); switch(choice){ case 1:push(); break; case 2:pop(); break; case 3:show(); break; case 4:exit(0); } } }

binarytreelinkedlist.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 #include<stdio.h> #include<stdlib.h> typedef struct bin { int data; struct bin *left; struct bin *right; } node; void insert(node *, node *); void inorder(node *); void preorder(node *); void postorder(node *); int height(node *root); void getLastNodeAndItsParent(node *root, int level, node *parent); void deleteNode(node *root); node *get_node(); node *lastNode, *parentOfLastNode; void main() { int choice; char ans = 'n'; node *new, *root; root = NULL; do{ printf("\n\t==== MENU ====\n"); printf("\t 1. Create\n"); printf("\t 2. Inorder\n"); printf("\t 3. Pre-order\n"); printf("\t 4. Post-Order\n"); printf("\t 5. Delete A Node\n"); printf("\t 6. Exit\n"); printf("\nYour Choice :- \t"); scanf("%d",&choice); switch(choice) { case 1: new = get_node(); printf("\t Enter the element :- \t"); scanf("%d",&new->data); if(root == NULL) root = new; else insert(root,new); break; case 2: if(root == NULL) printf("Tree is not created, It is empty.\n"); else inorder(root); break; case 3: if(root == NULL) printf("Tree is not created, It is empty.\n"); else preorder(root); break; case 4: if(root == NULL) printf("Tree is not created, It is empty.\n"); else postorder(root); break; case 5: if(root == NULL) printf("Tree is Empty, Cannot perform Deletion..\n"); else deleteNode(root); break; case 6: break; default: printf("Invalid Choice....\n"); } } while(choice != 6); } node *get_node(){ node *temp; temp = (node*) malloc(sizeof(node)); temp->left = NULL; temp->right = NULL; return temp; } int height(node *root){ int maximum, lheight, rheight; if (root == NULL) return 0; lheight = height(root->left) + 1; rheight = height(root->right) + 1; maximum = (lheight>rheight)?lheight:rheight; return maximum; } void getLastNodeAndItsParent(node *root, int level, node *parent) { if (root == NULL) return; if (level == 1){ lastNode = root; parentOfLastNode = parent; } getLastNodeAndItsParent(root->left, level - 1, root); getLastNodeAndItsParent(root->right, level - 1, root); } void insert(node *root, node *new){ int ch; printf("\t Where to insert left/right of %d ? (L - 1/R - 2) :- \t",root->data); scanf("%d",&ch); if(ch == 2){ if(root->right == NULL){ root->right = new; } else{ insert(root->right, new); } } else{ if(root->left == NULL){ root->left = new; } else{ insert(root->left, new); } } } void deleteNode(node *root){ int levelOfLastNode = height(root); getLastNodeAndItsParent(root, levelOfLastNode, NULL); if (lastNode && parentOfLastNode){ if (parentOfLastNode->right){ printf("Deleted Node is %d\n",(parentOfLastNode->right)->data); parentOfLastNode->right = NULL; } else{ printf("Deleted Node is %d\n",(parentOfLastNode->left)->data); parentOfLastNode->left = NULL; } } else printf("\tEmpty Tree..\n"); } void inorder(node *temp) { if(temp != NULL){ inorder(temp->left); printf("%d\t",temp->data); inorder(temp->right); } } void preorder(node *temp) { if(temp != NULL){ printf("%d\t",temp->data); inorder(temp->left); inorder(temp->right); } } void postorder(node *temp) { if(temp != NULL){ inorder(temp->left); inorder(temp->right); printf("%d\t",temp->data); } }

infixtopostfixandeval.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 #define SIZE 50 #include <ctype.h> #include <stdio.h> #include <math.h> char s[SIZE]; int top = -1; void push(char elem){ s[++top] = elem; } char pop(){ return (s[top--]); } int priority(char elem) { switch (elem) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*': case '/': return 3; } } void infix_to_postfix(char infix[], char postfix[]){ char ch, elem; int i = 0, k = 0; push('#'); while ((ch = infix[i++]) != '\n'){ if (ch == '(') push(ch); else if (isalnum(ch)) postfix[k++] = ch; else if (ch == ')'){ while (s[top] != '(') postfix[k++] = pop(); elem = pop(); } else{ while (priority(s[top]) >= priority(ch)) postfix[k++] = pop(); push(ch); } } while (s[top] != '#') postfix[k++] = pop(); postfix[k] = 0; } int eval_postfix(char *postfix) { char ch; int i = 0, op1, op2; while((ch = postfix[i++]) != 0) { if(isdigit(ch)) push(ch-'0'); else { op2 = pop(); op1 = pop(); switch(ch) { case '+' : push(op1+op2); break; case '-' : push(op1-op2); break; case '*' : push(op1*op2); break; case '/' : push(op1/op2); break; case '^': push(pow(op1,op2)); break; } } } return s[top]; } char infx[50], pofx[50]; void main() { printf("\nInput the infix expression: "); fgets(infx, 50, stdin); infix_to_postfix(infx, pofx); printf("\nGiven Infix Expression: %sPostfix Expression: %s", infx, pofx); top = -1; printf("\nResult of evaluation of postfix expression : %d\n", eval_postfix(pofx)); }

polynomialAddnMult.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 #include <stdio.h> #include <stdlib.h> struct poly{ int coefficient; int exponential; struct poly *ptr; }; struct poly *p =NULL,*q=NULL,*r=NULL; struct poly *pptr,*qptr; struct poly *add(struct poly *header,int coeff,int expo){ struct poly *newnode,*temp=header; newnode = (struct poly*)malloc(sizeof(struct poly)); newnode->coefficient = coeff; newnode->exponential = expo; newnode->ptr = NULL; if (header==NULL){ header = newnode; } else{ while(temp->ptr!=NULL){ temp = temp -> ptr; } temp -> ptr = newnode; return header; } } void display(struct poly *header){ struct poly *current = header; if(header==NULL) printf("list is empty"); else{ while(current!=NULL){ if(current->ptr!=NULL){ printf(" %d x^%d +",current->coefficient,current->exponential); } else{ printf(" %d x^%d",current->coefficient,current->exponential); } current = current->ptr; } } } void addition(struct poly *p,struct poly *q){ int result; pptr= p,qptr = q; if(pptr==NULL){ printf("pptr is null"); } while((pptr!=NULL)&&(qptr!=NULL)){ if (pptr->exponential > qptr->exponential){ printf("abc"); r = add(r,pptr->coefficient,pptr->exponential); pptr = pptr -> ptr; } else if(pptr->exponential < qptr->exponential){ printf("abc"); r = add(r,qptr->coefficient,qptr->exponential); qptr = qptr->ptr; } else{ result = qptr->coefficient,qptr->coefficient; r= add(r,pptr->coefficient+qptr->coefficient,pptr -> exponential); qptr = qptr->ptr; pptr = pptr->ptr; } } while(pptr!=NULL){ r=add(r,pptr->coefficient,pptr->exponential); pptr = pptr->ptr; } while(qptr!=NULL){ r=add(r,qptr->coefficient,qptr -> exponential); qptr = qptr->ptr; } } struct poly *sort_add(struct poly *header,int coeff,int expo){ struct poly *next = header,*prev = NULL; struct poly *newnode; newnode = (struct poly *) malloc(sizeof(struct poly*)); newnode -> coefficient = coeff; newnode -> exponential = expo; newnode -> ptr=NULL; if(header == NULL){ header = newnode; } else{ while(next!=NULL && next -> exponential > expo){ prev = next; next = next ->ptr; } if(prev == NULL){ if (next ->exponential ==expo){ next -> coefficient = coeff+ next->coefficient; free(newnode); } else{ newnode -> ptr = next; header = newnode; } } else{ if((next!=NULL) && (next ->exponential ==expo)){ next -> coefficient = next -> coefficient +coeff; free(newnode); } else{ prev -> ptr = newnode; newnode -> ptr = next; } } return header; } } void multiplication(struct poly *p,struct poly *q){ int coeff,expo; pptr=p; qptr = q; while(pptr!=NULL){ qptr = q; while(qptr!=NULL){ coeff = pptr->coefficient * qptr->coefficient; expo = pptr->exponential + qptr->exponential; r= sort_add(r,coeff,expo); qptr = qptr -> ptr; } pptr = pptr->ptr; } } void main(){ int a,n,coeff,expo; //printf("polynomial representation"); printf("Enter number of elements in 1st polynomial"); scanf("%d",&n); for(int i=0;i<n;i++){ printf("Enter coefficient "); scanf("%d",&coeff); printf("Enter exponential "); scanf("%d",&expo); p = add(p,coeff,expo); printf("\n"); } printf("Enter no of elements in 2nd polynomial"); scanf("%d",&n); for(int i=0;i<n;i++){ printf("Enter the coefficient"); scanf("%d",&coeff); printf("Enter exponential"); scanf("%d",&expo); q = add(q,coeff,expo); printf("\n"); } printf("Polynomial 1 is "); display(p); printf("\n"); printf("Polynomial 2 is "); display(q); printf("\n 1.Addition \n 2. Multiplication "); scanf("%d",&a); switch(a){ case 1: addition(p,q); printf("The result is"); display(r); break; case 2: multiplication(p,q); printf("The result is"); display(r); break; default: printf("Invalid Choice"); break; } }

singlylinkedlist.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 #include<stdlib.h> #include<stdio.h> void display(); void insert_begin(); void insert_end(); void insert_pos(); void delete_begin(); void delete_end(); void delete_pos(); struct node{ int info; struct node *next; }; struct node *start = NULL; int main(){ int choice; printf("\n Menu \n"); printf("\n1.Display"); printf("\n2.Insert at the beginning"); printf("\n3.Insert at the end"); printf("\n4.Insert at specified position"); printf("\n5.Delete from beginning"); printf("\n6.Delete from the end"); printf("\n7.Delete from specific position"); printf("\n8.Exit"); while(1){ printf("\n Enter your choice"); scanf("%d",&choice); switch(choice){ case 1: display(); break; case 2: insert_begin(); break; case 3: insert_end(); break; case 4: insert_pos(); break; case 5: delete_begin(); break; case 6: delete_end(); break; case 7: delete_pos(); break; case 8: exit(0); break; default: printf("\n Wrong choice"); } } return 0; } void display(){ struct node *ptr; if(start==NULL){ printf("\n List is empty"); return; } else{ ptr=start; printf("In the list elements are\n"); while(ptr!=NULL){ printf("%d\t",ptr->info); ptr=ptr->next; } } } void insert_begin(){ struct node *temp; temp = (struct node*)malloc(sizeof(struct node)); if(temp==NULL){ printf("\n Overflow"); return; } printf("\n Enter the data value of the node \t"); scanf("%d",&temp->info); temp->next = NULL; if(start==NULL) start = temp; else{ temp->next=start; start = temp; } } void insert_end(){ struct node *temp,*ptr; temp = (struct node*)malloc(sizeof(struct node)); if(temp==NULL){ printf("\n Overflow"); return; } printf("\n Enter the data value of the node \t"); scanf("%d",&temp->info); temp->next = NULL; if(start==NULL) start = temp; else{ ptr=start; while(ptr->next!=NULL){ ptr=ptr->next; } ptr->next = temp; } } void insert_pos(){ struct node *ptr,*temp; int i,pos; temp = (struct node*)malloc(sizeof(struct node)); if(temp==NULL){ printf("\n Overflow"); return; } printf("\n Enter the position for the new node to be inserted \t"); scanf("%d",&pos); printf("\n Enter the data value of the node \t"); scanf("%d",&temp->info); temp->next = NULL; if(pos==0){ temp->next=start; start = temp; } else{ for(i=0,ptr=start;i<pos-1;i++){ ptr = ptr->next; if(ptr==NULL){ printf("\n Position not found"); return; } } temp->next = ptr->next; ptr->next = temp; } } void delete_begin(){ struct node *ptr; if(start==NULL){ printf("\n Underflow"); return; } else{ printf("1"); ptr=start; printf("2"); start = start->next; printf("\n The deleted element is %d \t",ptr->info); free(ptr); } } void delete_end(){ struct node *temp, *ptr; if(start==NULL){ printf("\n Underflow"); return; } else if(start->next==NULL){ ptr=start; start=NULL; printf("\n The deleted element is %d",ptr->info); free(ptr); } else{ ptr=start; while(ptr->next!=NULL){ temp = ptr; ptr=ptr->next; } temp->next = NULL; printf("\n The deleted element is %d \t",ptr->info); free(ptr); } } void delete_pos(){ int i,pos; struct node *ptr, *temp; if(start==NULL){ printf("\n Underflow"); exit(0); } else{ printf("\n Enter the position to be deleted"); scanf("%d",&pos); if(pos == 0){ ptr = start; start = start->next; printf("\n The deleted element is %d \t",ptr->info); free(ptr); } else{ ptr = start; for(i=0;i<pos;i++){ temp = ptr; ptr = ptr->next; if(ptr == NULL){ printf("\n Position not found"); return; } } temp->next = ptr->next; printf("\n The deleted element is %d \t",ptr->info); free(ptr); } } }