Data Structures using C
Comprehensive exam revision notes for BCA-102N Unit 3 (Stacks, Queues & Expression Conversions). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
Data Structures using C
Stacks, Queues & Expression Conversions
Core Definitions & Key Concepts
A Stack and state its governing operational principle
A Stack is a linear data structure following the LIFO (Last In, First Out) principle, where additions (Push) and deletions (Pop) take place exclusively at a single designated end termed the Top of stack.
What are Stack Overflow and Stack Underflow conditions in an array implementation
- Stack Overflow: Attempting to push a new element onto a full stack (
top == MAX - 1). - Stack Underflow: Attempting to pop an element from an empty stack (
top == -1).
Polish Notation and Reverse Polish Notation (RPN)
- Polish Notation (Prefix): Operators precede their operands:
+ A B. - Reverse Polish Notation (Postfix): Operators succeed their operands:
A B +. It requires zero parentheses for unambiguous evaluation.
What is the role of the System Call Stack in recursion
Every function call pushes an Activation Record (Stack Frame) containing return address, parameters, and local variables onto the call stack. Returning pops the frame, restoring caller state.
A Queue and state its governing operational principle
A Queue is a linear data structure following the FIFO (First In, First Out) principle, where additions (Enqueue) take place at the Rear end and deletions (Dequeue) take place at the Front end.
What is the major limitation of a Linear Array Queue and how does a Circular Queue solve it
In a linear queue, once rear == MAX - 1, no new elements can be enqueued even if vacant slots exist at the front due to prior dequeues (false overflow). A Circular Queue wraps rear around to index 0 using modulo arithmetic: rear = (rear + 1) % MAX.
State the Empty and Full conditions for a Circular Queue
- Empty Condition:
front == -1(orfront == reardepending on convention). - Full Condition:
(rear + 1) % MAX == front.
What is a Deque (Double-Ended Queue)? Differentiate Input-Restricted and Output-Restricted Deques
A Deque allows insertion and deletion at both ends (Front and Rear).
- Input-Restricted Deque: Deletions permitted at both ends, but insertions restricted to only one end.
- Output-Restricted Deque: Insertions permitted at both ends, but deletions restricted to only one end.
Conceptual & Architectural Analysis
Trace the Infix to Postfix conversion of A + (B * C - (D / E ^ F) * G) * H using a stack
Using standard operator precedence (^ > *, / > +, -):
| Symbol Scanned | Stack State (Bottom to Top) | Postfix Output Buffer |
|---|---|---|
| A | Empty | A |
| + | + | A |
| ( | + ( | A |
| B | + ( | A B |
| * | + ( * | A B |
| C | + ( * | A B C |
| - | + ( - (pops *) | A B C * |
| ( | + ( - ( | A B C * |
| D | + ( - ( | A B C * D |
| / | + ( - ( / | A B C * D |
| E | + ( - ( / | A B C * D E |
| ^ | + ( - ( / ^ | A B C * D E |
| F | + ( - ( / ^ | A B C * D E F |
| ) | + ( - (pops ^, /) | A B C * D E F ^ / |
| * | + ( - * | A B C * D E F ^ / |
| G | + ( - * | A B C * D E F ^ / G |
| ) | + (pops *, -) | A B C D E F ^ / G - |
| * | + * | A B C D E F ^ / G - |
| H | + * | A B C D E F ^ / G - H |
| End | Empty (pops *, +) | *A B C D E F ^ / G - H +** |
Trace step-by-step the Evaluation of Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 ^ 3 +
Postfix evaluation uses an operand stack. Scanning left to right:
- Push 6, Push 2, Push 3. Stack:
[6, 2, 3]. - Operator
+: Pop 3, Pop 2. Compute2 + 3 = 5. Push 5. Stack:[6, 5]. - Operator
-: Pop 5, Pop 6. Compute6 - 5 = 1. Push 1. Stack:[1]. - Push 3, Push 8, Push 2. Stack:
[1, 3, 8, 2]. - Operator
/: Pop 2, Pop 8. Compute8 / 2 = 4. Push 4. Stack:[1, 3, 4]. - Operator
+: Pop 4, Pop 3. Compute3 + 4 = 7. Push 7. Stack:[1, 7]. - Operator
*: Pop 7, Pop 1. Compute1 * 7 = 7. Push 7. Stack:[7]. - Push 2. Operator
^: Pop 2, Pop 7. Compute7^2 = 49. Push 49. Stack:[49]. - Push 3. Operator
+: Pop 3, Pop 49. Compute49 + 3 = 52. Push 52. Stack:[52].
Final Result: 52.
The implementation of a Priority Queue using an array and explain its real-world applications
A Priority Queue is a queue where each element has an assigned priority. Elements are dequeued in order of priority rather than arrival time. In an ascending priority queue, the element with the lowest numerical value has the highest priority.
- Applications: CPU Process Scheduling (Shortest Job First), Dijkstra's Shortest Path Algorithm, Prim's Minimum Spanning Tree, Bandwidth management in network routers.
In-Depth Analysis & Implementation
Write a complete C program implementing a Circular Queue using an array with Enqueue, Dequeue, and Display operations
1#include <stdio.h>2#include <stdlib.h>3
4#define MAX 55
6int cqueue[MAX];7int front = -1, rear = -1;8
9int isFull() {10 return ((rear + 1) % MAX == front);11}12
13int isEmpty() {14 return (front == -1);15}16
17void enqueue(int val) {18 if (isFull()) {19 printf("Circular Queue Overflow! Cannot insert %d\n", val);20 return;21 }22 if (isEmpty()) {23 front = rear = 0;24 } else {25 rear = (rear + 1) % MAX;26 }27 cqueue[rear] = val;28 printf("Enqueued: %d (Front: %d, Rear: %d)\n", val, front, rear);29}30
31int dequeue() {32 if (isEmpty()) {33 printf("Circular Queue Underflow!\n");34 return -1;35 }36 int val = cqueue[front];37 if (front == rear) {38 front = rear = -1; // Reset when queue becomes empty39 } else {40 front = (front + 1) % MAX;41 }42 printf("Dequeued: %d (New Front: %d, Rear: %d)\n", val, front, rear);43 return val;44}45
46void display() {47 if (isEmpty()) {48 printf("Queue is Empty!\n");49 return;50 }51 printf("Queue Elements: ");52 int i = front;53 while (1) {54 printf("%d ", cqueue[i]);55 if (i == rear) break;56 i = (i + 1) % MAX;57 }58 printf("\n");59}60
61int main() {62 enqueue(10);63 enqueue(20);64 enqueue(30);65 enqueue(40);66 display();67 dequeue();68 dequeue();69 display();70 enqueue(50);71 enqueue(60); // Demonstrates circular wrap-around to index 072 display();73 return 0;74}The Infix to Postfix conversion algorithm using Stack. Write complete C code and explain how parenthesized expressions are handled
1. Algorithm Overview
- Initialize an empty operator stack.
- Scan the infix expression string from left to right:
- If the scanned character is an operand, directly append it to the postfix output.
- If
(, push it onto the stack. - If
), pop and append from the stack until a(is popped. Discard both parentheses. - If an operator
op: while the stack is not empty and precedence of top operator >= precedence ofop(with right-associativity consideration for^), pop and append to output. Then pushop.
- After scanning all tokens, pop and append all remaining operators from the stack.
2. C Implementation
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char stack[100];
int top = -1;
void push(char c) { stack[++top] = c; }
char pop() { return stack[top--]; }
char peek() { return stack[top]; }
int precedence(char op) {
if (op == '^') return 3;
if (op == '*' || op == '/') return 2;
if (op == '+' || op == '-') return 1;
return 0;
}
void infixToPostfix(char infix[], char postfix[]) {
int k = 0;
for (int i = 0; infix[i] != '\0'; i++) {
char ch = infix[i];
if (isalnum(ch)) {
postfix[k++] = ch;
} else if (ch == '(') {
push(ch);
} else if (ch == ')') {
while (top != -1 && peek() != '(') postfix[k++] = pop();
pop(); // Discard '('
} else {
while (top != -1 && precedence(peek()) >= precedence(ch) && ch != '^') {
postfix[k++] = pop();
}
push(ch);
}
}
while (top != -1) postfix[k++] = pop();
postfix[k] = '\0';
}
int main() {
char infix[] = "(A+B)*(C-D)/E";
char postfix[100];
infixToPostfix(infix, postfix);
printf("Infix: %s\nPostfix: %s\n", infix, postfix);
return 0;
}Important Exam Points & Quick Revision
In BCA-102N Unit 3, a tabular trace of Infix-to-Postfix conversion or Postfix evaluation is a guaranteed 5-mark or 10-mark question. In addition, the circular queue C program with modulo arithmetic ((rear + 1) % MAX) appears in almost every alternate university question paper.
100% left on this page — finishing it closes the gap for Stacks, Queues & Expression Conversions.
Stacks, Queues & Expression Conversions still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.