LINKLIST
LINKLIST
UNIT - 2
Linked Lists
A pointer variable called START or HEAD which contains the address of the first node.
A special case is the list that has no nodes, such a list is called the null list or empty list
and isdenoted by the null pointer in the variable START.
• Memory usage: More memory is required in the linked list as compared to an array.
Because in a linked list, a pointer is also required to store the address of the next element
and it requires extra memory for itself.
• Traversal: In a Linked list traversal is more time-consuming as compared to an array.
Direct access to an element is not possible in a linked list as in an array by index. For
example, for accessing a node at position n, one has to traverse all the nodes before it.
• Reverse Traversing: In a singly linked list reverse traversing is not possible, but in the
case of a doubly-linked list, it can be possible as it contains a pointer to the previously
connected nodes with each node. For performing this extra memory is required for the
back pointer hence, there is a wastage of memory.
• Random Access: Random access is not possible in a linked list due to its dynamic memory
allocation.
• Lower efficiency at times: For certain operations, such as searching for an element or
iterating through the list, can be slower in a linked list.
• Complex implementation: The linked list implementation is more complex when
compared to array. It requires a complex programming understanding.
• Difficult to share data: This is because it is not possible to directly access the memory
address of an element in a linked list.
• Not suited for small dataset: Cannot provide any significant benefits on small dataset
compare to that of an array.
Let LIST be a linked list. Then LIST will be maintained in memory as follows.
1. LIST requires two linear arrays such as INFO and LINK-such that INFO[K] and
LINK[K] contains the information part and the nextpointer field of a node of LIST.
2. LIST also requires a variable name such as START which contains the location of
the beginning of the list, and a nextpointer sentinel denoted by NULL-which
indicates the end of the list.
3. The subscripts of the arrays INFO and LINK will be positive, so choose NULL =
0, unless otherwise stated.
The following examples of linked lists indicate that the nodes of a list need not occupy
adjacent elements in the arrays INFO and LINK, and that more than one list may be
maintained in thesame linear arrays INFO and LINK. However, each list must have its own
pointer variable giving the location of its first node.
START=9 INFO[9]=N
LINK[3]=6 INFO[6]=V
LINK[6]=11 INFO[11]=E
LINK[11]=7 INFO[7]= X
LINK[7]=10 INFO[10]= I
LINK[10]=4 INFO[4]= T
LINK[4]= NULL value, So the list has ended
Prof. Kiran Shejul Dr. DYPIMED
IT - 12. Data Structure & Algorithms Unit 1: Array / List
Suppose linked lists are implemented by parallel arrays and insertions and deletions are to
be performed linked lists. Then the unused memory cells in the arrays will also be linked
togetherto form a linked list using AVAIL as its list pointer variable. Such a data structure
will be denoted by
LIST (INFO, LINK, START,
AVAIL)
• Sometimes new data are to be inserted into a data structure but there is no available
space, i.e., the free-storage list is empty. This situation is usually called overflow.
Underflow
• The term underflow refers to the situation where one wants to delete data from a
datastructure that is empty.
• The programmer may handle underflow by printing the message UNDERFLOW.
• The underflow will occur with linked lists when START = NULL and there is a
deletion.
Suppose we have three nodes, and the addresses of these three nodes are 100, 200 and 300
respectively. The representation of three nodes as a linked list is shown in the below figure:
We can observe in the above figure that there are three different nodes having address 100, 200
and 300 respectively. The first node contains the address of the next node, i.e., 200, the second
node contains the address of the last node, i.e., 300, and the third node contains the NULL
value in its address part as it does not point to any node. The pointer that holds the address of
the initial node is known as a head pointer.
The linked list, which is shown in the above diagram, is known as a singly linked list as it
contains only a single link. In this list, only forward traversal is possible;
we cannot traverse in the backward direction as it has only one link in the list.
Insertion
The insertion into a singly linked list can be performed at different positions. Based on the
position of the new node being inserted, the insertion is categorized into the following
categories.
Insertion at beginning :
Inserting a new element into a singly linked list at beginning is quite simple. We just need to
make a few adjustments in the node links. There are the following steps which need to be
followed in order to insert a new node in the list at beginning.
The condition (head == NULL) gets satisfied. Hence, we just need to allocate the space for the
node by using malloc statement in C. Data and the link part of the node are set up by using the
following statements.
Since, ptr is the only node that will be inserted in the list hence, we need to make this node
pointed by the head pointer of the list. This will be done by using the following Statements.
Case 2: The node is being added to the end of the linked list
The condition Head = NULL would fail, since Head is not null. Now, we need to declare a
temporary pointer temp in order to traverse through the list. temp is made to point the first node
of the list.
Temp = head
Then, traverse through the entire linked list using the statements:
Now, we just need to make a few more link adjustments and our node at will be inserted at
the specified position. Since, at the end of the loop, the loop pointer temp would be pointing
to the node after which the new node will be inserted. Therefore, the next part of the new
node ptr must contain the address of the next part of the temp (since, ptr will be in between
temp and the next of the temp). This will be done by using the following statements.
ptr→ next = temp → next
now, we just need to make the next part of the temp, point to the new node ptr. This will
insert the new node ptr, at the specified position.
temp ->next = ptr;
Deletion at Begining:
Deleting a node from the beginning of the list is the simplest operation of all. It just needs a
few adjustments in the node pointers. Since the first node of the list is to be deleted, therefore,
we just need to make the head, point to the next of the head. This will be done by using the
following statements.
ptr = head;
head = ptr->next;
Now, free the pointer ptr which was pointing to the head node of the list. This will be done by
using the following statement.
free(ptr)
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: SET HEAD = HEAD -> NEXT
Step 4: FREE PTR
Step 5: EXIT
1. There is only one node in the list and that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be
deleted.
For this purpose, just declare a temporary pointer temp and assign it to head of the list. We also
need to keep track of the second last node of the list. For this purpose, two pointers ptr and ptr1
will be used where ptr will point to the last node and ptr1 will point to the second last node of
the list.
this all will be done by using the following statements.
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
Now, we just need to make the pointer ptr1 point to the NULL and the last node of the list that
is pointed by ptr will become free. It will be done by using the following statements.
ptr1->next = NULL;
free(ptr);
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 10
END OF IF
STEP 2: SET TEMP = HEAD
STEP 3: SET I = 0
STEP 4: REPEAT STEP 5 TO 8 UNTIL I
STEP 5: TEMP1 = TEMP
STEP 6: TEMP = TEMP → NEXT
STEP 7: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
STEP 8: I = I+1
END OF LOOP
STEP 9: TEMP1 → NEXT = TEMP → NEXT
STEP 10: FREE TEMP
STEP 11: EXIT
ptr = head;
while (ptr!=NULL)
{
ptr = ptr -> next;
}
Algorithm
STEP 1: SET PTR = HEAD
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 6
END OF IF
Algorithm
Step 1: SET PTR = HEAD
Step 2: Set I = 0
STEP 3: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 8
END OF IF
STEP 6: I = I + 1
STEP 7: PTR = PTR → NEXT
[END OF LOOP]
STEP 8: EXIT
As the name suggests, the doubly linked list contains two pointers. We can define the doubly
linked list as a linear data structure with three parts: the data part and the other two address
part. In other words, a doubly linked list is a list that has three parts in a single node, includes
one data part, a pointer to its previous node, and a pointer to the next node.
Suppose we have three nodes, and the address of these nodes are 100, 200 and 300,
respectively. The representation of these nodes in a doubly-linked list is shown below:
As we can observe in the above figure, the node in a doubly-linked list has two address parts;
one part stores the address of the next while the other part of the node stores the previous
node's address. The initial node in the doubly linked list has the NULL value in the address
part, which provides the address of the previous node.
Algorithm:
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET TEMP = START
Step 7: Repeat Step 8 while TEMP -> NEXT != NULL
Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 9: SET TEMP -> NEXT = NEW_NODE
Step 10C: SET NEW_NODE -> PREV = TEMP
Step 11: EXIT
Prof. Kiran Shejul Dr. DYPIMED
IT - 12. Data Structure & Algorithms Unit 1: Array / List
Insertion in doubly linked list after Specified node
In order to insert a node after the specified node in the list, we need to skip the required number
of nodes in order to reach the mentioned node and then make the pointer adjustments as
required.
Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 15
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET TEMP = START
Step 6: SET I = 0
Step 7: REPEAT 8 to 10 until I
Step 8: SET TEMP = TEMP -> NEXT
STEP 9: IF TEMP = NULL
STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS"
GOTO STEP 15
[END OF IF]
[END OF LOOP]
Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT
Step 12: SET NEW_NODE -> PREV = TEMP
Step 13 : SET TEMP -> NEXT = NEW_NODE
Step 14: SET TEMP -> NEXT -> PREV = NEW_NODE
Step 15: EXIT
Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 4: SET HEAD → PREV = NULL
STEP 5: FREE PTR
STEP 6: EXIT
We just need traverse the list in order to search for a specific element in the list. Perform
following operations in order to search a specific operation.
• Traverse the list until the pointer ptr becomes null. Keep shifting pointer to its next and
increasing i by +1.
• Compare each element of the list with the item which is to be searched.
• If the item matched with any node value then the location of that value, I will be
returned from the function else NULL is returned.
Algorithm
Step 1: IF HEAD == NULL
WRITE "UNDERFLOW"
GOTO STEP 8
[END OF IF]
Step 2: Set PTR = HEAD
Step 3: Set i = 0
Step 4: Repeat step 5 to 7 while PTR != NULL
Step 5: IF PTR → data = item
return i
[END OF IF]
Step 6: i = i + 1
Step 7: PTR = PTR → next
Step 8: Exit
then, traverse through the list by using while loop. Keep shifting value of pointer variable ptr
until we find the last node. The last node contains null in its next part.
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
Although, traversing means visiting each node of the list once to perform some specific
operation. Here, we are printing the data associated with each node of the list.
Algorithm
Step 1: IF HEAD == NULL
WRITE "UNDERFLOW"
GOTO STEP 6
[END OF IF]
Step 2: Set PTR = HEAD
Step 3: Repeat step 4 and 5 while PTR != NULL
Step 4: Write PTR → data
Step 5: PTR = PTR → next
Step 6: Exit
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.
Circular linked list is mostly used in task maintenance in operating systems. There are many
examples where circular linked list is being used in computer science including browser surfing
where a record of pages visited in the past by the user, is maintained in the form of circular
linked lists and can be accessed again on clicking the previous button.
In circular doubly linked list, each node has two pointers prev and next, similar to doubly linked
list. The prev pointer points to the previous node and the next points to the next node. Here, in
addition to the last node storing the address of the first node, the first node will also store the
address of the last node.