0% found this document useful (0 votes)
30 views9 pages

DS Unit-3

Data structure notes created by me. These are shorts notes. You can prepare from these notes for you exam and write it as it is. These notes were created for MDU students but you can use it if helpful.

Uploaded by

shitijalok2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views9 pages

DS Unit-3

Data structure notes created by me. These are shorts notes. You can prepare from these notes for you exam and write it as it is. These notes were created for MDU students but you can use it if helpful.

Uploaded by

shitijalok2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Q.

Describe a method to convert an infix expression in to a postfix expression


in to a postfix expression with the help of a suitable example.
Example:
Consider the infix expression: `A + B * (C - D) / E`
Conversion Steps:
1. Initialize an empty stack for operators and an empty list for the postfix
expression.
2. Start scanning the infix expression from left to right.
3. For each element in the infix expression:
- If it is an operand, add it to the postfix expression list.
- If it is an operator, pop operators from the stack and add them to the
postfix expression until the stack is empty or the top operator has lower
precedence than the current operator. Then, push the current operator onto
the stack.
- If it is an open parenthesis, push it onto the stack.
- If it is a close parenthesis, pop operators from the stack and add them to
the postfix expression until an open parenthesis is encountered. Pop and
discard the open parenthesis.
4. After scanning the entire infix expression, pop any remaining operators
from the stack and add them to the postfix expression.
Conversion Example:
Consider the infix expression: `A + B * (C - D) / E`
- Step 1: Empty stack, empty postfix expression.
- Step 2: Start scanning `A`.
- Operand A, add to postfix.
- Step 3: `+`
- Push onto stack.
- Step 4: `B`
- Operand B, add to postfix.
- Step 5: `*`
- Push onto stack.
- Step 6: `(`
- Push onto stack.
- Step 7: `C`
- Operand C, add to postfix.
- Step 8: `-`
- Push onto stack.
- Step 9: `D`
- Operand D, add to postfix.
- Step 10: `)`
- Pop operators from the stack and add to postfix until `(` is encountered.
Discard `(`.
- Step 11: `/`
- Push onto stack.
- Step 12: `E`
- Operand E, add to postfix.
- Step 13: End of expression.
- Pop any remaining operators from the stack and add to postfix.
Resultant Postfix Expression:
`A B C D - * E / +`
So, the infix expression `A + B * (C - D) / E` is converted to the postfix
expression `A B C D - * E / +`.
Q- What do you mean by pre order traversal of a tree.
Pre-order traversal is one of the methods used to traverse or visit nodes in a
tree data structure. In a pre-order traversal, you start from the root node and
visit the nodes in the following order:
1. Visit the root node.
2. Traverse the left subtree in pre-order.
3. Traverse the right subtree in pre-order.
The general algorithm for pre-order traversal is recursive. Here's a concise
explanation:
1. Visit the current node.
2. Recursively perform a pre-order traversal on the left subtree.
3. Recursively perform a pre-order traversal on the right subtree.
In terms of node processing, the order is Root -> Left -> Right.

Example:

Consider the following binary tree:


```
A
/\
B C
/\
D E
```

Pre-order traversal: `A -> B -> D -> E -> C`

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.

Checking for Stack Overflow and Underflow:


- Stack Overflow:
- Occurs when attempting to push an element onto a full stack (i.e., when the
top index is equal to the size of the array minus one).
- Stack Underflow:
- Occurs when attempting to pop an element from an empty stack (i.e., when
the top index is -1).
Advantages:
- Simple and straightforward implementation.
- Efficient access to the top element.
Disadvantages:
- Fixed size: The size of the stack is fixed during initialization, and it cannot be
dynamically resized.
- Memory usage: If the stack size is initially set to be too large, it may lead to
inefficient memory usage.

Stack Operations Algorithm:


Initialization:
1. Declare an array `stack_array` and initialize `top` to -1.
Push Operation:
1. Check if `top < MAX_SIZE - 1`.
- If true, increment `top` and set `stack_array[top]` to the new element.
- If false, print "Stack Overflow."
Pop Operation:
1. Check if `top >= 0`.
- If true, retrieve `stack_array[top]`, decrement `top`, and return the
element.
- If false, print "Stack Underflow" and return null.
Peek Operation:
1. Check if `top >= 0`.
- If true, return `stack_array[top]`.
- If false, print "Stack is empty" and return null.
Is Empty Operation:
1. Return `top < 0`.
Is Full Operation:
1. Return `top == MAX_SIZE - 1`.
Q-What is a doubly linked list? explain the following operations on a double
linked list:
a) Create
b) Insert
c) Delete

Doubly Linked List:


A doubly linked list is a type of linked list in which each node contains a data
element and two pointers, one pointing to the next node in the sequence
(next pointer), and another pointing to the previous node (prev pointer). This
structure allows for traversal in both forward and backward directions.

Operations on a Doubly Linked List:


a) Create Operation:
Algorithm:
1. Initialize an empty doubly linked list.
2. Create a new node with the given data.
3. Set the next and prev pointers of the node to `NULL` (for the first node).
4. The head of the doubly linked list is set to the newly created node.

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.

Various Applications of Queues:


1. Task Scheduling in Operating Systems:
- Operating systems often use queues to manage processes and tasks. The
scheduler maintains a queue of processes in the ready state, and they are
executed in the order they arrive.
2. Print Queue in Print Spoolers:
- In a computer system, when multiple print jobs are sent to a printer, they
are placed in a print queue. The printer processes these jobs in the order they
are received.
3. Breadth-First Search in Graphs:
- Queues are commonly used in graph traversal algorithms, such as breadth-
first search (BFS). BFS explores a graph level by level, visiting all neighbors of a
node before moving on to the next level.
4. Handling of Requests in Web Servers:
- Web servers use queues to manage incoming requests. The server
processes requests in the order they are received, ensuring fair access to
resources.
5. Buffer in Networking:
- Queues are used in computer networks to manage the flow of data
packets. Routers and switches often have queues to store packets temporarily
before forwarding them.
6. Task Management in Multitasking Systems:
- Queues help manage multiple tasks in a multitasking environment. Tasks
waiting for CPU time are placed in a queue and are executed when the CPU
becomes available.
7. Order Processing in E-commerce:
- In e-commerce systems, a queue can be used to manage orders. Orders are
processed in the order they are received, ensuring a fair and organized
system.
8. Print Job Management in Printers:
- Printers use queues to manage print jobs. The print queue ensures that
print jobs are processed in the order they are sent to the printer.
9. Call Center Systems:
- Call centers use queues to manage incoming calls. Calls are placed in a
queue and are answered by agents in the order they are received.
10. Simulation and Modeling:
- Queues are often used in simulations to model systems with waiting lines,
such as banks, supermarkets, or transportation systems.
Q-Write an algorithm for insert and delete an element to circular queue using
arrays?
Circular Queue:

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.

# Circular Queue Delete 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.
Output:
- The deleted element.
Procedure:
1. Check if the circular queue is empty.
- If `front == -1`, the queue is empty.
- Print "Queue Underflow."
- Return null or a specific value indicating underflow.
2. If the queue is not empty:
- Retrieve the element at `queue[front]`.
- If `front` is equal to `rear` (last element in the queue), set both `front` and
`rear` to -1 (indicating an empty queue).
- Otherwise, increment `front` by 1.
- Return the retrieved element.

You might also like