Stacks and Queues
Contents
Describe Stack and its operations 1
Recall Overflow and Underflow Conditions in Stack 5
List the Applications of Stack 8
Program to Implement Stack using Array 9
Write a Program to Implement Stack using Linked List 12
Recall Infix, Prefix, and Postfix Expressions 12
Evaluate Postfix Expression using Stacks 12
Describe Simulating Recursion and Tail Recursion 12
Differentiate between Recursion and Iteration 12
Recall Tower of Hanoi (TOH) Problem 12
Write a program to implement TOH Problem 12
Describe EightQueen Puzzle Problem 12
Recall the concept of Backtracking 12
Recall the concept of Multiple Stacks 12
Write a program to implement Multiple Stacks 12
Describe Queue and its operations 12
Write Algorithms/Functions to perform Insertion and Deletion operations 12
Write a Program in C to Implement Queue using Array 12
Write a Program in C to implement Queue using Linked List 12
Describe Circular Queue and its operations 12
Recall Overflow and Underflow conditions in Circular Queue 12
Apply the concept of Circular Queue operations and find the value of FRONT and REAR 12
Write Algorithm/Functions to perform insertion and Deletion operations 12
Describe Dequeue and its Operations 12
Recall Overflow and Underflow conditions in Deque 12
Write Algorithms/Functions to perform Insertion and Deletion Operations on Dequeue 12
List various Applications of the Queue 12
Describe Stack and its operations
A stack is a linear data structure that follows the LastInFirstOut (LIFO) principle. It can be visualized as a stack of items, where the last item added is the first one to be removed.
The operations commonly performed on a stack are:
 Push: This operation adds an element to the top of the stack. The new element becomes the top item, and the size of the stack increases by one.
 Pop: This operation removes the top element from the stack. The element that was added last is the first one to be removed. After the removal, the size of the stack decreases by one.
 Peek/Top: This operation returns the value of the top element without removing it from the stack. It allows you to access the top element without modifying the stack.
 isEmpty: This operation checks if the stack is empty. It returns true if the stack has no elements and false otherwise.
Stacks can be implemented using various data structures, such as arrays or linked lists. The choice of implementation depends on the requirements and constraints of the problem at hand. The stack operations provide a simple and efficient way to manage data in a LastInFirstOut fashion, making them useful in many applications, including expression evaluation, function calls, undoredo functionality, and more.
Here’s an example that demonstrates the operations of a stack:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
// Structure to represent the stack
struct Stack {
int items[MAX_SIZE];
int top;
};
// Function to initialize an empty stack
void initialize(struct Stack* stack) {
stack>top = 1;
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return (stack>top == 1);
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return (stack>top == MAX_SIZE – 1);
}
// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf(“Stack Overflow: Cannot push element %d\n”, value);
} else {
stack>items[++stack>top] = value;
printf(“Pushed element %d\n”, value);
}
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf(“Stack Underflow: Cannot pop element\n”);
return 1;
} else {
int value = stack>items[stack>top–];
printf(“Popped element %d\n”, value);
return value;
}
}
// Function to peek the top element of the stack
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf(“Stack is empty\n”);
return 1;
} else {
return stack>items[stack>top];
}
}
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf(“Top element: %d\n”, peek(&stack));
pop(&stack);
pop(&stack);
printf(“Top element: %d\n”, peek(&stack));
pop(&stack);
printf(“Is stack empty? %s\n”, isEmpty(&stack) ? “Yes” : “No”);
return 0;
}
In this example, we define a stack using a structure Stack that has an array to store the elements and a variable top to keep track of the top element’s index. We also define functions to perform the stack operations.
In the main function, we initialize an empty stack using the initialize function. Then, we push three elements (10, 20, and 30) onto the stack using the push function. We also print the top element of the stack using the peek function.
Next, we pop two elements from the stack using the pop function. After each pop operation, we print the popped element.
Finally, we check if the stack is empty using the isEmpty function and print the result.
The output of this program will be:
Pushed element 10
Pushed element 20
Pushed element 30
Top element: 30
Popped element 30
Popped element 20
Top element: 10
Popped element 10
Stack is empty: Yes
This example demonstrates the basic operations of a stack: push, pop, and peek. You can modify it as needed and experiment with different values and operations to further understand how a stack works.
Recall Overflow and Underflow Conditions in Stack
Here are the definitions of overflow and underflow conditions in a stack:
 Stack Overflow: This condition occurs when we try to push an element onto a stack that is already full. In other words, the stack has reached its maximum capacity, and there is no more space to accommodate additional elements. Trying to push an element onto a full stack will result in a stack overflow error.
 Stack Underflow: This condition occurs when we try to pop an element from an empty stack. In other words, the stack has no elements, and we are trying to remove an element from it. Trying to pop an element from an empty stack will result in a stack underflow error.
Both overflow and underflow conditions indicate that the stack is in an invalid state and that the requested operation cannot be performed. It’s important to handle these conditions appropriately in order to avoid program crashes or unexpected behavior.
Here’s an example that demonstrates the overflow and underflow conditions in a stack:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
// Structure to represent the stack
struct Stack {
int items[MAX_SIZE];
int top;
};
// Function to initialize an empty stack
void initialize(struct Stack* stack) {
stack>top = 1;
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return (stack>top == 1);
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return (stack>top == MAX_SIZE – 1);
}
// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf(“Stack Overflow: Cannot push element %d\n”, value);
} else {
stack>items[++stack>top] = value;
printf(“Pushed element %d\n”, value);
}
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf(“Stack Underflow: Cannot pop element\n”);
return 1;
} else {
int value = stack>items[stack>top–];
printf(“Popped element %d\n”, value);
return value;
}
}
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
push(&stack, 40);
push(&stack, 50);
push(&stack, 60); // Stack Overflow, as the stack is already full
pop(&stack);
pop(&stack);
pop(&stack);
pop(&stack);
pop(&stack);
pop(&stack); // Stack Underflow, as the stack is already empty
return 0;
}
In this example, we have a stack with a maximum size of 5 (MAX_SIZE). We push elements onto the stack using the push function and pop elements from the stack using the pop function.
Initially, we push 5 elements (10, 20, 30, 40, and 50) onto the stack successfully. However, when we try to push a sixth element (60), which would cause the stack to exceed its maximum size, we encounter a stack overflow condition. The program prints an error message indicating that the stack is already full and cannot accommodate any more elements.
Next, we pop all 5 elements from the stack successfully. However, when we try to pop an additional element from the already empty stack, we encounter a stack underflow condition. The program prints an error message indicating that the stack is already empty and there are no elements to remove.
This example demonstrates the overflow and underflow conditions in a stack, and how they can be detected and handled in a program.
List the Applications of Stack
Stacks have various applications in computer science and everyday life. Here are some common applications of stacks:
 Expression Evaluation: Stacks are widely used in expression evaluation algorithms. They can be used to evaluate arithmetic expressions, infix to postfix conversion, and postfix expression evaluation. The stack helps in maintaining the order of operations and operands.
 Function Call Stack: In programming languages, a stack is used to keep track of function calls and their respective local variables. Each function call creates a new stack frame, and the stack allows for proper function execution and return.
 Undo/Redo Functionality: Many applications, such as text editors and graphic design software, implement undo/redo functionality using a stack. Each change made is pushed onto the stack, allowing the user to undo or redo actions by popping elements from the stack.
 Backtracking Algorithms: Backtracking algorithms, such as depthfirst search and recursive algorithms, often use stacks to keep track of the current state and backtrack if necessary. The stack allows for efficient backtracking by storing the necessary information.
 Web Browser History: Web browsers use a stack to keep track of visited web pages. Each time a user visits a new page, it is pushed onto the stack. The user can then navigate backward by popping pages from the stack.
 Parenthesis Matching: Stacks are used to check if parentheses, braces, or brackets in an expression are properly balanced. Each opening symbol is pushed onto the stack, and when a closing symbol is encountered, it is matched with the top of the stack.
 Compiler and Interpreter Design: Stacks are used extensively in compiler and interpreter design for various purposes, including parsing, managing variable scopes, and evaluating expressions.
These are just a few examples of how stacks are used in different applications. Stacks provide a simple and efficient way to manage data in a LastInFirstOut fashion, making them versatile and widely used in computer science and programming.
Program to Implement Stack using Array
Here’s an example of a stack implementation using an array in C:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct Stack {
int items[MAX_SIZE];
int top;
};
void initialize(struct Stack* stack) {
stack>top = 1;
}
int isEmpty(struct Stack* stack) {
return (stack>top == 1);
}
int isFull(struct Stack* stack) {
return (stack>top == MAX_SIZE – 1);
}
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf(“Stack Overflow: Cannot push element %d\n”, value);
} else {
stack>items[++stack>top] = value;
printf(“Pushed element %d\n”, value);
}
}
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf(“Stack Underflow: Cannot pop element\n”);
return 1;
} else {
int value = stack>items[stack>top–];
printf(“Popped element %d\n”, value);
return value;
}
}
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
pop(&stack);
pop(&stack);
pop(&stack);
return 0;
}
In this example, we define a struct Stack to represent the stack. It contains an array items to store the elements and an integer top to keep track of the top of the stack.
The initialize function initializes an empty stack by setting top to 1. The isEmpty and isFull functions check if the stack is empty or full, respectively.
The push function adds an element to the stack. It first checks if the stack is full using isFull and displays an error message if it is. Otherwise, it increments top and adds the element to the items array.
The pop function removes an element from the stack. It checks if the stack is empty using isEmpty and displays an error message if it is. Otherwise, it retrieves the top element, decrements top, and returns the value.
In the main function, we create a stack, initialize it, and perform push and pop operations. The program outputs the pushed and popped elements to demonstrate the stack operations.
Note: In this example, we have used a fixedsize array to implement the stack. You can modify the MAX_SIZE constant to adjust the maximum capacity of the stack.
Write a Program to Implement Stack using Linked List
Here’s an example of a stack implementation using a linked list in C:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};
void initialize(struct Stack* stack) {
stack>top = NULL;
}
int isEmpty(struct Stack* stack) {
return (stack>top == NULL);
}
void push(struct Stack* stack, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf(“Memory allocation failed\n”);
return;
}
newNode>data = value;
newNode>next = stack>top;
stack>top = newNode;
printf(“Pushed element %d\n”, value);
}
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf(“Stack Underflow: Cannot pop element\n”);
return 1;
}
struct Node* temp = stack>top;
int value = temp>data;
stack>top = temp>next;
free(temp);
printf(“Popped element %d\n”, value);
return value;
}
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
pop(&stack);
pop(&stack);
pop(&stack);
return 0;
}
In this example, we define a struct Node to represent each node in the linked list. It contains an integer data to store the value and a pointer next to point to the next node.
The struct Stack represents the stack and contains a pointer top to the top node of the linked list.
The initialize function initializes an empty stack by setting top to NULL.
The isEmpty function checks if the stack is empty by checking if top is NULL.
The push function adds an element to the stack. It first creates a new node and assigns the value to it. Then it sets the next pointer of the new node to the current top node and updates the top pointer to the new node.
The pop function removes an element from the stack. It checks if the stack is empty and displays an error message if it is. Otherwise, it retrieves the top element, updates the top pointer to the next node, frees the memory of the popped node, and returns the value.
In the main function, we create a stack, initialize it, and perform push and pop operations. The program outputs the pushed and popped elements to demonstrate the stack operations.
Note: In this example, memory allocation is used to create new nodes dynamically. Make sure to free the memory using the free function to avoid memory leaks.
Recall Infix, Prefix, and Postfix Expressions
Infix, prefix, and postfix are different notations used to represent arithmetic expressions. They determine the order of operations and the placement of operands and operators within the expression. Here’s an explanation of each notation:
 Infix Expression:
Infix notation is the commonly used notation for writing arithmetic expressions. In this notation, the operators are placed between the operands. For example, the infix expression “2 + 3 * 4” represents the addition of 2 and the product of 3 and 4. Infix notation follows the usual mathematical convention of operator precedence and parentheses for grouping.
 Prefix Expression (also known as Polish Notation):
Prefix notation places the operator before the operands. In a prefix expression, every operator is preceded by its operands. For example, the prefix expression “+ 2 * 3 4” is equivalent to the infix expression “2 + 3 * 4”. Prefix notation eliminates the need for parentheses to denote the order of operations as it explicitly specifies the operatoroperand relationships.
 Postfix Expression (also known as Reverse Polish Notation or RPN):
Postfix notation places the operator after the operands. In a postfix expression, every operator is followed by its operands. For example, the postfix expression “2 3 4 * +” is equivalent to the infix expression “2 + 3 * 4”. Postfix notation also eliminates the need for parentheses, and the order of operations is determined solely by the position of the operators in the expression.
The advantage of prefix and postfix notations is that they eliminate the need for parentheses and provide a clear and unambiguous way to represent arithmetic expressions. These notations are also suitable for evaluation using stackbased algorithms such as postfix evaluation or prefix evaluation.
Converting between infix, prefix, and postfix notations can be done using various algorithms such as the Shunting Yard algorithm or by using stackbased operations.
Note: It’s important to understand the operator precedence and associativity rules when working with infix, prefix, and postfix expressions to ensure correct evaluation and interpretation of the expressions.
Evaluate Postfix Expression using Stacks
To evaluate a postfix expression using a stack, you can follow the steps outlined in the algorithm below:
Algorithm:
 Create an empty stack.
 Read the postfix expression from left to right.
 For each element in the expression:
a. If the element is an operand, push it onto the stack.
b. If the element is an operator, pop the top two operands from the stack.
c. Perform the operation using the operator on the operands.
d. Push the result back onto the stack.
 After processing all the elements in the expression, the result will be the top element of the stack.
 Pop the result from the stack and return it.
Let’s take an example to illustrate the evaluation of a postfix expression:
Example: Evaluate the postfix expression “5 3 4 * + 2 ”
 Create an empty stack.
 Read the postfix expression from left to right.
 The first element is 5, which is an operand. Push it onto the stack. Stack: [5]
 The next element is 3, which is an operand. Push it onto the stack. Stack: [5, 3]
 The next element is 4, which is an operand. Push it onto the stack. Stack: [5, 3, 4]
The next element is *, which is an operator. Pop the top two operands from the stack: 3 and 4.

 Perform the operation 3 * 4 = 12. Push the result back onto the stack. Stack: [5, 12]
The next element is +, which is an operator. Pop the top two operands from the stack: 5 and 12.

 Perform the operation 5 + 12 = 17. Push the result back onto the stack. Stack: [17]
 The next element is 2, which is an operand. Push it onto the stack. Stack: [17, 2]
The next element is , which is an operator. Pop the top two operands from the stack: 17 and 2.

 Perform the operation 17 – 2 = 15. Push the result back onto the stack. Stack: [15]
 After processing all the elements, the result is the top element of the stack, which is 15.
 Pop the result from the stack, which gives the final result: 15.
So, the postfix expression “5 3 4 * + 2 ” evaluates to 15.
Note: In this example, we assumed that the postfix expression is wellformed and does not contain any syntax errors.
Here’s a C program that evaluates a postfix expression using a stack:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_STACK_SIZE 100
struct Stack {
int top;
int items[MAX_STACK_SIZE];
};
void initialize(struct Stack* stack) {
stack>top = 1;
}
int isEmpty(struct Stack* stack) {
return stack>top == 1;
}
int isFull(struct Stack* stack) {
return stack>top == MAX_STACK_SIZE – 1;
}
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf(“Stack Overflow: Cannot push element\n”);
return;
}
stack>items[++stack>top] = value;
}
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf(“Stack Underflow: Cannot pop element\n”);
return 1;
}
return stack>items[stack>top–];
}
int evaluatePostfix(char* postfix) {
struct Stack stack;
initialize(&stack);
int i, operand1, operand2, result;
char ch;
for (i = 0; postfix[i] != ‘\0’; i++) {
ch = postfix[i];
if (isdigit(ch)) {
push(&stack, ch – ‘0’);
} else {
operand2 = pop(&stack);
operand1 = pop(&stack);
switch (ch) {
case ‘+’:
result = operand1 + operand2;
break;
case ‘‘:
result = operand1 – operand2;
break;
case ‘*’:
result = operand1 * operand2;
break;
case ‘/’:
result = operand1 / operand2;
break;
default:
printf(“Invalid operator\n”);
return 0;
}
push(&stack, result);
}
}
return pop(&stack);
}
int main() {
char postfix[100];
printf(“Enter a postfix expression: “);
scanf(“%s”, postfix);
int result = evaluatePostfix(postfix);
printf(“Result: %d\n”, result);
return 0;
}
In this program, we define a struct Stack to represent the stack, which contains an array items to store the elements and an integer top to keep track of the top of the stack.
The initialize, isEmpty, isFull, push, and pop functions are used to perform stack operations.
The evaluatePostfix function takes a postfix expression as input and evaluates it. It uses a loop to iterate through each character in the expression. If the character is a digit, it is converted to an integer and pushed onto the stack. If the character is an operator, the top two operands are popped from the stack, and the corresponding operation is performed. The result is then pushed back onto the stack. Finally, the result is popped from the stack and returned as the final evaluation.
In the main function, the user is prompted to enter a postfix expression. The evaluatePostfix function is called with the entered expression, and the result is displayed.
Note: This program assumes that the postfix expression is wellformed and does not contain any syntax errors.
Convert the following: i. Infix Expression to Postfix form ii. Infix Expression to Prefix form iii. Prefix Expression to Postfix form
i. Conversion from Infix Expression to Postfix Form (Postfix Notation):
Algorithm:
 Create an empty stack and an empty string to store the postfix expression.
 Scan the infix expression from left to right.
 If the scanned character is an operand, append it to the postfix string.
 If the scanned character is an operator:
a. While the stack is not empty and the precedence of the operator on the top of the stack is greater than or equal to the precedence of the current operator, pop operators from the stack and append them to the postfix string.
b. Push the current operator onto the stack.
 If the scanned character is an opening parenthesis ‘(‘, push it onto the stack.
 If the scanned character is a closing parenthesis ‘)’:
a. Pop operators from the stack and append them to the postfix string until an opening parenthesis is encountered. Exclude the opening parenthesis from the postfix string.
b. Discard the opening parenthesis from the stack.
 Repeat steps 36 until all characters in the infix expression have been scanned.
 Pop any remaining operators from the stack and append them to the postfix string.
 The resulting postfix string is the postfix form of the infix expression.
Example:
Infix expression: (A + B) * C – D / E
Postfix expression: AB+C*DE/
Conversion Steps:
 Stack: [empty], Postfix: [empty]
 Scan ‘(‘: Stack: [(, Postfix: [empty]
 Scan ‘A’: Append ‘A’ to Postfix: Postfix: A
 Scan ‘+’: Stack: [(, +], Postfix: A
 Scan ‘B’: Append ‘B’ to Postfix: Postfix: AB
 Scan ‘)’: Pop ‘+’ from stack and append to Postfix: Stack: [(, Postfix: AB+
 Pop ‘(‘: Stack: [empty], Postfix: AB+
 Scan ”: Stack: [], Postfix: AB+
 Scan ‘C’: Append ‘C’ to Postfix: Postfix: AB+C
 Scan ‘‘: Stack: [], Postfix: AB+C*
 Scan ‘D’: Append ‘D’ to Postfix: Postfix: AB+C*D
 Scan ‘/’: Stack: [, /], Postfix: AB+C*D
 Scan ‘E’: Append ‘E’ to Postfix: Postfix: AB+C*DE
 Pop ‘/’ from stack and append to Postfix: Stack: [], Postfix: AB+C*DE/
 Pop ‘‘ from stack and append to Postfix: Stack: [empty], Postfix: AB+C*DE/
 The resulting postfix expression is AB+C*DE/.
ii. Conversion from Infix Expression to Prefix Form (Prefix Notation):
Algorithm:
 Reverse the infix expression.
 Replace ‘(‘ with ‘)’ and vice versa.
 Apply the algorithm for conversion from infix to postfix to the reversed expression.
 Reverse the resulting postfix expression to obtain the prefix form.
Example:
Infix expression: (A + B) * C – D / E
Prefix expression: *+ABC/DE
Conversion Steps:
 Reverse infix expression: E / D – C * (B + A)
 Replace ‘(‘ with ‘)’ and vice versa: E / D – C * )B + A(
 Apply infix to postfix algorithm to the reversed expression: E D / C B A + * –
(Conversion steps are the same as in the previous example but applied to the reversed expression)
 Reverse the resulting postfix expression: *+ABC/DE
iii. Conversion from Prefix Expression to Postfix Form:
Algorithm:
 Create an empty stack and an empty string to store the postfix expression.
 Reverse the prefix expression.
 Scan the reversed prefix expression from left to right.
 If the scanned character is an operand, append it to the postfix string.
 If the scanned character is an operator, pop two operands from the stack and append them to the postfix string, followed by the operator.
 Repeat steps 45 until all characters in the reversed prefix expression have been scanned.
 Reverse the resulting postfix string to obtain the postfix form of the prefix expression.
Example:
Prefix expression: +ABC/DE
Postfix expression: AB+CDE/
Conversion Steps:
 Reverse prefix expression: +*A/EDCB
 Scan ‘‘: Stack: [], Postfix: [empty]
 Scan ‘+’: Stack: [, +], Postfix: [empty]
 Scan ‘*’: Stack: [, +, *], Postfix: [empty]
 Scan ‘A’: Append ‘A’ to Postfix: Stack: [, +], Postfix: A
 Scan ‘/’: Stack: [, +, /], Postfix: A
 Scan ‘E’: Append ‘E’ to Postfix: Stack: [, +], Postfix: AE
 Scan ‘D’: Append ‘D’ to Postfix: Stack: [, +], Postfix: AED
 Pop ‘/’ from stack and append to Postfix: Stack: [, +], Postfix: AED/
 Pop ‘+’ from stack and append to Postfix: Stack: [], Postfix: AED/+
 Scan ‘C’: Append ‘C’ to Postfix: Stack: [], Postfix: AED/+C
 Pop ‘‘ from stack and append to Postfix: Stack: [empty], Postfix: AED/+C
 Reverse the resulting postfix expression: AB+C*DE/
Note: In the conversion from prefix to postfix, the operator precedence and associativity are preserved, as in the original prefix expression.
Here are the C programs for each of the conversions:
i. Infix to Postfix Conversion:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
char stack[MAX_SIZE];
int top;
} Stack;
void initialize(Stack* s) {
s>top = 1;
}
int isEmpty(Stack* s) {
return s>top == 1;
}
int isFull(Stack* s) {
return s>top == MAX_SIZE – 1;
}
void push(Stack* s, char value) {
if (isFull(s)) {
printf(“Stack Overflow\n”);
return;
}
s>stack[++(s>top)] = value;
}
char pop(Stack* s) {
if (isEmpty(s)) {
printf(“Stack Underflow\n”);
return ‘\0’;
}
return s>stack[(s>top)–];
}
int isOperator(char ch) {
return (ch == ‘+’  ch == ‘‘  ch == ‘*’  ch == ‘/’);
}
int getPrecedence(char ch) {
switch (ch) {
case ‘+’:
case ‘‘:
return 1;
case ‘*’:
case ‘/’:
return 2;
}
return 0;
}
void infixToPostfix(char* infix, char* postfix) {
Stack stack;
initialize(&stack);
int i, j = 0;
for (i = 0; infix[i] != ‘\0’; i++) {
char ch = infix[i];
if (isalnum(ch)) {
postfix[j++] = ch;
} else if (ch == ‘(‘) {
push(&stack, ch);
} else if (ch == ‘)’) {
while (!isEmpty(&stack) && stack.stack[stack.top] != ‘(‘) {
postfix[j++] = pop(&stack);
}
if (!isEmpty(&stack) && stack.stack[stack.top] == ‘(‘) {
pop(&stack);
}
} else if (isOperator(ch)) {
while (!isEmpty(&stack) && getPrecedence(stack.stack[stack.top]) >= getPrecedence(ch)) {
postfix[j++] = pop(&stack);
}
push(&stack, ch);
}
}
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}
postfix[j] = ‘\0’;
}
int main() {
char infix[MAX_SIZE];
char postfix[MAX_SIZE];
printf(“Enter the infix expression: “);
gets(infix);
infixToPostfix(infix, postfix);
printf(“Postfix expression: %s\n”, postfix);
return 0;
}
ii. Infix to Prefix Conversion:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
char stack[MAX_SIZE];
int top;
} Stack;
void initialize(Stack* s) {
s>top = 1;
}
int isEmpty(Stack* s) {
return s>top == 1;
}
int isFull(Stack* s) {
return s>top == MAX_SIZE – 1;
}
void push(Stack* s, char value) {
if (isFull(s)) {
printf(“Stack Overflow\n”);
return;
}
s>stack[++(s>top)] = value;
}
char pop(Stack* s) {
if (isEmpty(s)) {
printf(“Stack Underflow\n”);
return ‘\0’;
}
return s>stack[(s>top)–];
}
int isOperator(char ch) {
return (ch == ‘+’  ch == ‘‘  ch == ‘*’  ch == ‘/’);
}
int getPrecedence(char ch) {
switch (ch) {
case ‘+’:
case ‘‘:
return 1;
case ‘*’:
case ‘/’:
return 2;
}
return 0;
}
void reverseString(char* str) {
int i, j;
for (i = 0, j = strlen(str) – 1; i < j; i++, j–) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
void infixToPrefix(char* infix, char* prefix) {
Stack stack;
initialize(&stack);
reverseString(infix);
int i, j = 0;
for (i = 0; infix[i] != ‘\0’; i++) {
char ch = infix[i];
if (isalnum(ch)) {
prefix[j++] = ch;
} else if (ch == ‘)’) {
push(&stack, ch);
} else if (ch == ‘(‘) {
while (!isEmpty(&stack) && stack.stack[stack.top] != ‘)’) {
prefix[j++] = pop(&stack);
}
if (!isEmpty(&stack) && stack.stack[stack.top] == ‘)’) {
pop(&stack);
}
} else if (isOperator(ch)) {
while (!isEmpty(&stack) && getPrecedence(stack.stack[stack.top]) > getPrecedence(ch)) {
prefix[j++] = pop(&stack);
}
push(&stack, ch);
}
}
while (!isEmpty(&stack)) {
prefix[j++] = pop(&stack);
}
prefix[j] = ‘\0’;
reverseString(prefix);
}
int main() {
char infix[MAX_SIZE];
char prefix[MAX_SIZE];
printf(“Enter the infix expression: “);
gets(infix);
infixToPrefix(infix, prefix);
printf(“Prefix expression: %s\n”, prefix);
return 0;
}
iii. Prefix to Postfix Conversion:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
char stack[MAX_SIZE];
int top;
} Stack;
void initialize(Stack* s) {
s>top = 1;
}
int isEmpty(Stack* s) {
return s>top == 1;
}
int isFull(Stack* s) {
return s>top == MAX_SIZE – 1;
}
void push(Stack* s, char value) {
if (isFull(s)) {
printf(“Stack Overflow\n”);
return;
}
s>stack[++(s>top)] = value;
}
char pop(Stack* s) {
if (isEmpty(s)) {
printf(“Stack Underflow\n”);
return ‘\0’;
}
return s>stack[(s>top)–];
}
int isOperator(char ch) {
return (ch == ‘+’  ch == ‘‘  ch == ‘*’  ch == ‘/’);
}
void prefixToPostfix(char* prefix, char* postfix) {
Stack stack;
initialize(&stack);
int i, j = 0;
for (i = strlen(prefix) – 1; i >= 0; i–) {
char ch = prefix[i];
if (isalnum(ch)) {
postfix[j++] = ch;
} else if (isOperator(ch)) {
char operand1 = pop(&stack);
char operand2 = pop(&stack);
postfix[j++] = operand1;
postfix[j++] = operand2;
postfix[j++] = ch;
push(&stack, ch);
}
}
postfix[j] = ‘\0’;
}
int main() {
char prefix[MAX_SIZE];
char postfix[MAX_SIZE];
printf(“Enter the prefix expression: “);
gets(prefix);
prefixToPostfix(prefix, postfix);
printf(“Postfix expression: %s\n”, postfix);
return 0;
}
These programs will convert the expressions as described. Simply compile and run the programs to see the output.
Concept of Recursion
Recursion is a programming concept where a function calls itself to solve a problem. It allows a problem to be solved by breaking it down into smaller, simpler subproblems. The function keeps calling itself with smaller inputs until a base case is reached, which is a condition that terminates the recursion.
In C, recursion is implemented using a recursive function, which is a function that calls itself within its own definition. The recursive function consists of two main components:
 Base Case: It is the condition that specifies when the recursion should stop. When the base case is satisfied, the function does not call itself and returns a specific value.
 Recursive Case: It is the part of the function where the function calls itself with a smaller input. The recursive case breaks down the problem into smaller subproblems and eventually leads to the base case.
Here is an example of a recursive function in C to calculate the factorial of a number:
#include <stdio.h>
int factorial(int n) {
// Base case: If n is 0 or 1, return 1
if (n == 0  n == 1) {
return 1;
}
// Recursive case: Call the factorial function with n1 and multiply it with n
else {
return n * factorial(n – 1);
}
}
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
int result = factorial(num);
printf(“Factorial of %d is %d\n”, num, result);
return 0;
}
In this example, the factorial function is defined to calculate the factorial of a number. It uses recursion by calling itself with a smaller input n – 1 until the base case is reached (n == 0 or n == 1). The function returns the factorial of the number by multiplying n with the factorial of n – 1.
Recursion is a powerful technique for solving problems that can be divided into smaller subproblems. However, it is important to ensure that recursive functions have a base case and progress towards it, to avoid infinite recursion.
Describe Simulating Recursion and Tail Recursion
Simulating Recursion:
Simulating recursion refers to implementing recursive functionality using iterative methods, typically using a stack data structure. Instead of making recursive function calls, an explicit stack is used to keep track of the function calls and their corresponding parameters. The stack is used to mimic the behavior of function calls and their activation records.
The process of simulating recursion involves manually managing the stack and iterating through the problem’s solution space. The iterative solution maintains a stack of intermediate states and iteratively processes each state until the desired result is obtained. By using a stack, the function calls are simulated in a nonrecursive manner.
Simulating recursion can be beneficial in situations where direct recursion is not suitable, such as when there are limitations on the maximum depth of recursion or when tail recursion optimization is not available.
Tail Recursion:
Tail recursion is a special case of recursion where the recursive call occurs at the end of a function. In tail recursion, the recursive call is the last operation performed in the function, and there is no further computation required after the recursive call.
Tail recursion is significant because it enables tail call optimization (TCO), which is a compiler optimization technique. TCO eliminates the need for additional stack frames to be allocated for recursive function calls. Instead of creating a new stack frame for each recursive call, TCO reuses the existing stack frame, effectively transforming the recursive call into a looplike structure.
By optimizing tail recursion, the compiler ensures that the function does not consume additional stack space with each recursive call. This optimization can lead to more efficient memory usage and prevent stack overflow errors when dealing with large recursion depths.
It’s important to note that not all programming languages or compilers automatically optimize tail recursion. Some languages explicitly support tail call optimization, while others may require manual optimization techniques, such as using an accumulator variable to store intermediate results and avoid the need for recursive calls.
In summary, simulating recursion involves implementing recursive functionality using iterative methods, typically with a stack data structure. Tail recursion, on the other hand, refers to a special case of recursion where the recursive call is the last operation in a function, enabling tail call optimization for efficient memory usage.
Here are examples of simulating recursion and tail recursion in C:
 Simulating Recursion:
#include <stdio.h>
#define MAX_STACK_SIZE 100
void simulateRecursion(int n) {
int stack[MAX_STACK_SIZE];
int top = 1;
// Push initial value to the stack
stack[++top] = n;
while (top >= 0) {
// Pop the top value from the stack
int value = stack[top–];
// Base case: Print the value
if (value == 0) {
printf(“%d “, value);
}
// Recursive case: Push the smaller subproblem to the stack
else {
stack[++top] = value – 1;
stack[++top] = value – 1;
printf(“%d “, value);
}
}
}
int main() {
int n;
printf(“Enter a number: “);
scanf(“%d”, &n);
printf(“Simulating Recursion: “);
simulateRecursion(n);
return 0;
}
In this example, the simulateRecursion function simulates the behavior of a recursive function by using an explicit stack. It pushes the initial value n to the stack and then enters a loop. Inside the loop, it pops the top value from the stack and performs the necessary operations based on the value. If the value is zero, it prints the value. Otherwise, it pushes the smaller subproblem (value – 1) to the stack. The loop continues until the stack is empty.
 Tail Recursion:
#include <stdio.h>
void tailRecursion(int n, int accumulator) {
// Base case: Print the accumulator
if (n == 0) {
printf(“%d “, accumulator);
return;
}
// Recursive case: Update the accumulator and make a tailrecursive call
accumulator += n;
tailRecursion(n – 1, accumulator);
}
int main() {
int n;
printf(“Enter a number: “);
scanf(“%d”, &n);
printf(“Tail Recursion: “);
tailRecursion(n, 0);
return 0;
}
In this example, the tailRecursion function demonstrates tail recursion. It takes two parameters: n (the input value) and accumulator (to store intermediate results). In each recursive call, the function updates the accumulator by adding the current value of n. Instead of making a normal recursive call, it makes a tailrecursive call by passing the updated accumulator value and the decremented n. The function continues until the base case is reached, where it prints the final value of the accumulator.
Differentiate between Recursion and Iteration
Here’s a comparison between recursion and iteration in tabular form:
Recursion  Iteration  
Definition  A programming concept where a function calls itself to solve a problem.  A loop construct that repeatedly executes a block of code. 
Approach  Topdown approach, breaking down a problem into smaller subproblems.  Bottomup approach, executing code repeatedly based on a condition. 
Execution  Recursive function calls itself with different input parameters.  Loop iterates over a block of code until a condition is met. 
Control Flow  Execution flow depends on the function calls and returns.  Execution flow follows the loop condition and loop counter. 
Memory Usage  Recursive function calls create additional stack frames for each call, which consume memory.  Iteration uses a fixed amount of memory for loop variables. 
Code Structure  Recursive functions tend to have shorter, more concise code.  Iterative code may have longer code with explicit loop conditions and counter updates. 
Complexity  Recursive solutions may have simpler logic, but may have higher time and space complexity due to function call overhead.  Iterative solutions may have more complex logic, but can have optimized time and space complexity. 
Examples  Tree traversal, factorial calculation using recursion.  For loop, while loop, dowhile loop for iterative tasks. 
It’s important to note that recursion and iteration are different approaches for solving problems, and the choice between them depends on the specific problem and the tradeoffs involved. Both techniques have their own advantages and disadvantages, and the choice often depends on factors like code readability, efficiency, and the problem’s nature.
Recall Tower of Hanoi (TOH) Problem
The Tower of Hanoi (TOH) problem is a classic puzzle that involves three rods and a set of disks of different sizes. The objective is to move all the disks from one rod to another, following a specific set of rules:
 Only one disk can be moved at a time.
 Each move consists of taking the top disk from one of the rods and placing it on another rod.
 A larger disk cannot be placed on top of a smaller disk.
The problem can be stated as follows:
Given three rods labeled A, B, and C, and a stack of n disks initially placed on rod A in decreasing order of size (with the largest disk at the bottom), the task is to move all the disks from rod A to rod C using rod B as an intermediate rod.
The recursive algorithm to solve the Tower of Hanoi problem can be described as:
 If there is only one disk to move, simply move it from the source rod to the destination rod.
 Otherwise, recursively move n1 disks from the source rod to the auxiliary rod (using the destination rod as an intermediate).
 Move the remaining largest disk from the source rod to the destination rod.
 Recursively move the n1 disks from the auxiliary rod to the destination rod (using the source rod as an intermediate).
The Tower of Hanoi problem is often used to illustrate the concept of recursion and its application in problemsolving. It demonstrates the power of breaking down a complex problem into simpler subproblems and solving them recursively.
The minimum number of moves required to solve the Tower of Hanoi problem with n disks is 2^n – 1.
Write a program to implement TOH Problem
Here’s a C program to implement the Tower of Hanoi problem:
#include <stdio.h>
void towerOfHanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
printf(“Move disk 1 from rod %c to rod %c\n”, source, destination);
return;
}
towerOfHanoi(n – 1, source, auxiliary, destination);
printf(“Move disk %d from rod %c to rod %c\n”, n, source, destination);
towerOfHanoi(n – 1, auxiliary, destination, source);
}
int main() {
int numDisks;
printf(“Enter the number of disks: “);
scanf(“%d”, &numDisks);
printf(“Tower of Hanoi steps:\n”);
towerOfHanoi(numDisks, ‘A’, ‘C’, ‘B’);
return 0;
}
In this program, the towerOfHanoi function is defined to solve the Tower of Hanoi problem recursively. It takes four parameters: n (the number of disks), source (the source rod), destination (the destination rod), and auxiliary (the auxiliary rod).
The base case is when there is only one disk (n == 1), in which case the program simply moves the disk from the source rod to the destination rod and returns.
For the recursive case, the function follows the steps described earlier: recursively move n1 disks from the source rod to the auxiliary rod, then move the remaining largest disk from the source rod to the destination rod, and finally recursively move the n1 disks from the auxiliary rod to the destination rod.
In the main function, the user is prompted to enter the number of disks. The program then calls the towerOfHanoi function with the initial rods (A, C, and B) and displays the steps for solving the Tower of Hanoi problem.
When you run the program and enter the number of disks, it will output each step required to solve the Tower of Hanoi problem.
Describe EightQueen Puzzle Problem
The EightQueen puzzle problem is a classic problem in which the task is to place eight queens on an 8×8 chessboard in such a way that no two queens threaten each other. In chess, a queen can move horizontally, vertically, and diagonally, so the solution must ensure that no two queens share the same row, column, or diagonal.
The problem can be stated as follows:
Given an 8×8 chessboard, the task is to find a placement of eight queens such that no two queens threaten each other. In other words, each row, column, and diagonal should contain at most one queen.
The goal is to find all possible solutions to the problem, considering different arrangements of queens on the chessboard. There can be multiple valid solutions, and the objective is to find all of them.
To solve the EightQueen puzzle problem, backtracking is a commonly used approach. The algorithm can be described as follows:
 Start with an empty chessboard.
 Begin with the first row and iterate through each column.
 Check if placing a queen in the current position is valid (i.e., it does not threaten any other queen).
 If the position is valid, place the queen in that cell.
 Move to the next row and repeat steps 34 recursively.
 If the position is not valid or we have reached the last row, backtrack to the previous row and continue with the next column.
 Repeat steps 36 until all valid solutions are found.
The EightQueen puzzle problem is a challenging problem that requires an efficient algorithm to find all possible solutions. It demonstrates the importance of backtracking and systematic exploration of the solution space to find valid arrangements of the queens on the chessboard.
Here’s an example solution to the EightQueen puzzle problem:
#include <stdio.h>
#define N 8
int board[N][N] = {0};
int isSafe(int row, int col) {
int i, j;
// Check if there is a queen in the same row
for (i = 0; i < col; i++) {
if (board[row][i] == 1) {
return 0;
}
}
// Check if there is a queen in the upper left diagonal
for (i = row, j = col; i >= 0 && j >= 0; i–, j–) {
if (board[i][j] == 1) {
return 0;
}
}
// Check if there is a queen in the lower left diagonal
for (i = row, j = col; i < N && j >= 0; i++, j–) {
if (board[i][j] == 1) {
return 0;
}
}
return 1;
}
void printBoard() {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf(“%d “, board[i][j]);
}
printf(“\n”);
}
printf(“\n”);
}
int solve(int col) {
int i;
if (col >= N) {
// All queens are successfully placed, print the solution
printBoard();
return 1;
}
for (i = 0; i < N; i++) {
if (isSafe(i, col)) {
// Place the queen in the current cell
board[i][col] = 1;
// Recursively solve for the next column
solve(col + 1);
// Backtrack and remove the queen from the current cell
board[i][col] = 0;
}
}
return 0;
}
int main() {
solve(0);
return 0;
}
In this example program, the solution to the EightQueen puzzle problem is found using a recursive approach. The isSafe function checks if it is safe to place a queen in a particular cell on the chessboard. The solve function recursively tries different combinations of queen placements, backtracking when a solution is not valid. The printBoard function is used to print each valid solution found.
When you run this program, it will output all the valid solutions to the EightQueen puzzle problem, where each queen is represented by a 1 on the chessboard. The program explores different arrangements of queens on the board, ensuring that no two queens threaten each other.
Recall the concept of Backtracking
Backtracking is a problemsolving technique that involves exploring all possible solutions to a problem by incrementally building a solution and undoing certain steps (backtracking) when they are found to be incorrect or lead to a dead end. It is commonly used when the problem can be formulated as a search through a large solution space.
The backtracking algorithm can be described as follows:
 Define the problem in terms of a solution space, and identify the constraints and goals of the problem.
 Start with an empty or initial partial solution.
 Make a decision to extend the current partial solution by adding a new element or making a choice.
 Check if the current partial solution violates any constraints. If it does, backtrack to the previous decision point and try a different option.
 If the current partial solution satisfies the constraints and represents a valid solution, accept it and store it.
 Recursively explore further by making another decision and repeating steps 35.
 If all possibilities have been explored and no valid solution is found, backtrack to the previous decision point and try a different option.
 Repeat steps 37 until all possible solutions have been found or the search space has been exhausted.
Backtracking is characterized by its ability to prune branches of the search space by quickly determining when a partial solution cannot lead to a valid solution. It avoids unnecessary exploration of the entire solution space, making it an efficient approach for solving problems with large solution spaces.
Backtracking is commonly used to solve problems such as permutations, combinations, subset generation, maze traversal, sudoku, and many more. It is a fundamental technique in algorithm design and is often combined with other strategies to optimize the search process.
Recall the concept of Multiple Stacks
The concept of Multiple Stacks refers to a data structure that allows the simultaneous management of multiple stacks within a single data structure. Each stack operates independently, and the data within each stack is kept separate from the others.
Multiple stacks are typically implemented using an array or a linked list, where each stack is allocated a portion of the memory. The memory for each stack is divided into segments, and each segment contains the elements of a specific stack. The segments can be of fixed size or dynamically resized as needed.
Multiple stacks can be useful in situations where there is a need to maintain multiple separate stacks, but the overall memory usage needs to be optimized. Instead of allocating separate memory for each stack, a single data structure can be used to manage all the stacks, reducing memory overhead.
The operations performed on multiple stacks are similar to those performed on a single stack, including push (insertion), pop (removal), peek (accessing the top element), and checking if a stack is empty or full. However, these operations need to be implemented in a way that ensures data integrity and separation between the different stacks.
Multiple stacks find applications in various domains, including expression evaluation, memory management, task scheduling, and resource allocation. They provide a convenient and efficient way to manage multiple stacks within a single data structure, offering flexibility and space optimization.
Write a program to implement Multiple Stacks
Here’s an example of implementing multiple stacks using an array in C:
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACKS 3
#define MAX_SIZE 10
typedef struct {
int* data;
int top;
} Stack;
Stack stacks[MAX_STACKS];
int stackIndex = 1;
void push(int stackNumber, int element) {
if (stackNumber < 0  stackNumber >= MAX_STACKS) {
printf(“Invalid stack number\n”);
return;
}
if (stacks[stackNumber].top == MAX_SIZE – 1) {
printf(“Stack %d is full\n”, stackNumber);
return;
}
stacks[stackNumber].data[++stacks[stackNumber].top] = element;
}
int pop(int stackNumber) {
if (stackNumber < 0  stackNumber >= MAX_STACKS) {
printf(“Invalid stack number\n”);
return 1;
}
if (stacks[stackNumber].top == 1) {
printf(“Stack %d is empty\n”, stackNumber);
return 1;
}
return stacks[stackNumber].data[stacks[stackNumber].top–];
}
int peek(int stackNumber) {
if (stackNumber < 0  stackNumber >= MAX_STACKS) {
printf(“Invalid stack number\n”);
return 1;
}
if (stacks[stackNumber].top == 1) {
printf(“Stack %d is empty\n”, stackNumber);
return 1;
}
return stacks[stackNumber].data[stacks[stackNumber].top];
}
int main() {
int i;
// Initialize the stacks
for (i = 0; i < MAX_STACKS; i++) {
stacks[i].data = (int*)malloc(MAX_SIZE * sizeof(int));
stacks[i].top = 1;
}
// Push elements into the stacks
push(0, 10);
push(0, 20);
push(1, 30);
push(2, 40);
// Pop elements from the stacks
int poppedValue = pop(0);
printf(“Popped value from stack 0: %d\n”, poppedValue);
poppedValue = pop(1);
printf(“Popped value from stack 1: %d\n”, poppedValue);
// Peek into the stacks
int peekedValue = peek(2);
printf(“Peeked value from stack 2: %d\n”, peekedValue);
// Free the memory
for (i = 0; i < MAX_STACKS; i++) {
free(stacks[i].data);
}
return 0;
}
In this example, we have implemented an arraybased multiple stacks data structure. The Stack struct represents each individual stack, with a dynamic array to hold the stack elements and a top variable to keep track of the top element. The stacks array holds the multiple stacks, and stackIndex keeps track of the current stack.
The push function is used to insert an element into the specified stack, checking for stack full condition. The pop function removes and returns the top element from the specified stack, checking for stack empty condition. The peek function returns the top element of the specified stack without removing it.
In the main function, we demonstrate the usage of the multiple stacks data structure by pushing elements into different stacks, popping elements, and peeking at the top elements.
Remember to free the allocated memory at the end to prevent memory leaks.
Describe Queue and its operations
A queue is a linear data structure that follows the FirstInFirstOut (FIFO) principle, where the elements are inserted at one end called the rear and removed from the other end called the front. It can be visualized as a line of people waiting in a queue, where the person who arrives first is served first.
The operations commonly performed on a queue are:
 Enqueue (Insertion): This operation adds an element to the rear of the queue. It takes the element as a parameter and adds it to the end of the queue.
 Dequeue (Deletion): This operation removes the element from the front of the queue. It returns the element that is being removed from the queue.
 Front (Peek): This operation returns the element at the front of the queue without removing it. It allows you to access the next element that will be dequeued.
 IsEmpty: This operation checks whether the queue is empty or not. It returns a boolean value indicating whether the queue is empty or not.
 IsFull: This operation checks whether the queue is full or not. In some implementations, a queue has a fixed size, so this operation is used to check if the queue has reached its maximum capacity.
 Size: This operation returns the number of elements currently present in the queue.
The enqueue and dequeue operations maintain the order of the elements in the queue, ensuring that the element inserted first is the first to be removed. The front and rear pointers are used to keep track of the positions of the front and rear elements, allowing efficient insertion and deletion.
Queues find various applications in computer science and everyday scenarios. Some examples include job scheduling, process scheduling in operating systems, breadthfirst search algorithms, printer spooling, and handling requests in a network server.
Implementations of queues can vary, such as using arrays, linked lists, or circular buffers, depending on the requirements of the application and the available resources.
Write Algorithms/Functions to perform Insertion and Deletion operations
Here are the algorithms or C functions to perform insertion (enqueue) and deletion (dequeue) operations on a queue:
 Enqueue (Insertion) Operation:
#define MAX_SIZE 10
int queue[MAX_SIZE];
int front = 1;
int rear = 1;
void enqueue(int element) {
if (rear == MAX_SIZE – 1) {
printf(“Queue is full. Cannot enqueue.\n”);
return;
}
if (front == 1) {
front = 0;
}
rear++;
queue[rear] = element;
printf(“Enqueued element: %d\n”, element);
}
 Dequeue (Deletion) Operation:
int dequeue() {
if (front == 1  front > rear) {
printf(“Queue is empty. Cannot dequeue.\n”);
return 1;
}
int element = queue[front];
front++;
if (front > rear) {
// Reset front and rear to initial state
front = 1;
rear = 1;
}
printf(“Dequeued element: %d\n”, element);
return element;
}
In the above code snippets, we assume that the queue is implemented using an array (queue) with a maximum size defined as MAX_SIZE. The front and rear variables represent the positions of the front and rear elements in the queue.
The enqueue function checks if the queue is full before inserting an element at the rear. It also handles the case where the queue is initially empty by updating the front variable. If the queue becomes full after inserting an element, it displays an appropriate message.
The dequeue function checks if the queue is empty before removing an element from the front. It returns the dequeued element and updates the front variable accordingly. If the queue becomes empty after dequeuing an element, it resets the front and rear variables to their initial state.
Note that the above code snippets assume the elements in the queue are integers. You can modify the code accordingly for different data types or add additional error handling as per your requirements.
Write a Program in C to Implement Queue using Array
Here’s a program in C that implements a queue using an array:
#include <stdio.h>
#define MAX_SIZE 10
int queue[MAX_SIZE];
int front = 1;
int rear = 1;
// Function to check if the queue is empty
int isEmpty() {
return front == 1;
}
// Function to check if the queue is full
int isFull() {
return rear == MAX_SIZE – 1;
}
// Function to enqueue (insert) an element into the queue
void enqueue(int element) {
if (isFull()) {
printf(“Queue is full. Cannot enqueue.\n”);
return;
}
if (isEmpty()) {
front = 0;
}
rear++;
queue[rear] = element;
printf(“Enqueued element: %d\n”, element);
}
// Function to dequeue (remove) an element from the queue
int dequeue() {
if (isEmpty()) {
printf(“Queue is empty. Cannot dequeue.\n”);
return 1;
}
int element = queue[front];
front++;
if (front > rear) {
// Reset front and rear to initial state
front = 1;
rear = 1;
}
printf(“Dequeued element: %d\n”, element);
return element;
}
// Function to display the elements in the queue
void display() {
if (isEmpty()) {
printf(“Queue is empty.\n”);
return;
}
printf(“Queue elements: “);
for (int i = front; i <= rear; i++) {
printf(“%d “, queue[i]);
}
printf(“\n”);
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
enqueue(40);
enqueue(50);
display();
return 0;
}
In this program, we define an array queue with a maximum size of MAX_SIZE. The variables front and rear keep track of the front and rear indices of the queue, respectively. The isEmpty() and isFull() functions are used to check if the queue is empty or full.
The enqueue() function inserts an element into the queue. It checks if the queue is full before adding the element at the rear. If the queue was empty before enqueuing, the front index is updated. The dequeue() function removes an element from the queue. It checks if the queue is empty before dequeuing and updates the front index accordingly. If the queue becomes empty after dequeuing, the front and rear indices are reset to their initial state.
The display() function is used to print the elements in the queue. It checks if the queue is empty and then loops through the elements from front to rear to display them.
In the main() function, we demonstrate the usage of the queue by enqueuing and dequeuing elements and displaying the queue at different stages.
Note that this is a basic implementation of a queue using an array. You can modify the code to handle other data types, implement additional functionalities, or add error handling as per your requirements.
Write a Program in C to implement Queue using Linked List
Here’s a program in C that implements a queue using a linked list:
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a node in the queue
struct Node {
int data;
struct Node* next;
};
// Structure to represent the queue
struct Queue {
struct Node* front;
struct Node* rear;
};
// Function to create an empty queue
struct Queue* createQueue() {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue>front = NULL;
queue>rear = NULL;
return queue;
}
// Function to check if the queue is empty
int isEmpty(struct Queue* queue) {
return queue>front == NULL;
}
// Function to enqueue (insert) an element into the queue
void enqueue(struct Queue* queue, int element) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode>data = element;
newNode>next = NULL;
if (isEmpty(queue)) {
queue>front = newNode;
queue>rear = newNode;
} else {
queue>rear>next = newNode;
queue>rear = newNode;
}
printf(“Enqueued element: %d\n”, element);
}
// Function to dequeue (remove) an element from the queue
int dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf(“Queue is empty. Cannot dequeue.\n”);
return 1;
}
struct Node* temp = queue>front;
int element = temp>data;
queue>front = queue>front>next;
free(temp);
if (queue>front == NULL) {
queue>rear = NULL;
}
printf(“Dequeued element: %d\n”, element);
return element;
}
// Function to display the elements in the queue
void display(struct Queue* queue) {
if (isEmpty(queue)) {
printf(“Queue is empty.\n”);
return;
}
printf(“Queue elements: “);
struct Node* current = queue>front;
while (current != NULL) {
printf(“%d “, current>data);
current = current>next;
}
printf(“\n”);
}
int main() {
struct Queue* queue = createQueue();
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
display(queue);
dequeue(queue);
display(queue);
enqueue(queue, 40);
enqueue(queue, 50);
display(queue);
return 0;
}
In this program, we define a structure Node to represent a node in the queue. It contains an integer data and a pointer next to the next node. We also define a structure Queue to represent the queue. It contains pointers front and rear to the front and rear nodes of the queue, respectively.
The createQueue() function creates an empty queue by allocating memory for the Queue structure and initializing its front and rear pointers to NULL.
The isEmpty() function checks if the queue is empty by checking if the front pointer is NULL.
The enqueue() function inserts an element into the queue. It creates a new node, assigns the element to its data field, and updates the next pointers of the rear node and the rear pointer itself. If the queue is empty, it updates both front and rear to point to the new node.
The dequeue() function removes an element from the queue. It checks if the queue is empty and then removes the front node by updating the front pointer and freeing the memory. It also updates the rear pointer if the queue becomes empty.
The display() function prints the elements in the queue by traversing through the nodes starting from the front pointer and printing their data fields.
In the main() function, we demonstrate the usage of the queue by creating a queue, enqueuing and dequeuing elements, and displaying the queue at different stages.
Note that this is a basic implementation of a queue using a linked list. You can modify the code to handle other data types, implement additional functionalities, or add error handling as per your requirements.
Describe Circular Queue and its operations
A circular queue is a variation of a regular queue where the rear and front pointers are connected in a circular manner. This allows the elements to wrap around and occupy the unused spaces at the beginning of the queue when the rear pointer reaches the end of the underlying array or buffer.
Operations of a circular queue typically include:
 enqueue: Adds an element to the rear of the circular queue. If the queue is full, it displays an overflow message.
 dequeue: Removes the element from the front of the circular queue. If the queue is empty, it displays an underflow message.
 isEmpty: Checks if the circular queue is empty. It returns a boolean value indicating whether the queue is empty or not.
 isFull: Checks if the circular queue is full. It returns a boolean value indicating whether the queue is full or not.
 getFront: Retrieves the element at the front of the circular queue without removing it. If the queue is empty, it displays an error message.
 getRear: Retrieves the element at the rear of the circular queue without removing it. If the queue is empty, it displays an error message.
 display: Displays the elements of the circular queue in the order they are stored.
In the context of a circular queue, the formulas for insertion and deletion are as follows:
Insertion (Enqueue):
 Check if the queue is full by checking if (rear + 1) % MAX_SIZE == front.
 If the queue is full, it means there is no space to insert the element.
 If the queue is not full:
 If the queue is empty (front == 1 and rear == 1), set front and rear to 0 and insert the element at the rear index.
 If the queue is not empty, increment rear by 1 modulo MAX_SIZE and insert the element at the rear index.
Deletion (Dequeue):
 Check if the queue is empty by checking if front == 1 and rear == 1.
 If the queue is empty, it means there are no elements to delete.
 If the queue is not empty:
 If front is equal to rear, it means there is only one element in the queue. Set front and rear to 1 to indicate an empty queue.
 If front is not equal to rear, increment front by 1 modulo MAX_SIZE.
These formulas ensure that the circular nature of the queue is maintained, and elements are inserted and deleted correctly.
Note: The formulas assume that MAX_SIZE is the maximum capacity of the circular queue.
Recall Overflow and Underflow conditions in Circular Queue
Overflow and underflow are conditions that can occur in a circular queue:
 Overflow: Overflow occurs when there is no more space in the circular queue to insert new elements. In a circular queue, the overflow condition is typically reached when the (rear + 1) % MAX_SIZE is equal to the front index. This means that the next position for insertion is the same as the position of the front element, indicating that the queue is full.
 Underflow: Underflow occurs when there are no elements in the circular queue to perform a deletion operation. In a circular queue, the underflow condition is typically reached when both the front and rear indices are 1, indicating an empty queue.
It’s important to handle these overflow and underflow conditions properly to ensure the correct functioning of the circular queue. When an overflow or underflow condition is encountered, appropriate error messages can be displayed or additional logic can be implemented based on the requirements of the application.
Apply the concept of Circular Queue operations and find the value of FRONT and REAR
In a circular queue, the value of FRONT and REAR can be found as follows:
 FRONT: The value of FRONT is the index of the element that is at the front of the queue, and is the next element to be dequeued. The value of FRONT can be found by incrementing it by 1 after each dequeue operation, and taking the modulo of the result with the maximum size of the queue.
FRONT = (FRONT + 1) % MAX_SIZE, where MAX_SIZE is the maximum size of the queue.
 REAR: The value of REAR is the index of the element that was most recently added to the queue. The value of REAR can be found by incrementing it by 1 after each enqueue operation, and taking the modulo of the result with the maximum size of the queue.
REAR = (REAR + 1) % MAX_SIZE, where MAX_SIZE is the maximum size of the queue.
It’s important to note that in a circular queue, the value of FRONT and REAR can never be equal as this would indicate that the queue is empty.
To find the value of the FRONT and REAR indices in a circular queue after performing a series of operations, let’s consider the following sequence of operations:
 Enqueue 10
 Enqueue 20
 Dequeue
 Enqueue 30
 Enqueue 40
 Dequeue
 Enqueue 50
Let’s assume that the circular queue has a maximum size of 5 (MAX_SIZE = 5). Initially, the FRONT and REAR indices are 1 to indicate an empty queue.
 Enqueue 10:
 FRONT = 0, REAR = 0 (front and rear are both set to 0, as it’s the first element)
 Enqueue 20:
 FRONT = 0, REAR = 1 (rear is incremented to 1)
 Dequeue:
 FRONT = 1, REAR = 1 (front is incremented to 1, as the first element is dequeued)
 Enqueue 30:
 FRONT = 1, REAR = 2 (rear is incremented to 2)
 Enqueue 40:
 FRONT = 1, REAR = 3 (rear is incremented to 3)
 Dequeue:
 FRONT = 2, REAR = 3 (front is incremented to 2)
 Enqueue 50:
 FRONT = 2, REAR = 4 (rear is incremented to 4)
After performing these operations, the final values of FRONT and REAR are FRONT = 2 and REAR = 4, respectively.
Note: The value of FRONT and REAR may vary depending on the implementation and the specific operations performed on the circular queue. The above example demonstrates the value changes based on the given operations.
Write Algorithm/Functions to perform insertion and Deletion operations
Here are the algorithms and C functions to perform insertion (enqueue) and deletion (dequeue) operations on a circular queue:
 Circular Queue Insertion (Enqueue) Algorithm:
enqueue(value):
if (rear + 1) % MAX_SIZE == front:
// Circular queue is full
return “Queue Overflow”
else if front == 1 and rear == 1:
// First element is being inserted
front = 0
rear = 0
else:
rear = (rear + 1) % MAX_SIZE
circularQueue[rear] = value
return “Element Inserted Successfully”
 Circular Queue Deletion (Dequeue) Algorithm:
dequeue():
if front == 1 and rear == 1:
// Circular queue is empty
return “Queue Underflow”
else if front == rear:
// Last element is being deleted
front = 1
rear = 1
else:
front = (front + 1) % MAX_SIZE
return “Element Deleted Successfully”
Here’s the corresponding C code for the circular queue insertion and deletion functions:
#define MAX_SIZE 10
int circularQueue[MAX_SIZE];
int front = 1, rear = 1;
// Circular Queue Insertion (Enqueue) Function
void enqueue(int value) {
if ((rear + 1) % MAX_SIZE == front) {
printf(“Queue Overflow\n”);
return;
} else if (front == 1 && rear == 1) {
front = 0;
rear = 0;
} else {
rear = (rear + 1) % MAX_SIZE;
}
circularQueue[rear] = value;
printf(“Element Inserted Successfully\n”);
}
// Circular Queue Deletion (Dequeue) Function
void dequeue() {
if (front == 1 && rear == 1) {
printf(“Queue Underflow\n”);
return;
} else if (front == rear) {
front = 1;
rear = 1;
} else {
front = (front + 1) % MAX_SIZE;
}
printf(“Element Deleted Successfully\n”);
}
You can use these functions to perform insertion and deletion operations on a circular queue in your C program.
Describe Dequeue and its Operations
A Deque (Double Ended Queue) is a linear data structure that allows insertion and deletion of elements from both ends, i.e., front and rear. It combines the properties of both stacks and queues.
Deque supports the following operations:
 Insertion at Front: Add an element at the front of the deque.
 Insertion at Rear: Add an element at the rear of the deque.
 Deletion from Front: Remove an element from the front of the deque.
 Deletion from Rear: Remove an element from the rear of the deque.
 Get Front: Retrieve the element at the front of the deque without removing it.
 Get Rear: Retrieve the element at the rear of the deque without removing it.
 Check if Deque is Empty: Check whether the deque is empty or not.
 Check if Deque is Full: Check whether the deque is full or not.
The operations on a deque can be summarized as follows:
 Insertion at Front:
 If the deque is full, it results in overflow.
 If the deque is empty, set both front and rear indices to 0 and insert the element.
 If the deque is not empty, decrement the front index circularly and insert the element.
 Insertion at Rear:
 If the deque is full, it results in overflow.
 If the deque is empty, set both front and rear indices to 0 and insert the element.
 If the deque is not empty, increment the rear index circularly and insert the element.
 Deletion from Front:
 If the deque is empty, it results in underflow.
 If the deque has only one element, set both front and rear indices to 1.
 If the deque has more than one element, increment the front index circularly.
 Deletion from Rear:
 If the deque is empty, it results in underflow.
 If the deque has only one element, set both front and rear indices to 1.
 If the deque has more than one element, decrement the rear index circularly.
 Get Front: Return the element at the front index of the deque without removing it.
 Get Rear: Return the element at the rear index of the deque without removing it.
 Check if Deque is Empty: Check whether both front and rear indices are 1 to determine if the deque is empty.
 Check if Deque is Full: Check whether (rear + 1) % MAX_SIZE == front to determine if the deque is full.
These operations make a deque a versatile data structure that can be used in various applications where elements need to be inserted or deleted from both ends efficiently.
Recall Overflow and Underflow conditions in Deque
Overflow and underflow are conditions that occur in data structures when certain operations cannot be performed due to limitations in the structure’s capacity or empty state. Here are the definitions of overflow and underflow in different data structures:
 Overflow:
 Stack Overflow: Occurs when a push operation is attempted on a full stack, resulting in the stack exceeding its maximum capacity.
 Queue Overflow: Occurs when an enqueue operation is attempted on a full queue, meaning the queue has reached its maximum size and cannot accommodate additional elements.
 Circular Queue Overflow: Similar to queue overflow, it happens when the rear index catches up with the front index, making it impossible to insert any more elements.
 Deque Overflow: Occurs when an insertion operation is attempted on a full deque, exceeding its capacity.
 Underflow:
 Stack Underflow: Occurs when a pop operation is attempted on an empty stack, meaning there are no elements to remove.
 Queue Underflow: Occurs when a dequeue operation is attempted on an empty queue, indicating that there are no elements to remove.
 Circular Queue Underflow: Similar to queue underflow, it occurs when the front index catches up with the rear index, indicating that there are no elements to remove.
 Deque Underflow: Occurs when a deletion operation is attempted on an empty deque, indicating that there are no elements to remove.
In summary, overflow occurs when attempting to insert elements into a full data structure, while underflow occurs when attempting to remove elements from an empty data structure. Handling these conditions is important to ensure the proper functioning and integrity of the data structure.
Write Algorithms/Functions to perform Insertion and Deletion Operations on Dequeue
Here are the algorithms for performing insertion and deletion operations on a Deque (Double Ended Queue) using C functions:
 Insertion at Front:
void insertFront(int value) {
if (isFull()) {
printf(“Deque Overflow\n”);
return;
}
if (isEmpty()) {
front = rear = 0;
} else if (front == 0) {
front = MAX_SIZE – 1;
} else {
front–;
}
deque[front] = value;
}
 Insertion at Rear:
void insertRear(int value) {
if (isFull()) {
printf(“Deque Overflow\n”);
return;
}
if (isEmpty()) {
front = rear = 0;
} else if (rear == MAX_SIZE – 1) {
rear = 0;
} else {
rear++;
}
deque[rear] = value;
}
 Deletion from Front:
void deleteFront() {
if (isEmpty()) {
printf(“Deque Underflow\n”);
return;
}
if (front == rear) {
front = rear = 1;
} else if (front == MAX_SIZE – 1) {
front = 0;
} else {
front++;
}
}
 Deletion from Rear:
void deleteRear() {
if (isEmpty()) {
printf(“Deque Underflow\n”);
return;
}
if (front == rear) {
front = rear = 1;
} else if (rear == 0) {
rear = MAX_SIZE – 1;
} else {
rear–;
}
}
Please note that isFull() and isEmpty() are helper functions that check if the deque is full or empty, respectively.
These algorithms allow you to insert elements at the front and rear of the deque, as well as delete elements from the front and rear, ensuring proper handling of overflow and underflow conditions.
Describe Priority Queue
A Priority Queue is a data structure that stores elements along with their associated priorities. The priority determines the order in which elements are processed or accessed. In a priority queue, the element with the highest priority is given the highest precedence.
The key characteristics of a priority queue are as follows:
 Ordering: Elements in a priority queue are ordered based on their priority. The element with the highest priority is considered the front or top of the queue.
 Priority Criteria: The priority of elements can be defined based on specific criteria, such as numerical value, time stamp, or any userdefined rule. The priority can be either in ascending or descending order.
 Insertion: When inserting an element into a priority queue, it is placed in a position based on its priority. The element with the highest priority is inserted at the appropriate position to maintain the ordering.
 Removal: When removing an element from a priority queue, the element with the highest priority (i.e., the front or top element) is removed and returned. This ensures that the element with the highest priority is always processed first.
 Dynamic Modification: Elements can be dynamically added or removed from the priority queue based on changing priorities or requirements. Insertion and deletion operations may rearrange the elements to maintain the priority order.
Priority queues are commonly implemented using data structures such as heaps, binary search trees, or arrays. The choice of implementation depends on the specific requirements of the application.
Priority queues are used in various scenarios, including:
 Scheduling tasks or processes based on their priorities.
 Handling events in eventdriven systems.
 Implementing Dijkstra’s algorithm for finding the shortest path in graph traversal.
 Huffman coding for data compression.
 Job scheduling in operating systems.
The operations commonly associated with a priority queue include:
 Insertion: Inserting an element with a given priority into the queue.
 Deletion: Removing the element with the highest priority from the queue.
 Peek: Retrieving the element with the highest priority without removing it.
 Size: Getting the number of elements currently in the priority queue.
 IsEmpty: Checking if the priority queue is empty.
Overall, a priority queue provides an efficient way to manage elements based on their priority, enabling efficient processing and access to elements in order of their importance.
List various Applications of the Queue
Queues are widely used in various applications to efficiently manage and process data in a firstin, firstout (FIFO) manner. Here are some common applications of queues:
 Operating Systems:
 Process Scheduling: Queues are used to manage the execution of processes in a multitasking operating system.
 Input/Output (I/O) Operations: Queues are used to handle I/O requests from various devices such as keyboards, printers, and disks.
 Message Queues: Queues are used for interprocess communication, allowing processes to exchange messages.
 Network Communications:
 Data Packet Routing: Queues are used in routers to manage the flow of data packets through a network.
 Request Handling: Queues are used to handle incoming requests in server applications, ensuring fair and efficient processing.
 Simulations:
 Eventdriven Simulations: Queues are used to manage the sequence of events in a simulation, ensuring they are processed in the correct order.
 Call Center Management: Queues are used to manage incoming calls and distribute them to available operators.
 Task Management:
 Task Queues: Queues are used to manage a list of tasks or jobs, such as in task scheduling systems or job queues.
 Print Spooling: Queues are used to manage print jobs, ensuring they are processed in the order they were submitted.
 Data Structures:
 BreadthFirst Search (BFS): Queues are used in BFS algorithms to traverse and search graphs or trees level by level.
 Caches: Queues are used in cache eviction policies, such as the least recently used (LRU) strategy.
 Waiting Line Management:
 Ticketing Systems: Queues are used in ticketing systems to manage waiting lines at various service points.
 Bank Transactions: Queues are used to manage customer requests in banking systems, such as deposit and withdrawal queues.
 Buffering and Synchronization:
 ProducerConsumer Problem: Queues are used to synchronize data transfer between producer and consumer processes.
 Buffering Data: Queues are used to temporarily store data when the rate of data production and consumption varies.
These are just a few examples of the many applications of queues. Queues provide an efficient and organized way to handle tasks, manage data flow, and ensure proper sequencing in various domains and systems.