You are on page 1of 7

LINKED LIST (ALGORITHMS and EXAMPLES)

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

ONE WAY LINKED LIST:

1. TRAVERSING A 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:

2. SEARCHING IN AN UNSORTED LINKED LIST

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

3. OVERFLOW AND UNDERFLOW CONDITIONS

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

4. INSERTION AT THE BEGINNING OF A LINKED LIST

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:

5. INSERTION AFTER A GIVEN NODE IN A LINKED LIST

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:

6. INSERTION AT THE END OF A LINKED LIST

LOGIC:

7. DELETION AFTER A GIVEN NODE IN A LINKED LIST

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.

How to check whether circular header list is empty?

LINK [START] = START

TRAVERSING A CIRCULAR HEADER LINKED LIST:

1. PTR=LINK[START]
2. While PTR != START
3. Process INFO[PTR]
4. PTR=LINK[PTR]
5. Return

9. DOUBLY LINKED LIST / TWO WAY LINKED LIST

A 2-way list is a linear collection of data elements, called nodes, where each node N is divided
into 3 parts:

1. An information field INFO which contains the data of N.


2. A pointer field FORW which contains the location of the next node in the list.
3. A pointer filed BACK which contains the location of the preceding node in the list.

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

Another Representation of Doubly Linked List:

LAST pointer is not necessary to maintain.

Reference:

Data Structures with C (Schaum's Outline Series) by Seymour Lipschutz, TMH

You might also like