You are on page 1of 9

1.

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;

struct Node* next;

};

struct Node* reverse(struct Node* head) {

struct Node* prev = NULL;

struct Node* current = head;

struct Node* next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

head = prev;

return head;

void printList(struct Node* head) {

while (head != NULL) {

printf("%d ", head->data);

head = head->next;

printf("\n");

}
int main() {

struct Node* head = NULL;

struct Node* second = NULL;

struct Node* third = NULL;

// allocate 3 nodes in the heap

head = (struct Node*)malloc(sizeof(struct Node));

second = (struct Node*)malloc(sizeof(struct Node));

third = (struct Node*)malloc(sizeof(struct Node));

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: [ ]

The expression is Balanced


3. Write a c program to implement Circular linked list operations.
SOLUTION:-
#include <stdio.h>
#include <stdlib.h>

// Define a structure for the linked list node


struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a new node at the end of the list


void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
(*head)->next = *head;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}
}

// Function to delete a node from the list


void deleteNode(struct Node** head, int data) {
if (*head == NULL) {
return;
}
struct Node* temp = *head;
struct Node* prev = NULL;
while (temp->next != *head && temp->data != data) {
prev = temp;
temp = temp->next;
}
if (temp->data == data) {
if (temp == *head) {
prev = *head;
while (prev->next != *head) {
prev = prev->next;
}
prev->next = (*head)->next;
*head = (*head)->next;
free(temp);
} else {
prev->next = temp->next;
free(temp);
}
}
}

// Function to print the list


void printList(struct Node* head) {
struct Node* temp = head;
if (head == NULL) {
return;
}
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
}

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;
}

4. Give the step-by-step procedure to convert the following infix


expression into postfix
expression using stack. (a*b%c)+(d/e-f/g*h).And evaluate it a=18,
b=-21, c=7, d=2765,
e=13, f=1932, g=27, h=6)

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:

1. Initialize an empty stack.


2. Start scanning the infix expression from left to right.
3. If the current character is an operand, append it to the postfix expression.
4. If the current character is an operator:
• Pop all the operators from the stack that have higher or equal precedence than
the current operator.
• Push the current operator onto the stack.
5. Repeat steps 3 and 4 until all characters in the infix expression have been processed.
6. If there are still operators in the stack, pop them and append them to the postfix
expression.
7. The resulting postfix expression is ab*c%de/f/gh*-+.
8. Evaluate the postfix expression by using a stack. Start scanning the postfix expression
from left to right.
• If the current character is an operand, push it onto the stack.
• If the current character is an operator:
• Pop the top two elements from the stack.
• Apply the operator to the two elements popped.
• Push the result onto the stack.
9. Repeat steps 8 until all characters in the postfix expression have been processed.
10. The final result on the stack is the answer to the expression.

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.

5. Write a c program to implement priority queue using


linked list?

SOLUTION:-

#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
int priority;
struct node *next;
};

struct node *start = NULL;

struct node *create_node(int data, int priority) {


struct node *new_node = (struct node *) malloc(sizeof(struct
node));
new_node->data = data;
new_node->priority = priority;
new_node->next = NULL;
return new_node;
}

void enqueue(int data, int priority) {


struct node *new_node = create_node(data, priority);
if (start == NULL || priority < start->priority) {
new_node->next = start;
start = new_node;
return;
}
struct node *temp = start;
while (temp->next != NULL && temp->next->priority <= priority)
{
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}

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;
}

You might also like