PRACTICAL NO. 1 AIM : Introduction to Dynamic memory Allocation. (a) W.A.P. to create dynamic int array using malloc() and free(). (b) W.A.P. to create dynamic char array using calloc() and free(). (a) INPUT : #include #include int main() { int n, count, *ptr, sum = 0; printf("Enter no. of elements : "); scanf("%d", &count); ptr = (int *)malloc(count * sizeof(int)); if (ptr == 0) { printf("Memory is not allocated"); exit(0); } for (n = 0; n < count; n++) { printf("Enter element %d : ", (n + 1)); scanf("%d", ptr + n); sum += *(ptr + n); } printf("Sum of all %d elements = %d \n", count,sum); free(ptr); return 0; } OUTPUT : (b) INPUT : #include #include int main() { int n, count, *ptr, sum = 0; printf("Enter no. of elements : "); scanf("%d", &count); ptr = (int *)calloc(count, sizeof(int)); if (ptr == 0) { printf("Memory is not allocated"); exit(0); } for (n = 0; n < count; n++) { printf("Enter element %d : ", (n + 1)); scanf("%d", ptr + n); sum += *(ptr + n); } printf("Sum of all %d elements = %d \n", count,sum); free(ptr); return 0; } OUTPUT :   PRACTICAL NO. 2 AIM : W.A.P. to implement structure in C. INPUT : #include struct student { char name[100]; int enro; int sem; char address[100]; int phno; char mail[100]; } s1; int main() { printf("\nEnter your details\n"); printf("NAME : "); scanf("%s", &s1.name); printf("En.No : "); scanf("%d", &s1.enro); printf("SEMESTER : "); scanf("%d", &s1.sem); printf("ADDRESS : "); scanf("%s", &s1.address); printf("Ph.No : "); scanf("%d", &s1.phno); printf("EMAIL : "); scanf("%d", &s1.mail); getchar(); return 0; } OUPUT :   PRACTICAL NO. 3 AIM : Write a program to implement (a) Linear Search (b) Binary Search. (a) INPUT : #include #include void main() { int arr[5], p, q, i; p = 0; for (i = 0; i < 5; i++) { printf("Enter element %d = ", i); scanf("%d", &arr[i]); } printf("Enter element you want to search --> "); scanf("%d", &q); for (i = 0; i < 5; i++) { if (arr[i] == q) { printf("Entered element is at position %d.\n\n", i); p++; } } if (p == 0) { printf("Entered element is not in the array.\n"); } } OUTPUT : (b) INPUT : #include void main() { int arr[10], flag, q, low, high, mid, i; flag = 0; low = 0; high = 9; for (i = 0; i < 10; i++) { printf("Enter element %d = ", i); scanf("%d", &arr[i]); } printf("Enter element you want to search --> "); scanf("%d", &q); while (low <= high) { mid = low + high / 2; if (arr[mid] == q){ flag++; break; } else if (arr[mid] < q){ low = mid++; } else{ high = mid--; } } if (flag == 1){ printf("Match found.\n"); } else printf("No match found.\n"); } OUTPUT : PRACTICAL NO. 5 AIM : Implement a program for stack that performs following operations using array. (a)PUSH (b) POP (c) PEEP (d) CHANGE (e) DISPLAY. INPUT : #include #define A 5 int top = -1, i, d; int stack[A]; void push(int b); int pop(); int peep(); int display(); void push(int b){ if (top >= A - 1){ printf("Stack is FULL!!"); } else{ top = top + 1; stack[top] = b; } } int pop(){ if (top == -1){ printf("Stack is Underflow, Insert element\n"); } else{ d = stack[top]; top = top - 1; printf("Last inserted element was %d\n", d); } } int display(){ for (i = top; i > 0; i--) printf("\t%d", stack[i]); } int peep(){ printf("\ntop = %d ", top); printf("\nvalue = %d ", stack[top]); } int main() { int a, b, c = 1; while (c == 1){ printf("\nOPERATIONS\n"); printf("Enter 1 --> Push Operation\n"); printf("Enter 2 --> Pop Operation\n"); printf("Enter 3 --> Display Operation\n"); printf("Enter 4 --> Peep Operation\n"); printf("Enter 5 --> To exit\n"); printf("Select Operation --> "); scanf("%d", &a); switch (a){ case 1: printf("Enter element --> "); scanf("%d", &b); push(b); printf("%d is pushed\n", b); break; case 2: b = pop(); printf("Element is popped\n"); break; case 3: display(); break; case 4: peep(); break; case 5: goto exit; } } exit: return 0; } OUTPUT : PRACTICAL - 6 AIM : Implement a program to convert infix notation to postfix notation using stack. INPUT : #include #include char stack[100]; int top = -1; void push(char x){ stack[++top] = x; } char pop(){ if (top == -1) return -1; else return stack[top--]; } int priority(char x){ if (x == '(') return 0; if (x == '+' || x == '-') return 1; if (x == '*' || x == '/') return 2; return 0; return 3; } int main() { char a[100]; char *e, x; printf("Enter infix expression : "); scanf("%s", a); e = a; while (*e != '\0'){ if (isalnum(*e)) printf("%c", *e); else if (*e == '(') push(*e); else if (*e == ')') { while ((x = pop()) != '(') printf("%c", x); } else { while (priority(stack[top]) >= priority(*e)) printf("%c", pop()); push(*e); } e++; } while (top != -1){ printf("%c", pop()); } printf("\n"); return 0; } OUTPUT : PRACTICAL - 7 AIM : Write a program to evaluate postfix notation. INPUT : // c program to evaluate value of a postfix expression #include #include #include #include // stack type struct stack{ int top; unsigned capacity; int *array; }; // stack operations struct stack *createstack(unsigned capacity){ struct stack *stack = (struct stack *)malloc(sizeof(struct stack)); if (!stack) return NULL; stack->top = -1; stack->capacity = capacity; stack->array = (int *)malloc(stack->capacity * sizeof(int)); if (!stack->array) return NULL; return stack; } int isempty(struct stack *stack){ return stack->top == -1; } char peek(struct stack *stack){ return stack->array[stack->top]; } char pop(struct stack *stack){ if (!isempty(stack)) return stack->array[stack->top--]; return '$'; } void push(struct stack *stack, char op){ stack->array[++stack->top] = op; } // the main function that returns value of a given postfix expression int evaluatepostfix(char *exp){ struct stack *stack = createstack(strlen(exp)); int i; // see if stack was created successfully if (!stack) return -1; // scan all characters one by one for (i = 0; exp[i]; ++i) { // if the scanned character is an operand (number here), // push it to the stack. if (isdigit(exp[i])) push(stack, exp[i] - '0'); // if the scanned character is an operator, pop two // elements from stack apply the operator else { int val1 = pop(stack); int val2 = pop(stack); switch (exp[i]) { case '+': push(stack, val2 + val1); break; case '-': push(stack, val2 - val1); break; case '*': push(stack, val2 * val1); break; case '/': push(stack, val2 / val1); break; } } } return pop(stack); } int main() { char exp[] = "478*+9-"; printf("Postfic evaluation %d\n", evaluatepostfix(exp)); return 0; } OUTPUT : PRACTICAL - 8 AIM : Write a program to implement QUEUE using arrays that performs following operations (a)INSERT (b)DELETE (c)DISPLAY. INPUT : #include #define SIZE 5 void enQueue(int); void deQueue(); void display(); int items[SIZE], front = -1, rear = -1; void display(){ if (rear == -1) printf("\nQueue is Empty!!!"); else{ int i; printf("\nElements in queue are :\n"); for (i = front; i <= rear; i++) printf("%d ", items[i]); } printf("\n"); } void enQueue(int value){ if (rear == SIZE - 1) printf("\nQueue FULL"); else{ if (front == -1) front = 0; rear++; items[rear] = value; printf("\nInserted element--> %d", value); } } void deQueue(){ if (front == -1) printf("\nQueue EMPTY"); else{ printf("\nDeleted element --> %d", items[front]); front++; if (front > rear) front = rear = -1; } } int main() { deQueue(); enQueue(11); enQueue(22); enQueue(33); enQueue(44); enQueue(55); display(); deQueue(); display(); return 0; } OUTPUT : PRACTICAL - 9 AIM : Write a menu driven program to implement following operations on the singly linked list. (a) Insert node at front of linked list.(b) Insert a node at end of linked list. (c) Insert node at specified location. (d) Delete first node of linked list. (e) Delete node after specified position. INPUT : #include struct node{ int info; struct node *link; }; struct node *start = NULL; void display(){ struct node *temp; if (start == NULL) printf("\nList is empty\n"); else { temp = start; while (temp != NULL) { printf("Data = %d\n", temp->info); temp = temp->link; } } } void insertAtFront(){ int data; struct node *temp; temp = malloc(sizeof(struct node)); printf("\nEnter number to" " be inserted : "); scanf("%d", &data); temp->info = data; temp->link = start; start = temp; } void insertAtEnd(){ int data; struct node *temp, *head; temp = malloc(sizeof(struct node)); printf("\nEnter number to" " be inserted : "); scanf("%d", &data); temp->link = 0; temp->info = data; head = start; while (head->link != NULL) { head = head->link; } head->link = temp; } void insertAtPosition(){ struct node *temp, *newnode; int pos, data, i = 1; newnode = malloc(sizeof(struct node)); printf("\nEnter position and data :"); scanf("%d %d", &pos, &data); temp = start; newnode->info = data; newnode->link = 0; while (i < pos - 1) { temp = temp->link; i++; } newnode->link = temp->link; temp->link = newnode; } void deleteFirst(){ struct node *temp; if (start == NULL) printf("\nList is empty\n"); else{ temp = start; start = start->link; free(temp); } } void deletePosition(){ struct node *temp, *position; int i = 1, pos; if (start == NULL) printf("\nList is empty\n"); else { printf("\nEnter index : "); scanf("%d", &pos); position = malloc(sizeof(struct node)); temp = start; while (i < pos - 1) { temp = temp->link; i++; } position = temp->link; temp->link = position->link; free(position); } } int main() { int choice; while (1) { printf("\nEnter 1 --> To see list\n"); printf("Enter 2 --> To insert at front\n"); printf("Enter 3 --> To insert at end\n"); printf("Enter 4 --> To insert at specified position\n"); printf("Enter 5 --> To delete at first element\n"); printf("Enter 6 --> To delete at specified position\n"); printf("\nEnter your choice --> "); scanf("%d", &choice); switch (choice) { case 1: display(); break; case 2: insertAtFront(); break; case 3: insertAtEnd(); break; case 4: insertAtPosition(); break; case 5: deleteFirst(); break; case 6: deletePosition(); default: printf("Invalid input\n"); } } return 0; } OUTPUT : PRACTICAL - 10 AIM : W.A.P. to implement foll. operations. on doubly linked list.(a)Insert node at front.(b)Insert node at end.(c)Delete last node.(d)Delete a node before specified position. INPUT : #include #include struct Node{ int data; struct Node *next; struct Node *prev; }; void linked_listtraversal(struct Node *ptr){ while (ptr != NULL){ printf("element = %d\n", ptr->data); ptr = ptr->next; } } struct Node *addatbeg(struct Node *head, int value){ struct Node *ptr = (struct Node *)malloc(sizeof(struct Node)); ptr->data = value; ptr->next = head; head->prev = ptr; ptr->prev = NULL; head = ptr; return head; } struct Node *addatend(struct Node *head, int value){ struct Node *q = (struct Node *)malloc(sizeof(struct Node)); q->data = value; struct Node *p = head; while (p->next != NULL){ p = p->next; } p->next = q; q->prev = p; q->next = NULL; return head; } int main() { struct Node *head; struct Node *second; struct Node *third; head = (struct Node *)malloc(sizeof(struct Node)); second = (struct Node *)malloc(sizeof(struct Node)); third = (struct Node *)malloc(sizeof(struct Node)); head->prev = NULL; head->data = 7; head->next = second; second->prev = head; second->data = 11; second->next = third; third->prev = second; third->data = 66; third->next = NULL; head = addatbeg(head, 10); head = addatend(head, 6); linked_listtraversal(head); return 0; } OUTPUT :