Single Linked List is a collection of nodes. Each node contains 2 fields: I) info where the information is stored and ii) link which points to the next node in the list.
The node is like this:
Node
Info (or) data Link (or) next
The operations that can be performed on single linked lists includes: insertion, deletion and traversing the list.
Inserting a node in a single linked list:
Insertion can be done in 3 ways:
1.Inserting an element in the beginning of the list.
2.Inserting in the middle and 3.Inserting at end.
The algorithm for inserting an element in the beginning of a list:
Procedure insertbeg(x, first)
/* x – is an element first is the Pointer to the first element in a SLL */
begin
if avail = NULL then
Write(‘ Availability Stack underflow’) return (first)
else
new avail;
avail link(avail) info(new) x link(new) first return (new);
end.
In the algorithm, first, the memory for a new node is available or not is checked out. Here avail is a pointer to the available memory.
If it is NULL then there is no memory. Otherwise a new node is created from available memory and it is stored in new. The new contains the address of the new node and avail moves forward to point to next available memory location. In the info field of new, x is stored and the link field of new points to the first element address of the list.
Now, new becomes the pointer to the whole list.
This can be shown as:
(a)
(b)
First is having value of 100, means it is Pointing the 100th memory location. The node at 100th location is having info as 10 and containing the address 200 of the next node. The second node at 200th location, contains 20 and address 300 of the next node.
At,300th memory location, the node contains info as 30 and its link field is NULL. That means it is no more having any nodes. That is, the list is having 3 nodes with a starting address of 100.
Now, we created another node new that is having the address of 50 and its info is 40 and its link field is NULL.
After the call to insertbeg( ) function the list will look like.
The insertbeg( ) function, inserts a node by storing the address of the first node of list in the link field of new and making the new address as the pointer to the list.
The algorithm for inserting an element at the end of the list:
Procedure insertend(x, first) begin
if avail = null then /* Checking for memory availability */
write (‘ Availability Stack underflow’);
return(first);
else /* Obtaining the next free node */
new avail; /* Removing free node from available memory */
avail link(avail)
info(new) x/* Initializing the fields of new node */
link(new) NULL
if first = NULL then /* Is the list empty? */
return(new);
Save first /* Searching the last node */
Repeat while link (Save) NULL
Save link(Save) /* Setting the link field of last node to new */
Link (Save) new return (first)
end;
The Pictorial Representation:
(a)
(b)
(c)
(d)
first (100) address is stored in Save to go through the end of the list, after reaching to the last node, set the address of new node in the link field of last node.
Inserting in the middle:
In this process, the address of first is stored in Save to go to the particular node at which the insertion is to be done. After reaching the particular node, set the link to point to new node, and set the link of new node to connect the remaining nodes.
ALGORITHM: The algorithm for inserting an element in the middle of a list:
Procedure insertmid(x, first) begin
if avail = NULL then
write (‘ Availability Stack Underflow’);
return (first)
else /* Obtain the address of next free node */
new avail
avail link(avail) /* Removing free node */
info(new) x /* Copying Information into new node */
if first = NULL then /* Checking whether the list is empty */
link(new) NULL /* list is empty*/
return(new) /* if the new data precedes all other in the list */
if info(new) info(first) then link(new) first
return(new) /* initialise temporary Pointer */
Save first
Repeat while link(Save) NULL and Info(link(Save)) info(new)
Save link(Save) /* Search for predecessor of new data */
link(new) link(Save) /* Setting the links of new and its Predecessor*/
link(Save) new return(first) end
(a)
(b)
(c)
(d)
(e) Deleting a node in a single linked list:
Deletion can be done in 3 ways:
1. Deleting an element in the beginning of the list.
2. Deleting in the middle and 3. Deleting at the end.
The algorithms for deleting an element in the list:
Procedure:
If the linked list is empty then write underflow and return
Repeat Step 3 while the end of the list has not been reached and the node has not been found.
Obtain the next node in the list and record its predecessor node.
If the end of the list has been reached then write node not found and return.
delete the node from the list.
Return the node to the availability area.
Algorithm:
Procedure deletelement(x, first) begin
if first = NULL then /* Checking whether the list is empty */
write(‘ Underflow’);
return
temp first /* Initializing search for x */
while((temp x ) and (link(temp) NULL)) begin
pred temp temp link(temp)
end
if temp x then /* if the node not found */
write(‘node not found’) return
if (x = first) then / is x first node */
first link(first) /* delete the node at x */
else link(pred) link(x)
link(x) avail /* Return node to availability area */
avail x return:
end.
In the algorithm, it first checks whether the list is empty, if it is empty it prints underflow message. Otherwise, it initializes a temporary pointer to first. Until the Predecessor of x node found, the temporary pointer is moved forward. If it reaches the end of the list, without finding the node of address x, it flashes an error message, stating
‘node not found’. If x is the first address, then first node is deleted and now first points to second node. Otherwise it sets the links so that it deletes the node of address x.
The original list is:
1. If x = 100 (First node)
(a)
(b)
The node which is 100th memory address will be added to free space (ii) if x = 200
(a)
(b)
(c) (iii) if x = 300 (Last node)
(a)
(b) Traversing the List:
This includes, visiting the node and printing the data of that node one at a time and moving forward until it reaches end of the list. (i.e., link becomes NULL)
Procedure(first) begin
for (temp = first; temp!= NULL; temp = temp link) write (temp info)
end.
APPLICATIONS LINKED LISTS:
The main Applications of Linked Lists are
It means in addition/subtraction /multipication.. of two polynimials.
Eg:p1=2x^2+3x+7 and p2=3x^3+5x+2 p1+p2=3x^3+2x^2+8x+9
* In Dynamic Memory Management
In allocation and releasing memory at runtime.
*In Symbol Tables in Balancing paranthesis
* Representing Sparse Matrix