Unit 3: Introduction to Linked Lists - Subjective Questions
INT322 — Computing System And Technologies • Practice Questions with Detailed Answers
20 questions
Define a linked list. Explain the structure of a node in a singly linked list and the role of the head pointer.
Linked list: A linked list is a dynamic linear data structure made up of nodes that need not be stored in contiguous memory locations.
Each node of a singly linked list contains:
- Data field: Stores the value or information.
- Link field: Stores a reference or pointer to the next node.
The head pointer stores the address of the first node. If the list is empty, head is NULL. The link field of the final node is also NULL, indicating the end of the list.
A node can be represented conceptually as [data | next].
Describe how a singly linked list can be implemented using a self-referential structure. Include the steps required to create the first node.
A singly linked list is implemented using a self-referential structure, which contains a pointer to another structure of the same type.
Example in C:
struct Node {
int data;
struct Node *next;
};
Steps to create the first node:
- Declare
headand initialize it toNULL. - Allocate memory for a new node dynamically.
- Store the required value in its
datafield. - Set its
nextfield toNULL. - Assign the address of the new node to
head.
After these operations, the new node is both the first and last node of the list.
Explain the traversal of a singly linked list. Write an algorithm and state its time and space complexity.
Traversal means visiting every node of the linked list, usually from the first node to the last node.
Algorithm:
- Set a temporary pointer
current = head. - Repeat while
current != NULL:- Process or display
current.data. - Move to the next node using
current = current.next.
- Process or display
- Stop when
currentbecomesNULL.
Pseudocode:
TRAVERSE(head)
current = head
while current != NULL
print current.data
current = current.next
For a list containing nodes:
- Time complexity:
- Auxiliary space complexity:
The temporary pointer should be used so that the original head pointer is not changed.
Describe the procedure for inserting a new node at the beginning of a singly linked list. Illustrate the pointer changes.
To insert a node at the beginning:
- Allocate memory for
newNode. - Store the new value in
newNode.data. - Set
newNode.next = head. - Set
head = newNode.
Pointer transformation:
- Before insertion:
head -> A -> B -> NULL - After inserting
N:head -> N -> A -> B -> NULL
The same steps work for an empty list because the old value of head is NULL.
- Time complexity:
- No traversal is required.
Explain how a node is inserted at the end of a singly linked list. Discuss the cases of an empty and a non-empty list.
To insert newNode at the end, first initialize newNode.next to NULL.
Case 1: Empty list
- If
head == NULL, assignhead = newNode. - The new node becomes the first and last node.
Case 2: Non-empty list
- Set
current = head. - Traverse until
current.next == NULL. - Set
current.next = newNode.
Pseudocode:
INSERT_END(head, value)
create newNode
newNode.data = value
newNode.next = NULL
if head == NULL
head = newNode
else
current = head
while current.next != NULL
current = current.next
current.next = newNode
Without a tail pointer, the time complexity is . With a maintained tail pointer, insertion at the end can be performed in time.
Describe an algorithm to insert a node at a specified position in a singly linked list. Explain how invalid positions should be handled.
Assume that positions start from .
Algorithm:
- Create
newNodeand store the value in it. - If the position is :
- Set
newNode.next = head. - Set
head = newNode.
- Set
- Otherwise, move a pointer to the node at position .
- If that predecessor does not exist, report an invalid position and release the newly allocated node.
- Set
newNode.next = previous.next. - Set
previous.next = newNode.
For example, inserting X at position changes:
A -> B -> C -> NULL
to:
A -> B -> X -> C -> NULL
The worst-case time complexity is , while the pointer updates themselves take time.
Explain how the first node is deleted from a singly linked list. Why must the removed node be released?
Deletion from the beginning is performed as follows:
- Check whether
head == NULL. - If true, report underflow because there is no node to delete.
- Otherwise, store the first node in a temporary pointer:
temp = head. - Move the head pointer forward:
head = head.next. - Release or deallocate
temp.
Example:
- Before:
head -> A -> B -> C -> NULL - After:
head -> B -> C -> NULL
The removed node must be released so that dynamically allocated memory does not become inaccessible and cause a memory leak.
The operation takes time.
Describe the deletion of the last node from a singly linked list. Explain the special cases that must be considered.
Three cases must be considered:
1. Empty list:
- If
head == NULL, deletion is impossible and an underflow condition is reported.
2. List with one node:
- Store
headin a temporary pointer. - Set
head = NULL. - Release the temporary node.
3. List with multiple nodes:
- Traverse with
currentuntilcurrent.next.next == NULL. - Store the final node in
temp = current.next. - Set
current.next = NULL. - Release
temp.
The traversal makes the time complexity . If both tail and predecessor information are not available, a singly linked list cannot directly move backward from the last node.
Develop an algorithm to delete the first node containing a specified key from a singly linked list. Explain all possible outcomes.
The operation must handle an empty list, a match at the head, a match elsewhere, and an absent key.
Algorithm:
- If
head == NULL, report underflow. - If
head.data == key:- Store
headintemp. - Set
head = head.next. - Release
temp.
- Store
- Otherwise, set
current = head. - Move forward while
current.next != NULLandcurrent.next.data != key. - If
current.next == NULL, report that the key was not found. - Otherwise:
- Set
temp = current.next. - Set
current.next = temp.next. - Release
temp.
- Set
Only the first occurrence is deleted. The worst-case time complexity is and the auxiliary space complexity is .
What are linked list underflow and overflow conditions? Explain when each condition occurs and how it should be handled.
Underflow occurs when an operation attempts to delete or access a node from an empty linked list.
- Condition:
head == NULL - Example: Attempting to delete the first node when no node exists.
- Handling: Check whether the list is empty before deletion and display or return an appropriate error.
Overflow occurs when a new node cannot be created because memory allocation fails.
- Condition: The memory allocator returns
NULLor an equivalent failure result. - Unlike a fixed-size array, a linked list has no predetermined capacity; its practical limit is available memory.
- Handling: Verify allocation success before accessing the node and report failure if memory is unavailable.
Thus, underflow concerns removing from an empty structure, whereas overflow concerns failure to obtain memory for insertion.
Compare a singly linked list with an array in terms of memory organization, size, access, insertion, and deletion.
| Feature | Array | Singly linked list |
|---|---|---|
| Memory organization | Usually contiguous | Nodes may be non-contiguous |
| Size | Commonly fixed or resized as a block | Grows and shrinks dynamically |
| Direct access | Supports indexed access in | Sequential access takes |
| Insertion at beginning | Usually due to shifting | |
| Deletion at beginning | Usually due to shifting | |
| Extra memory | No link field per element | Requires one next pointer per node |
| Cache performance | Generally better | Often poorer due to scattered nodes |
A linked list is suitable when frequent insertions and deletions are needed. An array is preferable when fast indexed access and compact storage are more important.
Define a tree as a data structure. How does a tree differ conceptually from a linear data structure such as a linked list?
A tree is a non-linear, hierarchical data structure consisting of nodes connected by edges. A non-empty tree has a distinguished node called the root, and the remaining nodes form disjoint subtrees below it.
Key differences are:
- A linked list represents a linear sequence, whereas a tree represents a hierarchy.
- Except for the root, every node in a tree has one parent in the usual rooted-tree model.
- A tree node may have multiple children, while a singly linked-list node normally has only one next link.
- There is a unique path from the root to each node in a tree.
- Trees are useful for representing file systems, organization charts, expression structures, and search structures.
A tree with nodes has edges, provided it is connected and contains no cycles.
Explain the tree terms root, edge, parent, child, siblings, and leaf with the help of a suitable example.
Consider a tree in which A has children B and C, while B has children D and E.
- Root: The topmost node with no parent. Here,
Ais the root. - Edge: A connection between two nodes. Examples include
A-BandB-D. - Parent: A node directly above another node.
Bis the parent ofDandE. - Child: A node directly below another node.
BandCare children ofA. - Siblings: Nodes having the same parent.
BandCare siblings;DandEare also siblings. - Leaf: A node with no children. In this example,
C,D, andEare leaves.
These terms describe the immediate structural relationships among nodes in a rooted tree.
Define degree, level, depth, and height in a tree. Clearly distinguish the height of a node from the height of a tree.
- Degree of a node: The number of children of that node. A leaf has degree .
- Degree of a tree: The maximum degree of any node in the tree.
- Depth of a node: The number of edges from the root to that node. The root has depth .
- Level of a node: Often defined as
depth + 1, so the root is at level . Some texts start levels at , so the convention must be stated. - Height of a node: The number of edges on the longest downward path from that node to a leaf. A leaf has height .
- Height of a tree: The height of its root, equal to the maximum depth of any node.
Depth measures distance from the root downward to a node, while height measures the longest distance from a node downward to a leaf.
What are a path and a subtree in a tree? Explain how path length is measured and how a subtree is formed.
A path is a sequence of nodes in which each consecutive pair is connected by an edge. For example, if A is connected to B and B to D, then A -> B -> D is a path.
- The path length is the number of edges in the path.
- Therefore, the path
A -> B -> Dhas length . - In a tree, there is exactly one simple path between any two nodes.
A subtree consists of a selected node together with all of its descendants and the edges connecting them. If B has descendants D and E, the subtree rooted at B contains B, D, and E.
Every node can be regarded as the root of a subtree. A leaf forms a subtree containing only itself.
Explain the concept of a binary tree. State important properties of binary trees and distinguish a binary tree from a general tree.
A binary tree is a tree in which each node has at most two children, specifically identified as the left child and right child.
Important properties, assuming the root is at level , include:
- Maximum nodes at level :
- Maximum nodes in a binary tree of height :
- Maximum number of leaf nodes at height :
- For nodes, the number of edges is
- In a linked representation with two child pointers per node, a binary tree with nodes has null child links.
A general tree may have any number of children per node. A binary tree permits no more than two, and the positions of left and right children are distinct even when only one child exists.
Define a binary search tree. Explain its ordering property and describe how searching, insertion, and deletion are conceptually performed.
A binary search tree (BST) is a binary tree satisfying the following ordering property for every node:
- All keys in the left subtree are smaller than the node's key.
- All keys in the right subtree are larger than the node's key.
- Each left and right subtree must also be a BST.
A consistent policy is required if duplicate keys are allowed.
Searching: Compare the key with the current node. Move left for a smaller key and right for a larger key.
Insertion: Follow the same comparisons until a null child position is found, then attach the new node there.
Deletion:
- A leaf can be removed directly.
- A node with one child is replaced by its child.
- A node with two children is commonly replaced by its inorder successor or predecessor, after which that replacement node is deleted.
These operations take time, where is the tree height. They are in a balanced BST but may degrade to in a skewed BST.
Explain preorder traversal of a binary tree. Give its recursive algorithm and determine the preorder sequence for a tree with root A, children B and C, children D and E under B, and right child F under C.
Preorder traversal follows the order:
- Visit the root.
- Traverse the left subtree.
- Traverse the right subtree.
Recursive algorithm:
PREORDER(node)
if node != NULL
visit node
PREORDER(node.left)
PREORDER(node.right)
For the given tree:
- Visit
Afirst. - Traverse the subtree rooted at
B:B, D, E. - Traverse the subtree rooted at
C:C, F.
Therefore, the preorder sequence is:
A, B, D, E, C, F
For nodes, traversal takes time. Recursive auxiliary space is , where is the tree height.
Explain inorder traversal of a binary tree. Give its recursive algorithm and state its special significance for a binary search tree.
Inorder traversal follows the order:
- Traverse the left subtree.
- Visit the root.
- Traverse the right subtree.
Recursive algorithm:
INORDER(node)
if node != NULL
INORDER(node.left)
visit node
INORDER(node.right)
For a tree with root A, children B and C, children D and E under B, and right child F under C, the inorder sequence is:
D, B, E, A, C, F
The special significance of inorder traversal is that it visits the keys of a binary search tree in sorted order, provided its duplicate-key policy preserves the required ordering.
Its time complexity is and its recursive auxiliary space is .
Explain postorder traversal and compare preorder, inorder, and postorder traversals. Mention one practical use of each traversal.
Postorder traversal visits nodes in this order:
- Traverse the left subtree.
- Traverse the right subtree.
- Visit the root.
Recursive algorithm:
POSTORDER(node)
if node != NULL
POSTORDER(node.left)
POSTORDER(node.right)
visit node
For a tree with root A, children B and C, children D and E under B, and right child F under C, the postorder sequence is D, E, B, F, C, A.
Comparison and uses:
- Preorder — Root, Left, Right: Useful for copying a tree or generating prefix expressions.
- Inorder — Left, Root, Right: Produces sorted keys when applied to a BST.
- Postorder — Left, Right, Root: Useful for deleting a tree because children are processed before their parent, and for generating postfix expressions.
Each traversal visits every node once, so its time complexity is . Recursive space usage is .
Define a linked list. Explain the structure of a node in a singly linked list and the role of the head pointer.
Linked list: A linked list is a dynamic linear data structure made up of nodes that need not be stored in contiguous memory locations.
Each node of a singly linked list contains:
- Data field: Stores the value or information.
- Link field: Stores a reference or pointer to the next node.
The head pointer stores the address of the first node. If the list is empty, head is NULL. The link field of the final node is also NULL, indicating the end of the list.
A node can be represented conceptually as [data | next].
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →