Data Structure and Algorithms - Basic Linked Lists
Data Structure and Algorithms - Basic Linked Lists
Linked List
If arrays accommodate similar types of data types, linked lists
consist of elements with different data types that are also
arranged sequentially.
Singly linked lists contain two “buckets” in one node; one bucket
holds the data and the other bucket holds the address of the next
node of the list. Traversals can be done in one direction only as
there is only a single link between two nodes of the same list.
Circular linked lists can exist in both singly linked list and doubly
linked list.
Since the last node and the first node of the circular linked list are
connected, the traversal in this linked list will go on forever until it
is broken.
Insertion Operation
Adding a new node in linked list is a more than one step activity.
We shall learn this with diagrams here. First, create a node using
the same structure and find the location where it has to be
inserted.
Imagine that we are inserting a node B (NewNode), between A
(LeftNode) and C (RightNode). Then point B.next to C −
Now, the next node at the left should point to the new node.
This will put the new node in the middle of the two. The new list
should look like this −
Insertion at Beginning
In this operation, we are adding an element at the beginning of
the list.
Algorithm
1. START
2. Create a node to store the data
3. Check if the list is empty
4. If the list is empty, add the data to the node and assign the head pointer to
it.
5 If the list is not empty, add the data to a node and link to the current head.
Assign the head to the newly added node.
6. END
Insertion at Ending
Algorithm
1. START
2. Create a new node and assign the data
3. Find the last node
4. Point the last node to new node
5. END
Algorithm
1. START
2. Create a new node and assign data to it
3. Iterate until the node at position is found
4. Point first to new first node
5. END
Deletion Operation
Deletion is also a more than one step process. We shall learn with
pictorial representation. First, locate the target node to be
removed, by using searching algorithms.
The left (previous) node of the target node now should point to
the next node of the target node −
This will remove the link that was pointing to the target node.
Now, using the following code, we will remove what the target
node is pointing at.
Deletion at Beginning
Algorithm
1. START
2. Assign the head pointer to the next node in the list
3. END
Deletion at Ending
Algorithm
1. START
2. Iterate until you find the second last element in the list.
3. Assign NULL to the second last element in the list.
4. END
Deletion at a Given Position
Algorithm
1. START
2. Iterate until find the current node at position in the list
3. Assign the adjacent node of current node in the list to its previous node.
4. END
Reverse Operation
This operation is a thorough one. We need to make the last node
to be pointed by the head node and reverse the whole linked list.
We have to make sure that the last node is not the last node. So
we'll have some temp node, which looks like the head node
pointing to the last node. Now, we shall make all left side nodes
point to their previous nodes one by one.
Except the node (first node) pointed by the head node, all nodes
should point to their predecessor, making them their new
successor. The first node will point to NULL.
We'll make the head node point to the new first node by using the
temp node.
Algorithm
1 START
2. We use three pointers to perform the reversing: prev, next, head.
3. Point the current node to head and assign its next value to the prev node.
4. Iteratively repeat the step 3 for all the nodes in the list.
5. Assign head to the prev node.
Search Operation
Searching for an element in the list using a key element. This
operation is done in the same way as array search; comparing
every element in the list with the key element given.
Algorithm
1 START
2 If the list is not empty, iteratively check if the list contains the key
3 If the key element is not present in the list, unsuccessful search
4 END
Traversal Operation
The traversal operation walks through all the elements of the list
in an order and displays the elements in that order.
Algorithm
1. START
2. While the list is not empty and did not reach the end of the list, print the
data in each node
3. END