Linked List: Data Structure and Algorithms
Linked List: Data Structure and Algorithms
Linked List: Data Structure and Algorithms
Topics
Singly Linked Lists and Chains, Representing
Chains in C, Polynomials, Sparse Matrix,
Doubly Linked Lists, Circular & Header Linked
Lists
Linked Lists
A linked list, is a linear collection of data
for each node is allocated dynamically means as and when required. So the
Linked List can increase as per the user wish and the size is not fixed, it can
vary.
Suppose first node of linked list is allocated with an address
1008. Its graphical representation looks like the figure
shown below
becomes
Address
Comments
links
1008
506
506
10
10
NULL
manner.
Searchsearch an element using given key or data item.
Deletedelete an element using given key or data item.
Traverse Backwarddisplaying complete list in backward manner.
Applicable for double and circular linked list
Traverse Algorithm
1.Traverse Algorithm
Consider LIST be a linked list in memory
stored in linear arrays INFO and LINK with
START pointing to the first element, NULL
indicating the end of the list and PTR is the
pointer that points to the node currently
being processed. Below is the algorithm to
traverse through all nodes exactly once and
write the node data element and can be
represented using procedure
PRINT(INFO,LINK,START)
1.Set PTR :=START
2.Repeat steps 3 and 4 while PTR<>NULL
3. PRINT INFO[PTR]
4. PTR:=LINK[PTR]
[PTR pointing to next node]
5.Exit
Searching Algorithm
Unsorted List
Consider LIST be a linked list in memory stored in
linear arrays INFO and LINK with START
pointing to the first element, NULL indicating the
end of the list and PTR is the pointer that points to
the node currently being processed and ITEM is
the information given. Below is the algorithm to
find the location LOC of the node where ITEM
first appears in the LIST and can be represented by
SEARCH(INFO,LINK,START,ITEM)
1.Set PTR = START
2.Set LOC=NULL
3.Repeat steps 4 and 5 while PTR <> NULL
4.IF (ITEM= INFO[PTR]) THEN LOC=PTR
EXIT
5.ELSE PTR=LINK[PTR]
6.Exit
Sorted List
Consider LIST be a linked list in memory
stored in linear arrays INFO and LINK with
START pointing to the first element, NULL
indicating the end of the list and PTR is the
pointer that points to the node currently being
processed and ITEM is the information given.
Below is the algorithm to find the location
LOC of the node where ITEM first appears in
the LIST and can be represented by
SEARCH_SL(INFO,LINK,START,ITEM)
1.Set PTR = START
2.Set LOC = NULL
3.Repeat 4 while PTR<>NULL
4.IF(ITEM<INFO[PTR]) then Set
PTR=LINK[PTR]
else
IF(ITEM=INFO[PTR])
then LOC=PTR
EXIT
else EXIT
Garbage Collection
The maintenance of the linked list in
memory assumes the possibility of inserting
new nodes into the list requires some
mechanism which provides unused memory
space for the new nodes. Analogously some
mechanism is required where by the memory
space of deleted nodes becomes available
for further use. Together with the linked list
in memory, a special list is maintained which
consists of unused memory cells. The list,
which has its own pointer, is called the list
of available space or the free storage list or
free pool. Suppose the linked list is
implemented by parallel arrays and then the
unused memory cells in the array will be
also linked together to form linked list using
AVAIL as its list pointer variable.
deleted space on to the free storage list and the techniques does this
collection is called garbage collection. It is invisible to the
programmer. It takes 2 steps
1st The computer runs through all the list, tagging those cells which
are currently in use
2nd computer run through the memory, collecting all untagged
space onto the free-storage list The garbage collection take place
whenThere is only some minimum amount of space
No space at all left in the free-storage list
CPU is idle and has time to do the collection
Insertion Algorithm
Various situations are possible for inserting a node in a linked list. They are
as following
Inserts node at the beginning of the list
Inserts node after the node with a given location
Inserts a node into a sorted list
All our algorithms assume that the linked list is in memory in the form
LIST(INFO,LINK,START,AVAIL)
and that the variable ITEM contains new information to be added to the list.
All algorithms will include following steps:
If AVAIL=NULL, then print OVERFLOW
NEW:=AVAIL, AVAIL=LINK[AVAIL]
INFO[NEW]:=ITEM
INSFIRST(INFO, LINK,START,AVAIL,ITEM)
1.If AVAIL=NULL, then Write: OVERFLOW and Exit
2.Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
3.Set INFO[NEW]:=ITEM
4.LINK[NEW]:=START
5.START:=NEW
6.Exit
Deletion Algorithm
Double
Linked
List
Insertion
Algorithm
Suppose we are given the locations LOCA and LOCB of adjacent
nodes A and B in LIST and wanted to insert a given ITEM of
information between nodes A and B. As with a single linklist, we
first remove the first node N from the AVAIL list, using the
variable NEW to keep track of its location and then copy the
data ITEM into the node N i.e.we set: NEW=AVAIL,
AVAIL=NEXT[AVAIL], INFO[NEW]=ITEM As shown in the picture,
the node N with contents ITEM is inserted into the list by
changing the following four pointers: NEXT[LOC]=NEW,
NEXT[NEW]=LOCB, PREV[LOCB]=NEW and PREV[NEW]=LOCA
1.IF(AVAIL=NULL) THEN PRINT(OVERFLOW) and EXIT
2.NEW=AVAIL, AVAIL=NEXT[AVAIL], INFO[NEW]=ITEM
3.NEXT[LOCA]=NEW, NEXT[NEW]=LOCB, PREV[LOCB]=NEW
and PREV[NEW]=LOCA 4.Exit
Sparse Matrix
A sparse matrix is a two-dimensional array in which most
hnodes,onepernon
zeroelementofthematrix.Forexample,considert
hesparsematrix
Thelinkedlistcanberepresentedusinglinkedlistasshow
nbelow