Lecture 4 - Link List - Rules and List Implementation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Data structures and algorithms

LinkedList – LinkedList type and


implementation
What is Link List
A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations. The elements in a linked list are linked using pointers as shown in the below
image

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?

•For example, in a system, if we maintain a sorted list of IDs in an


array id[].
•id[] = [1000, 1010, 1050, 2000, 2040].
And if we want to insert a new ID 1005, then to maintain the sorted
order, we have to move all the elements after 1000 (excluding
1000).
Deletion is also expensive with arrays until unless some special
techniques are used. For example, to delete 1010 in id[], everything
after 1010 has to be moved.
Advantages over arrays

1) Dynamic size
2) Ease of insertion/deletion
Drawbacks:

1) Random access is not allowed. We have to access elements


sequentially starting from the first node. So we cannot do binary
search with linked lists efficiently with its default implementation.
2) Extra memory space for a pointer is required with each element
of the list.
3) Not cache friendly. Since array elements are contiguous
locations, there is locality of reference which is not there in case of
linked lists.
Representation:

A linked list is represented by a pointer to the first node of the linked


list. The first node is called the head. If the linked list is empty, then
the value of the head is NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
Types of Link List
Single Link List
It is the most common. Each node has data and a pointer to the next
node.
Types of Link List
Doubly Linked List
We add a pointer to the previous node in a doubly-linked list. Thus, we
can go in either direction: forward or backward.
Types of Link List
Circular Linked List
A circular linked list is a variation of a linked list in which the last
element is linked to the first element. This forms a circular loop.
Types of Link List
A circular linked list can be either singly linked or doubly linked.
for singly linked list, next pointer of last item points to the first item
In the doubly linked list, prev pointer of the first item points to the last item
as well
Linked list implementation of stack

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

The top most node in


the stack always
contains null in its
address field. Lets
discuss the way in
which, each operation
is performed in linked
list implementation of
stack.
What is a Node?
A Node in a linked list holds the data value and the pointer which points to the location of
the next node in the linked list.
What is a Node?

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.

● Insertion at the end of the list.


Deleting a node from the stack (POP
operation)
Deleting a node from the top of stack is referred to as pop operation.
Deleting a node from the linked list implementation of stack is
different from that in the array implementation. In order to pop an
element from the stack, we need to follow the following steps :
1. Check for the underflow condition: The underflow condition occurs when we
try to pop from an already empty stack. The stack will be empty if the head
pointer of the list points to null.
2. Adjust the head pointer accordingly: In stack, the elements are popped only
from one end, therefore, the value stored in the head pointer must be deleted
and the node must be freed. The next node of the head node now becomes the
head node.
Display the nodes (Traversing)

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

You might also like