Data Structures Revision Notes-pt3
Data Structures Revision Notes-pt3
Implementation:
c
#include <stdio.h>
#include <stdlib.h> // For malloc
Show Image
2. Pop Operation:
Check if stack is empty (top == NULL)
Show Image
3. Peek Operation:
Check if stack is empty (top == NULL)
If not empty, return data of top node without removing it
2. Q: What are the advantages of implementing a stack using linked list over arrays? A: Dynamic
size (no fixed capacity), no overflow condition, better memory utilization, and operations remain O(1)
time complexity.
3. Q: What are the basic operations of a stack using linked list? A: Push (insert at top), Pop (remove
from top), Peek/Top (view top element without removing), and IsEmpty (check if stack is empty).
4. Q: What is the time complexity of Push and Pop operations in a stack implemented using
linked list? A: Both operations have O(1) time complexity as they only involve manipulating the top
pointer.
5. Q: How is memory allocated for a stack implemented using linked list? A: Memory is
dynamically allocated for each node as elements are pushed onto the stack, using malloc() or similar
functions.
6. Q: What happens when Pop is called on an empty stack? A: It results in a "Stack Underflow"
condition, which should be handled by returning an error value or message.
7. Q: Where is the top element located in a stack implemented using linked list? A: The top
element is the node pointed to by the 'top' pointer, which is the head of the linked list.
8. Q: Can a stack implemented using linked list ever be full? A: Theoretically no, unless the system
runs out of memory for allocating new nodes.
Implementation:
c
#include <stdio.h>
#include <stdlib.h> // For malloc
If queue is empty, set both front and rear to this new node
Otherwise, set rear's next to point to new node and update rear
Show Image
2. Dequeue Operation:
Check if queue is empty (front == NULL)
If not empty, store front node in temp
Show Image
4. Q: What is the time complexity of enqueue and dequeue operations in a queue implemented
using linked list? A: Both operations have O(1) time complexity as they only involve manipulating the
front and rear pointers.
5. Q: How does queue using linked list overcome the limitations of array-based queue
implementation? A: It eliminates the need for circular implementation, has no fixed size limitation,
and utilizes memory efficiently.
6. Q: What happens to front and rear pointers when the last element is dequeued? A: Both front
and rear pointers are set to NULL, indicating an empty queue.
7. Q: Can a queue implemented using linked list ever be full? A: Theoretically no, unless the system
runs out of memory for allocating new nodes.
8. Q: What is the special case to handle during the dequeue operation? A: When dequeuing the last
element, we need to set both front and rear pointers to NULL to properly indicate an empty queue.
Implementation:
c
#include <stdio.h>
#include <stdlib.h> // For malloc
3. Update the head pointer to point to the last node: head = prev
Show Image
Step-by-Step Example:
Given a linked list: 10 -> 20 -> 30 -> NULL
Step 1:
prev = NULL
current = 10
next = 20
10's next pointer now points to NULL
Step 2:
prev = 10
current = 20
next = 30
20's next pointer now points to 10
Step 3:
prev = 20
current = 30
next = NULL
30's next pointer now points to 20
Step 4:
prev = 30
current = NULL
head = 30
2. Q: What is the time complexity of reversing a linked list? A: O(n), where n is the number of nodes
in the linked list, as we need to traverse each node exactly once.
3. Q: What is the space complexity of the iterative approach to reverse a linked list? A: O(1), as we
only use a fixed number of pointers regardless of the list size.
4. Q: Can the reversal of a linked list be implemented recursively? A: Yes, a linked list can be
reversed recursively, though it has O(n) space complexity due to the recursion stack.
5. Q: What are the edge cases to consider when reversing a linked list? A: Empty list (head = NULL)
and single-node list (head->next = NULL) - in both cases, no reversal is needed.
6. Q: How many pointers are needed to reverse a linked list iteratively? A: Three pointers: previous,
current, and next.
7. Q: After reversing a linked list, which node becomes the new head? A: The last node of the
original list becomes the new head.
8. Q: How does reversing a linked list affect its order? A: The elements appear in reverse order - the
first element becomes the last, the second becomes the second-last, and so on.
Trees
Height: The length of the longest path from the root to a leaf
Binary Tree
A tree in which each node has at most two children
5. Degenerate (or Pathological) Tree: Every parent node has only one child
Show Image
Show Image
The left subtree of a node contains only nodes with keys less than the node's key
The right subtree of a node contains only nodes with keys greater than the node's key
Both the left and right subtrees must also be binary search trees
Show Image
return current;
}
3. Q: What are the common traversal methods for a binary tree? A: The common traversal methods
are: in-order (left-root-right), pre-order (root-left-right), and post-order (left-right-root).
4. Q: Why is in-order traversal important for binary search trees? A: In-order traversal of a binary
search tree visits nodes in ascending order, which is useful for obtaining sorted data.
5. Q: What is the maximum number of nodes at level 'L' of a binary tree? A: 2^L (where the root is
at level 0).
6. Q: What is a balanced binary tree? A: A binary tree in which the height of the left and right subtrees
of any node differ by not more than 1.
7. Q: What is the height of a perfect binary tree with 'n' nodes? A: log₂(n+1) - 1
8. Q: What is the time complexity of searching, insertion and deletion in a binary search tree? A:
Average case: O(log n), Worst case (unbalanced tree): O(n)
9. Q: What is a leaf node in a tree? A: A node that has no children.
10. Q: How do you find the height of a binary tree? A: The height is the length of the longest path from
the root to any leaf. It can be calculated recursively as max(height(left subtree), height(right subtree)) +
1.
11. Q: What is a complete binary tree? A: A binary tree in which all levels are completely filled except
possibly the last level, which is filled from left to right.
12. Q: What are the applications of binary trees? A: Binary trees are used in expression evaluation,
Huffman coding for data compression, priority queues, and as a basis for other data structures like
heaps and binary search trees.
Stack Applications
Expression Notations:
5 If lower/equal precedence, pop stack until higher precedence found, then push
Example:
Converting A + B * C to postfix:
Example:
Evaluating 6 2 3 + - 3 8 2 / + * 2 ^
// Global variables
char stack[MAX]; // Stack to store opening brackets
int top = -1; // Index of top element
How it Works:
Example:
Expression: { [ ( ) ] }
1. Push {, [, (
2. Pop ( for )
3. Pop [ for ]
4. Pop { for }
5. Stack is empty - balanced!
Tree Applications
1. Expression Tree
An expression tree is a binary tree that represents an arithmetic expression:
Show Image
Benefits:
2. Huffman Encoding