Lecture 4 - Link List - Rules and List Implementation
Lecture 4 - Link List - Rules and List Implementation
As per the above illustration, following are the important points to be considered.
1. Linked List contains a link element called first.
2. Each link carries a data field(s) and a link field called next.
3. Each link is linked with its next link using its next link.
4. Last link carries a link as null to mark the end of the list.
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays
have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on
the number of elements in advance. Also, generally, the allocated
memory is equal to the upper limit irrespective of the usage.
2) Inserting a new element in an array of elements is expensive
because the room has to be created for the new elements and to
create room existing elements have to be shifted.
Why Linked List?
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks:
Instead of using array, we can also use linked list to implement stack.
Linked list allocates the memory dynamically. However, time complexity in
both the scenario is same for all the operations i.e. push, pop and peek.
In linked list implementation of stack, the nodes are maintained
non-contiguously in the memory. Each node contains a pointer to its
immediate successor node in the stack. Stack is said to be overflown if the
space left in the memory heap is not enough to create a node.
Linked list implementation of stack
In the picture above we have a linked list, containing 4 nodes, each node
has some data(A, B, C and D) and a pointer which stores the location of
the next node.
You must be wondering why we need to store the location of the next
node. Well, because the memory locations allocated to these nodes are
not contiguous hence each node should know where the next node is
stored
As the node is a combination of multiple information, hence we will be
defining a class for Node which will have a variable to store data and
another variable to store the pointer
Adding a node to the stack (Push
operation)
Adding a node to the stack is referred to as push operation. Pushing an element to a
stack in linked list implementation is different from that of an array implementation. In
order to push an element onto the stack, the following steps are involved.
1. Create a node first and allocate memory to it.
2. If the list is empty then the item is to be pushed as the start node of the list. This
includes assigning value to the data part of the node and assign null to the address
part of the node.
3. If there are some nodes in the list already, then we have to add the new element in
the beginning of the list (to not violate the property of the stack). For this purpose,
assign the address of the starting element to the address field of the new node and
make the new node, the starting node of the list.
4.
Adding a node to the stack (Push
operation)
The element in such a linked list can be inserted in 2 ways:
● Insertion at beginning of the list.
Start with the head and access each node until you reach null. Do
not change the head reference.
Displaying all the nodes of a stack needs traversing all the nodes of
the linked list organized in the form of stack. For this purpose, we
need to follow the following steps.
•Copy the head pointer into a temporary pointer.
•Move the temporary pointer through all the nodes of the list and print the
value field attached to every node.
References
programiz.com linked list types retrieved from
https://www.programiz.com/dsa/linked-list-types
geeksforgeeks.com Data Structure- Linked List
https://www.geeksforgeeks.org/data-structures/linked-list/
studytonight. com Data Structures linear linked list
https://www.studytonight.com/data-structures/linear-linked-list