text 2
text 2
1.a) What are the postfix and prefix forms of the given below expression? A+B*(C-
D)/(P-R)
b) List three examples that uses linked list. c) What are the merits and demerits
of array implementation of lists?
2. a) Convert the infix (a+b)*(c+d)/f into postfix & prefix expression
b) Write the routine to insert an element into a queue.
c) Write the routine for insertion operation of singly linked list.
Postfix Form:
A B C D - * P R - / +
Prefix Form:
+ A / * B - C D - P R
Merits:
Demerits:
1. Fixed size: The size of the array must be declared in advance, leading
to either wasted space or insufficient capacity.
2. Insertion/Deletion overhead: Adding or removing elements requires
shifting elements, which is time-consuming.
3. No dynamic resizing: Unless using a dynamic array structure (e.g.,
std::vector in C++), arrays don’t automatically resize.
Postfix Form:
a b + c d + * f /
Prefix Form:
/ * + a b + c d f
void enqueue(int queue[], int *front, int *rear, int maxSize, int element) {
if ((*rear + 1) % maxSize == *front) {
printf("Queue is full. Cannot insert.\n");
return;
}
if (*front == -1) {
*front = 0;
}
*rear = (*rear + 1) % maxSize;
queue[*rear] = element;
}
#include <stdio.h>
#include <stdlib.h>
if (temp == NULL) {
printf("Position out of bounds.\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
1. a) A circular queue has a size of 5 and has 3 elements 10,20 and 40 where F=2
and R=4. After inserting 50 and 60, what is the value of F and R. Trying to
insert 30 at this stage what happens? Delete 2 elements from the queue and
insert 70, 80 & 90.
Explain and also show the sequence of steps with necessary diagrams with the
value of F & R
b) Mention any four applications of stack.
Initial State:
Final State:
• F = 4
• R = 4
1. b) Applications of Stack
2. a) What is a DeQueue?
Operations:
Example:
Concept:
Implementation:
2. c) Features of Stacks
1. a) What are the limitations of queue? Explain the algorithms for various
[7M]
operations of circular queue.
b) What are the applications of queue?
c) Write a routine for IsEmpty condition of queue.
2. a) What are the draw backs of single linked list? Write and explain the
algorithm
for search and modify operations in doubly linked list with example.
b) What are enqueue and dequeue operations?
c) Explain the usage of stack in recursive algorithm implementation?
1. a) Limitations of Queue
A circular queue overcomes the inefficiency of linear queues by treating the array
as circular.
Let queue[] be the array, front the front index, rear the rear index, and size the
capacity of the queue.
1. Initialization:
2. Enqueue (Insert):
• Check if the queue is full:
3. Dequeue (Delete):
• Check if the queue is empty:
if (front == -1)
Queue is empty
element = queue[front]
• Update front:
1. b) Applications of Queue
1. Search:
• Traverse the list starting from the head.
• Compare the target value with the current node’s data.
• If found, return the pointer to the node; otherwise, continue.
2. Modify:
• Traverse the list to find the node.
• Update the data field of the node with the new value.
Example:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
1. Enqueue:
The process of inserting an element into the queue.
• In a circular queue, rear is incremented, and the element is added at
the new position.
2. Dequeue:
The process of removing an element from the queue.
• In a circular queue, front is incremented, and the element at the
original front is removed.
Example Code:
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1); // Recursive call pushes onto stack
}