Module 9 - Basic ADTS (Linked Data Structures)
Module 9 - Basic ADTS (Linked Data Structures)
Linked List:
A linked list or one way list is a linear collection of data elements, called nodes,
where the linear order is given by means of “pointers”. Each node is divided
into two parts.
• The first part contains the information of the element.
• The second part called the link field contains the
address of the next node in the list.
The Head is a special pointer variable which contains the address of the first
node of the list. If there is no node available in the list then Head contains
NULL value that means, List is empty. The left part of the each node
represents the information part of the node, which may contain an entire
record of data (e.g. ID, name, marks, age etc). the right part represents
pointer/link to the next node. The next pointer of the last node is null pointer
signal the end of the list.
Advantages:
List of data can be stored in arrays but linked structures (pointers) provide
several advantages. A linked list is appropriate when the number of data
elements to be represented in data structure is unpredictable. It also
appropriate when there are frequently insertions & deletions occurred in the
list. Linked lists are dynamic, so the length of a list can increase or decrease
as necessary.
Page 1 of 7
Types of Linked List
The following are the various types of linked list.
Page 2 of 7
PTR=HEAD [ Moves the pointer to the first node of the list]
PTR=PTR->NEXT [ Moves the pointer to the next node in the list.]
PTR
Example
The Figure below pictures a linked list in memory where each node of the list
contains a single character.
Page 3 of 7
We can obtain the actual list of characters, or, in other words, the string, as
follows:
Page 4 of 7
Search Linked List for insertion and deletion of Nodes:
Both insertion and deletion operations need searching the linked list.
• To add a new node, we must identify the logical predecessor
(address of previous node) where the new node is to be inserting.
• To delete a node, we must identify the location (addresses) of the
node to be deleted and its logical predecessor (previous node).
Page 5 of 7
Inserting at the Beginning of a List:
If the linked list is sorted list and new node has the least low value already stored
in the list i.e. (if New->info < Head->info) then new node is inserted at the beginning
/ Top of the list.
Page 6 of 7
then Set NewNode->NEXT=HEAD and HEAD=NewNode and return
[Add node on top of existing list]
6. PrevNode = NULL, CurrNode=NULL;
7. for(CurrNode =HEAD; CurrNode != NULL; CurrNode = CurrNode ->NEXT)
{ if(NewNode->INFO <= CurrNode ->INFO)
{
break the loop
}
PrevNode = CurrNode;
} [ end of loop ]
[Insert after PREV node (in middle or at end) of the list]
8. Set NewNode->NEXT = PrevNode->NEXT and
9. Set PrevNode->NEXT= NewNode.
10.Exit
Algorithm: DELETE(ITEM)
LIST is a linked list in the memory. This algorithm deletes the node
where ITEM first appear in LIST, otherwise it writes “NOT FOUND”
1. if Head =NULL then write: “Empty List” and return [Check for Empty List]
2. if ITEM = Head -> info then: [ Top node is to delete ]
Set Head = Head -> next and return
3. Set PrevNode = NULL, CurrNode=NULL.
4. for(CurrNode =HEAD; CurrNode != NULL; CurrNode = CurrNode ->NEXT)
{
if (ITEM = CurrNode ->INFO ) then:
{
break the loop
}
Set PrevNode = CurrNode;
} [ end of loop ]
5. if(CurrNode = NULL) then write : Item not found in the list and return
6. [delete the current node from the list]
Set PrevNode ->NEXT = CurrNode->NEXT
7. Exit
Page 7 of 7