Linked List (Unit 5)
Linked List (Unit 5)
Linked List
o Linked List can be defined as collection of objects called nodes that are
randomly stored in the memory.
o A node contains two fields i.e. data stored at that particular address and the
pointer which contains the address of the next node in the memory.
o The last node of the list contains pointer to the null.
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
But there are many use cases, like the one where we don't know the quantity of data to
be stored, for which advanced data structures are required, and one such data
structure is linked list.
Linear Linked List
Linear Linked list is the default linked list and a linear data structure in which data is
not stored in contiguous memory locations but each data node is connected to the next
data node via a pointer, hence forming a chain.
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.
Write Underflow
WRITE OVERFLOW
GOTO STEP 12
END OF IF
Write UNDERFLOW
Go to Step 5
[END OF IF]
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
WRITE UNDERFLOW
GOTO STEP 10
END OF IF
END OF LOOP
There are two scenarios of inserting any element into doubly linked list. Either the list is
empty or it contains at least one element. Perform the following steps to insert a node
in doubly linked list at beginning.
Algorithm :
o Step 1: IF ptr = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Algorithm
o Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
[END OF LOOP]
Algorithm
o Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 15
[END OF IF]
GOTO STEP 15
[END OF IF]
[END OF LOOP]
Algorithm
o STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
Algorithm
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
[END OF LOOP]
Write UNDERFLOW
Go to Step 9
[END OF IF]
[END OF LOOP]
We traverse a circular singly linked list until we reach the same node where we started.
The circular singly liked list has no beginning and no ending. There is no null value
present in the next part of any of the nodes.
Algorithm
o Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
[END OF LOOP]
Write OVERFLOW
Go to Step 1
[END OF IF]
[END OF LOOP]
[END OF LOOP]
next →← prev
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
o Step 6: SET PREPTR -> NEXT = HEAD
o Step 7: FREE PTR
o Step 8: EXIT