Linked List
Linked List
CONTENTS COVERED
1. Traversing a Linked List
2. Searching in an unsorted Linked List
3. Overflow and Underflow Conditions
4. Insertion at the beginning of a Linked List
5. Insertion after a given node in a Linked List
6. Insertion at the end of a Linked List
7. Deletion after a given node in a Linked List
8. Introduction to Circular Header Linked List
9. Introduction to Two-way Linked List / Doubly Linked List
ALGORITHM: Let LIST be a linked list in memory. Variable PTR points to the node currently
being processed.
1. PTR=START
2. While PTR != NULL
3. Process INFO[PTR]
4. PTR = LINK[PTR]
5. Return
LOGIC:
ALGORITHM: Let LIST be a linked list in memory and START is pointing to the first node of
the LIST. This algorithm finds the location LOC of the node where ITEM first appears in LIST,
or sets LOC=NULL. Variable PTR points to the node currently being processed.
1. PTR = START
2. While PTR != NULL
3. If ITEM = INFO[PTR]
4. Set LOC = PTR
5. Return
6. Else
7. PTR = LINK[PTR]
8. Set LOC = NULL // If search is unsuccessful
9. Return
OVERFLOW: When new data needs to be inserted but there is no available space in memory,
i.e. AVAIL = NULL. (If available space (AVAIL) is null, print OVERFLOW).
UNDERFLOW: When one wants to delete data from a data structure which is already empty, i.e.
START=NULL. (If START is pointing to null, print UNDERFLOW).
ALGORITHM: This algorithm inserts ITEM at the very first position or as first node in the given
linked list LIST. Variable NEW is pointing to the new node to be inserted.
1. If AVAIL=NULL
2. Print OVERFLOW
3. Return
4. NEW = AVAIL
5. INFO[NEW] = ITEM
6. LINK [NEW] = START
7. START = NEW
8. Return
LOGIC:
ALGORITHM: This algorithm inserts ITEM so that ITEM follows he node with the location
LOC or inserts ITEM as the first node when LOC = NULL.
1. If AVAIL = NULL
2. Print OVERFLOW
3. Return
4. NEW = AVAIL
5. INFO[NEW]= ITEM
6. If LOC= NULL
7. LINK[NEW] = START
8. START = NEW
9. Else
10. LINK [NEW] = LINK [LOC]
11. LINK[LOC] = NEW
12. Return
LOGIC:
LOGIC:
ALGORITHM: This algorithm deletes the node N with location LOC from the linked list LIST.
LOCP is the location of the node which precedes N or, when N is the first node, LOCP = NULL.
1. If LOCP = NULL
2. START = LINK[START]
3. Else
4. LINK[LOCP] = LINK[LOC]
5. Free (LOC)
6. Return
LOGIC:
8. CIRCULAR HEADER LINKED LIST
A header linked list is a linked list which always contains a special node, called header node, at
the beginning of the list. And a circular header linked list is a header list where the last node
points back to the header node.
1. PTR=LINK[START]
2. While PTR != START
3. Process INFO[PTR]
4. PTR=LINK[PTR]
5. Return
A 2-way list is a linear collection of data elements, called nodes, where each node N is divided
into 3 parts:
The list also requires 2 list pointer variables: FIRST, which points to the first node in the list, and
LAST, which points to the last node in the list.
OR
Reference: