This chapter examines the list data structure, including static and dynamic implementations. It covers list operations, array and linked list representations, and the advantages of dynamic lists over static lists.
Explain array representation of list? How does it differ from dynamic list?
Solution
Arrays are represented as a collection of buckets where each bucket stores one element. These buckets are indexed from '0' to 'n-1', where n is the size of that particular array. For example, an array with size 10 will have buckets indexed from 0 to 9.
Arrays have a fixed size set during creation. Lists are dynamic and can change in size during runtime. All elements in an array must be of the same data type. Lists can accommodate elements of different data types.
How do you implement array to represent queue as list?
Solution
Implementing a queue using an array involves using the array to store the elements of the queue and maintaining indices for the front and rear to track the beginning and end of the queue.
Operation in queue
Enqueue(insert): Add new elements to the end of the queue.
Dequeue(remove): Remove the front element by shifting all remaining elements to the left.
Peek: Get the front element without removing it.
isEmpty: Check if the queue is empty.
isFull (for fixed-size arrays): Check if the queue is full.
Differentiate static and dynamic implementation of list with suitable example.
Solution
S.N
Static List
Dynamic List
1
Static list ia an array implementation.
Dynamic list is linked list implementation
2
Size of list is fixed.
size of list is dynamic.
3
Programming implementation is easy.
Programming implementation is complex.
4
It takes less time for computation.
It takes more time for computation.
5
It is suitable if less nodes are available in a list (for smaller list).
It is suitable if larger nodes are available in a list (for larger list).
6
example: HubSpot (managing event attendees)
example: single linked list , doubly Linked list etc.
Q.N. 5 Discuss the merits and demerits of contiguous list and linked list. Write algorithms to insert and delete a node after a node in a singly linked list.
Demerits of Contiguous List:
1.Extra Memory for Pointers.
2.Inefficient excess.
3.complex implementation
Algorithm for the insertion of new node at the beginning in SLL: Step 1 - Create a newNode with given value. Step 2 - Check whether list is Empty (head == NULL) Step 3 - If it is Empty then, set newNode→next = NULL and head = newNode. Step 4 - If it is Not Empty then, set newNode→next = head and head = newNode.
Algorithm for the deletion of new node at the beginning in SLL:
Step 1 - Check whether the list is Empty (head == NULL) Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminates the function. Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head. Step 4 - Check whether the list is having only one node (temp → next == NULL) Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions) Step 6 - If it is FALSE then set head = temp → next, and delete temp.
Q.N. 6 Explain array implementation of list with suitable example. Consider an array to represent the list. Write an algorithm to insert an element at i^th position in the list.
Solution
Arrays are represented as a collection of buckets where each bucket stores one element. These buckets are indexed from '0' to 'n-1', where n is the size of that particular array. For example, an array with size 10 will have buckets indexed from 0 to 9.
Algorithm to insert an element at ith position:
Step 1- Create a newNode with the given value. Step 2- Check whether list is Empty (head == NULL) Step 3- If it is Empty then, set newNode → next = NULL and head = newNode. Step 4- If it is Not Empty then, define a node pointer temp and initialize with head. Step 5- Keep moving the temp to its next node until it reaches the node after which we want to insert the newNode (until temp1 → data is equal to location, here location is the node value after which we want to insert the newNode). Step 6- Every time check whether temp is reached to the last node or not. If it is reached to the last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise, move the temp to the next node. Step 7- Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'