Unit 4
Unit 4
Linked List:
Data Structure: Non-contiguous
Memory Allocation: Typically allocated one by one to individual elements
Insertion/Deletion: Efficient
Access: Sequential
3. Realization of Linked List Using Dynamic Memory Management
Linked lists use dynamic memory allocation, allowing nodes to be created and
destroyed during runtime.
Functions like malloc in C or new in C++/Java are commonly used to allocate
memory for new nodes.
2. Stack ADT
View of stack
In Stack ADT Implementation instead of data being stored in each node, the
pointer to data is stored.
The program allocates memory for the data and address is passed to the stack
ADT.
The head node and the data nodes are encapsulated in the ADT. The calling
function can only see the pointer to the stack.
The stack head structure also contains a pointer to top and count of number of
entries currently in stack.
push() – Insert an element at one end of the stack called top.
pop() – Remove and return the element at the top of the stack, if it is not empty.
peek() – Return the element at the top of the stack without removing it, if the stack
is not empty.
size() – Return the number of elements in the stack.
isEmpty() – Return true if the stack is empty, otherwise return false.
isFull() – Return true if the stack is full, otherwise return false.
3. Queue ADT
View of Queue
The queue abstract data type (ADT) follows the basic design of the stack
abstract data type.
Each node contains a void pointer to the data and the link pointer to the next
element in the queue. The program’s responsibility is to allocate memory for
storing the data.
enqueue() – Insert an element at the end of the queue.
dequeue() – Remove and return the first element of the queue, if the queue is not
empty.
peek() – Return the element of the queue without removing it, if the queue is not
empty.
size() – Return the number of elements in the queue.
isEmpty() – Return true if the queue is empty, otherwise return false.
isFull() – Return true if the queue is full, otherwise return false.
To insert a new node at the front, we create a new node and point
its next reference to the current head of the linked list. Then, we update
the head to be this new node. This operation is efficient because it only
requires adjusting a few pointers.
Algorithm:
Make the first node of Linked List linked to the new node
Remove the head from the original first node of Linked List
Make the new node as the Head of the Linked List.
Inserting at the end involves traversing the entire list until we reach the last node.
We then set the last node’s next reference to point to the new node, making the new
node the last element in the list.
Algorithm:
Go to the last node of the Linked List
Change the next pointer of last node from NULL to the new node
Make the next pointer of new node as NULL to show the end of Linked List
Approach:
Initialize a pointer curr to traverse the list starting from head.
Loop through the list to find the node with data equal to key.
If not found then return from function.
Create a new node, say new_node initialized with the given data.
Make the next pointer of new_node as next of given node.
Update the next pointer of given node point to the new_node.
Delete:
o From Beginning: Remove the first node.
o From End: Remove the last node.
o At Position: Delete a node at a specified position.
Sort: Arrange nodes in ascending or descending order.
Concatenate: Link the end of one list to the beginning of another.
To represent a list of items there are certain assumptions about the node
structure.
Flag = 1 implies that down pointer exists
Flag = 0 implies that next pointer exists
Data means the item.
Down pointer is the address of node which is down of the current node
Next pointer is the address of node which is attached as the next node
Why Generalized Linked List? Generalized linked lists are used because
although the efficiency of polynomial operations using linked list is good
but still, the disadvantage is that the linked list is unable to use multiple
variable polynomial equation efficiently. It helps us to represent multi-
variable polynomial along with the list of elements.