DS Unit-3
DS Unit-3
Example:
So, when you traverse the tree in pre-order, you start from the root (A), then
visit the left subtree (B), and finally the right subtree (C). Within each subtree,
the process is repeated: visit the root, traverse left, traverse right.
Q-What is stack? Explain three different applications of stack.
Stack:
A stack is a data structure that follows the Last In, First Out (LIFO) principle. It
means that the last element added to the stack is the first one to be removed.
The operations on a stack are typically limited to adding an item to the top of
the stack (push) and removing the top item (pop). Additional operations may
include checking the top item without removing it (peek) and checking if the
stack is empty.
Three Different Applications of Stacks:
1. Function Call Management (Call Stack):
- Stacks are commonly used in programming languages to manage function
calls and returns. When a function is called, its local variables and the address
to return are pushed onto the call stack. When the function execution is
completed, it is popped from the stack, and control returns to the calling
function. This ensures that the program execution follows the order in which
functions are called and returns.
2. Expression Evaluation (Expression Stack):
- Stacks are used to evaluate expressions, both arithmetic and logical. During
the parsing of an expression, a stack can be employed to keep track of
operators and operands. For example, in converting infix expressions to
postfix (as mentioned in a previous response), a stack helps in maintaining the
correct order of operators and operands.
3. Undo Mechanism in Applications:
- Stacks are employed in applications that require an undo mechanism. Each
user action that modifies the application state (such as typing, drawing, etc.) is
pushed onto a stack. If the user decides to undo an action, the application can
pop the most recent action from the stack, effectively reverting the state to
the previous one. This is a convenient way to implement a step-by-step undo
feature.
Q-How stack is implemented using array? Write an algorithm of its basic
operations?
A stack can be implemented using an array by considering the array as a fixed-
size container with a designated top index. The key components of this
implementation include:
1. Array: Allocate an array with a fixed size to store the elements of the stack.
The array serves as the underlying data structure to hold the elements.
2. Top: Maintain a variable, often called "top," to keep track of the index of
the top element in the stack. Initially, the stack is empty, so the top is typically
set to -1.
Operations:
- Push Operation:
- Increment the top index.
- Place the new element at the index specified by the top.
- Pop Operation:
- Retrieve the element at the top index.
- Decrement the top index.
- Peek Operation:
- Retrieve the element at the top index without removing it.
b) Insert Operation:
Algorithm:
1. Create a new node with the given data.
2. If inserting at the beginning:
- Set the next pointer of the new node to the current head.
- Set the prev pointer of the current head to the new node.
- Update the head to the new node.
3. If inserting at the end:
- Traverse the list to find the last node.
- Set the next pointer of the last node to the new node.
- Set the prev pointer of the new node to the last node.
4. If inserting at a specific position:
- Traverse the list to find the node before the desired position.
- Adjust the next and prev pointers to include the new node.
c) Delete Operation:
Algorithm:
1. If deleting the first node:
- Set the next node as the new head.
- Set the prev pointer of the new head to `NULL`.
2. If deleting the last node:
- Traverse the list to find the last node.
- Set the prev pointer of the last node to `NULL`.
3. If deleting a node in the middle:
- Traverse the list to find the node to be deleted.
- Adjust the next and prev pointers of the nodes before and after the node to
be deleted.
Q-What is queue? Discuss its various applications.
Queue:
A queue is a linear data structure that follows the First In, First Out (FIFO)
principle, meaning that the element that is added first is the one that gets
removed first. In a queue, elements are added at the rear (enqueue) and
removed from the front (dequeue). This ordering makes queues suitable for
modelling real-world scenarios where items are processed in a sequential
manner.
A circular queue is a data structure that follows the First In, First Out (FIFO)
principle, similar to a regular queue, with the added feature that the rear and
front pointers are connected in a circular manner. This circular arrangement
allows for efficient utilization of space, and when the rear pointer reaches the
end of the array, it wraps around to the beginning.
Circular Queue Insert Algorithm:
Input:
- `queue`: Circular queue implemented as an array.
- `front`: Front pointer of the circular queue.
- `rear`: Rear pointer of the circular queue.
- `MAX_SIZE`: Maximum size of the circular queue.
- `element`: Element to be inserted.
Procedure:
1. Check if the circular queue is full.
- If `(rear + 1) % MAX_SIZE == front`, the queue is full.
- Print "Queue Overflow."
- Return without inserting the element.
2. If the queue is not full:
- Increment `rear` by 1.
- Set `queue[rear]` to the new element.
- If `front` is -1 (indicating an empty queue), set `front` to 0.