DS HA-2 Solutions
DS HA-2 Solutions
DS HA-2 Solutions
Given the pointer to the head node of a linked list, change the next pointers of the nodes
so that their order is reversed. The head pointer given may be null meaning that the initial list
is empty. (Reverse the list and print).
SOLUTION:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
next = current->next;
current->next = prev;
prev = current;
current = next;
head = prev;
return head;
head = head->next;
printf("\n");
}
int main() {
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printf("Original List:\n");
printList(head);
head = reverse(head);
printf("Reversed List:\n");
printList(head);
return 0;
2. Trace the given infix expression with stack and check whether
it is balanced or not
((a+b-(c*d)+e/f)*(p-q))
SOLUTION:-
1. Initialize an empty stack
2. Start scanning the expression from left to right
3. If the character is an operand, add it to the result
4. If the character is an operator, pop two elements from the stack, perform the
operation and push the result back to the stack
5. Repeat steps 3 and 4 until the expression is completely scanned
Here's how the stack would look like during the evaluation of the expression ((a+b-
(cd)+e/f)(p-q)):
1. Stack: [ ]
2. Push '(' onto the stack: Stack: [ ( ]
3. Push '(' onto the stack: Stack: [ (, ( ]
4. Read 'a', add it to the result: Result = a, Stack: [ (, ( ]
5. Read '+', pop '(', push '+' onto the stack: Result = a, Stack: [ (, + ]
6. Read 'b', add it to the result: Result = ab, Stack: [ (, + ]
7. Read '-', pop '+', push '-' onto the stack: Result = ab, Stack: [ (, - ]
8. Push '(': Result = ab, Stack: [ (, -, ( ]
9. Read 'c', add it to the result: Result = abc, Stack: [ (, -, ( ]
10. Read '', pop '(', push '' onto the stack: Result = abc, Stack: [ (, -, * ]
11. Read 'd', add it to the result: Result = abcd, Stack: [ (, -, * ]
12. Pop '*', pop '-', perform 'abc * d' and add the result to the result: Result = ab-
cd, Stack: [ ( ]
13. Read '+', pop '(', push '+' onto the stack: Result = ab-cd, Stack: [ + ]
14. Read 'e', add it to the result: Result = ab-cde, Stack: [ + ]
15. Read '/', pop '+', push '/' onto the stack: Result = ab-cde, Stack: [ / ]
16. Read 'f', add it to the result: Result = ab-cdef, Stack: [ / ]
17. Pop '/', perform 'ab-cde / f' and add the result to the result: Result = ab-
cdef/f, Stack: [ ]
18. Push '(': Result = ab-cdef/f, Stack: [ ( ]
19. Read 'p', add it to the result: Result = ab-cdef/fp, Stack: [ ( ]
20. Read '-', pop '(', push '-' onto the stack: Result = ab-cdef/fp, Stack: [ - ]
21. Read 'q', add it to the result: Result = ab-cdef/fpq, Stack: [ - ]
22. Pop '-', perform 'ab-cdef/fp - q' and add the result to the result: Result = ab-
cdef/fp-q, Stack: [ ]
23. Pop '(', perform 'ab-cdef/fp-q * (ab-cdef/fp-q)' and add the result to the
result: Result = (ab-cdef/fp-q)*(ab-cdef/fp-q), Stack: [ ]
int main() {
struct Node* head = NULL;
insertEnd(&head, 1);
insertEnd(&head, 2);
insertEnd(&head, 3);
insertEnd(&head, 4);
insertEnd(&head, 5);
printf("Original list: ");
printList(head);
deleteNode(&head, 3);
printf("\nList after deleting node: ");
printList(head);
return 0;
}
SOLUTION:-
Here is the step-by-step procedure to convert the infix expression (a*b%c)+(d/e-f/g*h) into
postfix expression and evaluate it:
Given the values of a=18, b=-21, c=7, d=2765, e=13, f=1932, g=27, h=6, the final result would be
46.
SOLUTION:-
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
int priority;
struct node *next;
};
void dequeue() {
if (start == NULL) {
printf("Queue is empty!\n");
return;
}
struct node *temp = start;
printf("Dequeued item is: %d\n", temp->data);
start = temp->next;
free(temp);
}
void display_queue() {
if (start == NULL) {
printf("Queue is empty!\n");
return;
}
struct node *temp = start;
printf("Priority Queue: ");
while (temp != NULL) {
printf("%d(%d) ", temp->data, temp->priority);
temp = temp->next;
}
printf("\n");
}
int main() {
enqueue(10, 1);
enqueue(20, 2);
enqueue(30, 3);
display_queue();
enqueue(40, 0);
display_queue();
dequeue();
display_queue();
return 0;
}