0% found this document useful (0 votes)
16 views

Module 9 - Basic ADTS (Linked Data Structures)

Uploaded by

Grace Anyanwu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Module 9 - Basic ADTS (Linked Data Structures)

Uploaded by

Grace Anyanwu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

To see this more clearly lets look at an example:

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.

(a) Singly Linked Lists


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.

(b) Doubly Linked Lists


Doubly Linked Lists contain three “buckets” in one node; one bucket holds
the data and the other buckets hold the addresses of the previous and next
nodes in the list. The list is traversed twice as the nodes in the list are
connected to each other from both sides.

(c) Circular Linked Lists


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.

Operations on Linked List:


There are several operations associated with linked list i.e.

a) Traversing a Linked List


Suppose we want to traverse LIST in order to process each node exactly
once. The traversing algorithm uses a pointer variable PTR which points to
the node that is currently being processed. Accordingly, PTR->NEXT points
to the next node to be processed so,

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

Algorithm: (Traversing a Linked List)


Let LIST be a linked list in memory. This algorithm traverses LIST, applying an
operation PROCESS to each element of list. The variable PTR point to the node
currently being processed.

1. Set PTR=HEAD. [Initializes pointer PTR.]


2. Repeat Steps 3 and 4 while PTR!=NULL.
3. Apply PROCESS to PTR-> INFO.
4. Set PTR= PTR-> NEXT [PTR now points to the next node.]
[End of Step 2 loop.]
5. Exit.

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:

START = 9, so INFO[9] = N is the first character.

LINKLO] = 3, so 1NF0131 0 is the second character.

LINK(31 = 6, so INFO(6] = Li (blank) is the third character.

LINK161 11, so INFO[ II) E is the fourth character.

LINKI! 11 = 7, so INFOI71 = X is the fifth character.

LINK[7] = 10, so INFOLIOJ = I is the sixth character.

LINK[ 101 4, so INFO[41 = T is the seventh character.

LINK(4] = 0. the NULL value, so the list has ended.

In other words, NO EXIT is the character string.

b) Searching a Linked List:


Let list be a linked list in the memory and a specific ITEM of information is
given to search. If ITEM is actually a key value and we are searching through
a LIST for the record containing ITEM, then ITEM can appear only once in the
LIST. Search for wanted ITEM in List can be performed by traversing the list
using a pointer variable PTR and comparing ITEM with the contents PTR-
>INFO of each node, one by one of list.

Algorithm: SEARCH(INFO, NEXT, HEAD, ITEM, PREV, CURR, SCAN)


LIST is a linked list in the memory. This algorithm finds the location
LOC of the node where ITEM first appear in LIST, otherwise sets
LOC=NULL.
1. Set PTR=HEAD.
2. Repeat Step 3 and 4 while PTR≠NULL:
3. if ITEM = PTR->INFO then:
Set LOC=PTR, and return. [Search is successful.]
[End of If structure.]
4. Set PTR=PTR->NEXT
[End of Step 2 loop.]
5. Set LOC=NULL, and return. [Search is unsuccessful.]
6. Exit

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).

Basic Search Concept


Assume there is a sorted linked list and we wish that after each
insertion/deletion this list should always be sorted. Given a target value, the
search attempts to locate the requested node in the linked list. Since nodes in a
linked list have no names, we use two pointers, pre (for previous) and cur (for
current) nodes. At the beginning of the search, the pre pointer is null and the cur
pointer points to the first node (Head). The search algorithm moves the two
pointers together towards the end of the list. Following Figure shows the
movement of these two pointers through the list in an extreme case scenario:
when the target value is larger than any value in the list.

Moving of pre and cur pointers in searching a linked list

Insertion into a Linked List:


If a node N is to be inserted into the list between nodes A and B in a linked list
named LIST.
Its schematic diagram would be:

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.

Inserting a new node in list:


The following algorithm inserts an ITEM into LIST.

Algorithm: INSERT( ITEM)


[This algorithm add newnodes at any position (Top, in Middle and at
End) in the List ]
1. Create a NewNode node in memory
2. Set NewNode -> INFO =ITEM. [Copies new data into INFO of new node.]
3. Set NewNode -> NEXT = NULL. [Copies NULL in NEXT of new node.]
4. If HEAD=NULL, then HEAD=NewNode and return. [Add first node in list]
5. if NewNode-> INFO < HEAD->INFO

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

Delete a node from list:


The following algorithm deletes a node from any position in the LIST.

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

You might also like