1. Define Stack

A Stack is a linear data structure that follows the LIFO (Last In First Out) principle where insertion and deletion occur at the TOP.


2. What is LIFO principle?

LIFO (Last In First Out) means the element inserted last is removed first.


3. List Stack Operations
  • Push (Insertion)
  • Pop (Deletion)
  • Peek (Top element)
  • isEmpty()
  • isFull()

4. Define Queue

A Queue is a linear data structure that follows the FIFO (First In First Out) principle.


5. What is FIFO principle?

FIFO (First In First Out) means the element inserted first is removed first.


6. What is Circular Queue?

A Circular Queue is a queue in which the last position is connected back to the first position to utilize space efficiently.


7. Define Priority Queue

A Priority Queue is a queue where elements are processed based on priority rather than order of insertion.


8. What is Deque?

A Deque (Double Ended Queue) is a queue in which insertion and deletion can be performed at both ends.


9. Define Overflow and Underflow
  • Overflow → When insertion is attempted in a full data structure
  • Underflow → When deletion is attempted in an empty data structure

10. Mention two applications of Stack
  • Expression evaluation
  • Function calls (recursion)
16-Marks
1. Explain Stack Implementation using Arrays and Linked List.

🔷 STACK

A stack is a linear data structure that follows the LIFO (Last In First Out) principle. All insertions and deletions happen at one end called TOP. Stack is used in expression evaluation, recursion, and conversion of expressions.


📌 1. STACK IMPLEMENTATION USING ARRAY

In array implementation, a fixed-size array is used to store stack elements and a variable top is used to indicate the index of the topmost element. Initially, top = -1, which means the stack is empty. This is the simplest and most common implementation of stack.

🔹 Structure

int stack[MAX];
int top = -1;
🔹 PUSH OPERATION

When a new element is inserted:

  1. Check whether the stack is full.
  2. If full, report overflow.
  3. Otherwise increment top.
  4. Insert the element at stack[top].

if(top == MAX - 1)
    Overflow;
else
{
    top = top + 1;
    stack[top] = value;
}
🔹 POP OPERATION

When an element is removed:

  1. Check whether the stack is empty.
  2. If empty, report underflow.
  3. Otherwise read stack[top].
  4. Decrement top.

if(top == -1)
    Underflow;
else
{
    value = stack[top];
    top = top - 1;
}
🔹 PEEK OPERATION

peek() returns the top element without removing it.

🔹 DIAGRAM
TOP ↓ [30] [20] [10]
🔹 ADVANTAGES OF ARRAY IMPLEMENTATION
  • Simple to implement
  • Easy to access top element
  • Faster due to contiguous memory
🔹 DISADVANTAGES
  • Fixed size
  • Memory may be wasted
  • Overflow occurs if the array is full

📌 2. STACK IMPLEMENTATION USING LINKED LIST

In linked list implementation, each stack element is stored in a node and nodes are connected using pointers. The top pointer points to the first node of the list, which represents the top of the stack. This makes stack dynamic in size.

🔹 Node Structure

struct Node
{
    int data;
    struct Node *next;
};
🔹 PUSH OPERATION

To insert an element:

  1. Create a new node.
  2. Store the value in the node.
  3. Make the new node point to the current top.
  4. Update top to the new node.

newNode->next = top;
top = newNode;
🔹 POP OPERATION

To delete an element:

  1. Check if stack is empty.
  2. Store the current top in a temporary pointer.
  3. Move top to the next node.
  4. Free the old top node.

temp = top;
top = top->next;
free(temp);
🔹 DIAGRAM
TOP → [30 | •] → [20 | •] → [10 | NULL]
🔹 ADVANTAGES OF LINKED LIST IMPLEMENTATION
  • Dynamic size
  • No fixed capacity
  • Better memory utilization
🔹 DISADVANTAGES
  • Extra memory needed for pointer
  • Slightly complex implementation

📊 ARRAY VS LINKED LIST IMPLEMENTATION OF STACK
FeatureArrayLinked List
SizeFixedDynamic
MemoryContiguousNon-contiguous
OverflowPossibleOnly if memory exhausted
AccessFasterSlightly slower
FlexibilityLowHigh

🔷 TIME COMPLEXITY
OperationArrayLinked List
PushO(1)O(1)
PopO(1)O(1)
PeekO(1)O(1)

2. Discuss Applications of Stack (Infix to Postfix, Evaluation of Expression).

🔷 APPLICATIONS OF STACK

The syllabus and unit notes show stack applications such as expression evaluation, expression conversion, recursion, and Tower of Hanoi. Stack is especially useful because it follows LIFO order, which matches the way expressions are processed and function calls are stored.


📌 1. INFIX TO POSTFIX CONVERSION
🔹 Infix Notation

Operator is written between operands.

Example: A + B

🔹 Postfix Notation

Operator is written after operands.

Example: AB+

🔹 Why Stack Is Used

Operators must be arranged according to precedence and associativity. Stack stores operators temporarily until they can be placed in the correct order.

🔹 RULES
  1. If symbol is an operand, add it to output.
  2. If symbol is (, push it on stack.
  3. If symbol is ), pop until ( is found.
  4. If symbol is an operator, pop operators with higher or equal precedence and then push the current operator.
🔹 EXAMPLE

Convert: A + B * C

Postfix: ABC*+

🔹 ANOTHER EXAMPLE

Convert: 3 * 3 / (4 - 1) + 6 * 2

Postfix: 33*41-/62*+

The unit notes show this exact kind of step-by-step postfix conversion using stack.


📌 2. EXPRESSION EVALUATION

Postfix expressions are easy to evaluate using a stack.

🔹 ALGORITHM
  1. Scan the expression from left to right.
  2. If operand is found, push it.
  3. If operator is found, pop two operands.
  4. Perform the operation.
  5. Push the result back.
  6. Final stack value is the answer.

This is a standard application of stack in expression evaluation.

🔹 EXAMPLE

Expression: 2 3 1 * + 9 -

Step-by-step:

Push 2

Push 3

Push 1

* → 3 × 1 = 3

+ → 2 + 3 = 5

Push 9

- → 5 - 9 = -4

Final answer: -4

📌 3. OTHER APPLICATIONS OF STACK
  • Recursion
  • Tower of Hanoi
  • Maze solving
  • Parenthesis balancing
  • Undo/redo operations
📊 COMPARISON OF NOTATIONS
NotationFormExample
InfixOperand Operator OperandA + B
PrefixOperator Operand Operand+AB
PostfixOperand Operand OperatorAB+

3. Explain Queue Implementation using Arrays and Linked List.

🔷 QUEUE

A queue is a linear data structure that follows the FIFO (First In First Out) principle. The syllabus includes queue representation using arrays and linked lists, along with circular queue, priority queue, and queue applications.


📌 1. QUEUE IMPLEMENTATION USING ARRAY

In array implementation, a fixed-size array is used with two pointers:

  • front for deletion
  • rear for insertion

Initially:

front = -1
rear = -1

🔹 ENQUEUE OPERATION

Steps:

  1. Check if queue is full.
  2. If full, report overflow.
  3. If queue is empty, set front = rear = 0.
  4. Otherwise increment rear.
  5. Insert element at queue[rear].

if(rear == MAX - 1)
    Overflow;
else
{
    if(front == -1)
        front = 0;
    rear = rear + 1;
    queue[rear] = value;
}
🔹 DEQUEUE OPERATION

Steps:

  1. Check if queue is empty.
  2. If empty, report underflow.
  3. Read element at queue[front].
  4. Increment front.

if(front == -1 || front > rear)
    Underflow;
else
{
    value = queue[front];
    front = front + 1;
}
🔹 DIAGRAM
FRONT → [10] [20] [30] ← REAR
🔹 ADVANTAGES
  • Simple
  • Easy to implement
🔹 DISADVANTAGES
  • Fixed size
  • Space may be wasted

📌 2. QUEUE IMPLEMENTATION USING LINKED LIST

In linked list implementation, nodes are dynamically created and linked. Two pointers are maintained:

  • front points to first node
  • rear points to last node

This is more flexible because the queue can grow dynamically.

🔹 NODE STRUCTURE

struct Node
{
    int data;
    struct Node *next;
};
🔹 ENQUEUE
  1. Create new node.
  2. Insert it at rear.
  3. Update rear pointer.
🔹 DEQUEUE
  1. Check if queue is empty.
  2. Remove node from front.
  3. Move front pointer to next node.
🔹 DIAGRAM
FRONT → [10 | •] → [20 | •] → [30 | NULL] ← REAR
🔹 ADVANTAGES
  • Dynamic size
  • Better memory utilization
🔹 DISADVANTAGES
  • More complex
  • Extra pointer memory needed

📊 ARRAY VS LINKED LIST QUEUE
FeatureArrayLinked List
SizeFixedDynamic
InsertionO(1)O(1)
DeletionO(1)O(1)
MemoryContiguousDynamic nodes
OverflowPossibleOnly memory-based

4. Explain Circular Queue with Algorithm.

🔷 CIRCULAR QUEUE

A circular queue is a queue where the last position is connected back to the first position, so memory is used efficiently. The syllabus specifically includes circular queue under queue operations.


📌 WHY CIRCULAR QUEUE IS NEEDED

In a normal linear queue, deleted spaces at the front are not reused properly. Circular queue removes this limitation by wrapping around using modulo arithmetic.


📌 STRUCTURE
[10] [20] [30] [40] ↑ ↓ └───────────────┘

📌 BASIC VARIABLES

front = -1
rear = -1


📌 INSERTION ALGORITHM (ENQUEUE)
  1. Check overflow condition.
  2. If queue is empty, set front = rear = 0.
  3. Otherwise update rear using modulo.
  4. Insert the element.
🔹 ALGORITHM

if((rear + 1) % MAX == front)
    Overflow;
else if(front == -1)
{
    front = rear = 0;
    queue[rear] = value;
}
else
{
    rear = (rear + 1) % MAX;
    queue[rear] = value;
}

📌 DELETION ALGORITHM (DEQUEUE)
  1. Check if queue is empty.
  2. Remove the element at front.
  3. If only one element is present, reset queue.
  4. Otherwise move front using modulo.
🔹 ALGORITHM

if(front == -1)
    Underflow;
else if(front == rear)
{
    front = rear = -1;
}
else
{
    front = (front + 1) % MAX;
}

📌 EXAMPLE

Queue size = 5

Insert 10, 20, 30
front = 0, rear = 2

Delete 10
front = 1, rear = 2

Insert 40, 50
rear wraps around to use free space

🔹 DIAGRAM
Index: 0 1 2 3 4 [ ] [20] [30] [40] [50] ↑ ↑ FRONT REAR

📌 ADVANTAGES
  • Better space utilization
  • No wastage of deleted slots
  • Useful in buffers and scheduling
📌 DISADVANTAGES
  • Slightly complex
  • Full and empty conditions are harder to detect

5. Discuss Priority Queue and Its Applications.

🔷 PRIORITY QUEUE

A priority queue is a queue in which each element is assigned a priority, and the element with higher priority is removed before lower priority elements. Priority queue is part of the queue-related topics in your syllabus.


📌 TYPES OF PRIORITY QUEUE
  1. Ascending priority queue
    Smaller value means higher priority.
  2. Descending priority queue
    Larger value means higher priority.

📌 WORKING

Insertion happens based on priority or at the end with priority tracking.

Deletion removes the element with the highest priority.


📌 EXAMPLE

Elements:
A(2), B(5), C(1), D(4)

If lower number = higher priority:
C(1) → A(2) → D(4) → B(5)


📌 IMPLEMENTATION

Priority queue can be implemented using:

  • Array
  • Linked list
  • Heap

Heap is the most efficient implementation for priority queue operations.


📌 APPLICATIONS
  • CPU scheduling
  • Printer jobs
  • Network packet handling
  • Emergency management systems
  • Graph algorithms like Dijkstra and Prim

📌 ADVANTAGES
  • Important tasks handled first
  • Useful for real-time systems
  • Efficient scheduling
📌 DISADVANTAGES
  • More complex than simple queue
  • Deletion may be costly in array implementation

6. Write an Algorithm to Convert Infix Expression to Postfix and Evaluate It.

🔷 INFIX TO POSTFIX CONVERSION

The unit notes on stack specifically cover expression conversion and postfix evaluation using stack. This is one of the standard applications of stack.


📌 INFIX

Operator appears between operands.

A + B * C

📌 POSTFIX

Operator appears after operands.

ABC*+


📌 ALGORITHM FOR INFIX TO POSTFIX
  1. Scan expression from left to right.
  2. If the symbol is an operand, append it to output.
  3. If symbol is (, push it onto stack.
  4. If symbol is an operator:
    pop operators with higher or equal precedence
    push current operator
  5. If symbol is ), pop until ( appears.
  6. After input ends, pop all remaining operators.
🔹 PSEUDOCODE

while(symbol != end)
{
    if operand
        output = output + symbol;
    else if symbol == '('
        push(symbol);
    else if operator
    {
        while(stack not empty and precedence(top) >= precedence(symbol))
            output = output + pop();
        push(symbol);
    }
    else if symbol == ')'
    {
        while(top != '(')
            output = output + pop();
        pop(); // remove '('
    }
}
while(stack not empty)
    output = output + pop();

📌 EXAMPLE

Convert:
3 * 3 / (4 - 1) + 6 * 2

Postfix:
33*41-/62*+

This is exactly the style of postfix conversion shown in the stack unit notes.


📌 POSTFIX EVALUATION ALGORITHM
  1. Scan postfix expression left to right.
  2. If operand, push it.
  3. If operator, pop two operands.
  4. Apply operator.
  5. Push result back.
  6. Final stack value is answer.
🔹 PSEUDOCODE

for each token in postfix
{
    if operand
        push(token);
    else
    {
        op2 = pop();
        op1 = pop();
        result = op1 operator op2;
        push(result);
    }
}
answer = pop();

📌 EXAMPLE

Postfix:
2 3 1 * + 9 -

Steps:

Push 2

Push 3

Push 1

* → 3 × 1 = 3

+ → 2 + 3 = 5

Push 9

- → 5 - 9 = -4

Final answer:
-4


7. Explain Tower of Hanoi using Stack and Recursion.

🔷 TOWER OF HANOI

Tower of Hanoi is a classic recursive problem and is also a standard stack application listed in your course outline. It involves moving disks from one peg to another using an auxiliary peg, following the rule that a larger disk can never be placed on a smaller disk.


📌 RULES
  1. Only one disk can be moved at a time.
  2. A larger disk cannot be placed on a smaller disk.
  3. Only top disk of any peg can be moved.

📌 PROBLEM SETUP

Three pegs:

  • Source
  • Auxiliary
  • Destination

Example for 3 disks:

Source: A
Auxiliary: B
Destination: C

Initial:

A: [3,2,1]
B: [ ]
C: [ ]

Goal:

A: [ ]
B: [ ]
C: [3,2,1]


📌 RECURSIVE IDEA

For n disks:

  1. Move n-1 disks from source to auxiliary.
  2. Move largest disk to destination.
  3. Move n-1 disks from auxiliary to destination.

This is a classic recursion pattern where the problem is reduced into smaller subproblems.


📌 RECURSIVE ALGORITHM

TowerOfHanoi(n, source, auxiliary, destination)
{
    if(n == 1)
    {
        move disk from source to destination;
        return;
    }

    TowerOfHanoi(n-1, source, destination, auxiliary);
    move disk from source to destination;
    TowerOfHanoi(n-1, auxiliary, source, destination);
}

📌 EXAMPLE FOR 3 DISKS

Moves:

1. A → C

2. A → B

3. C → B

4. A → C

5. B → A

6. B → C

7. A → C

Total moves:
2^n - 1 = 2^3 - 1 = 7


📌 STACK CONNECTION

Recursion uses the call stack internally. Every recursive call is stored on the stack until the base case is reached. So Tower of Hanoi can be viewed both as a recursion problem and a stack-based problem.


📌 APPLICATIONS
  • Disk movement simulation
  • Recursion understanding
  • Algorithmic problem solving
  • Stack behavior demonstration

📊 KEY POINTS
TopicImportant Point
StackLIFO structure
QueueFIFO structure
Circular QueueReuses empty spaces
Priority QueueHigher priority first
Infix to PostfixStack-based conversion
Tower of HanoiRecursive + stack-based