1. Linked List
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and DataJava and Data
StructuresStructures
2. Linked List
• Linear collection of data elements called
nodes.
• Linear order is given by means of pointers.
• Each node is divided into two parts, first part
contains info and second part, called link.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
NULLNULL
Start Node A Node B Node C End
10 20 30 40
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
3. Representation of Linked List
• Let LIST be a linked list,
• LIST requires two linear array:
– INFO[k] – information part
– LINK [k] – next pointer field
• Also requires variable Name – such as START-
which contains the location of the beginning
of the list
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
4. Representation of Linked List
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
O
J
L
W
N
START
1
2
3
4
5
6
7
8
9
10
6
7
5
3
INFO LINK
9 START 9 INFO[9] N
LINK[9] 3 INFO[3] O
LINK[3] 6 INFO[6] L
LINK[6] 5 INFO[5] J
LINK[5] 7 INFO[7] W
5. Traversing a Linked List
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
NULLNULL
Start
Node A Node B Node C End
20 30 4010
PTR
Algorithm TraverseList(INFO, LINK, START)
{
PTR := START;
while (PTR != NULL)
{
Process --> INFO[PTR];
PTR := LINK[PTR];
}
}
6. Traversing a Linked List
• Write algorithm to
– Print elements
– Count elements
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
7. Searching a Linked List
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm SearchList(INFO, LINK, START, ITEM)
{
PTR := START;
while (PTR != NULL)
{
if (ITEM = INFO[PTR])THEN
LOC := PTR;
else
PTR := LINK[PTR];
}
}
8. Insertion into Linked List:@beginning
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm InsertFirst(INFO, LINK, START, AVAIL, ITEM)
{
if(AVAIL = NULL) THEN
write("Overflow!");
else
NEW := AVAIL;
AVAIL := LINK [AVAIL];
INFO[NEW] := ITEM;
LINK[NEW]:=START;
START:=NEW;
}
NULLNULL
Start
Node A Node B Node C End
20 30 4010
NULLNULL
AVAIL
NEW 50
9. Inserting after a given node
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm InsertAtLoc(INFO, LINK, START, AVAIL, LOC, ITEM)
{
if(AVAIL = NULL)
write("Overflow");
else
NEW := AVAIL;
AVAIL := LINK [AVAIL];
INFO [NEW] := ITEM;
if(LOC = NULL) then
{
LINK [NEW] := START;
START := NEW;
}
else
{
LINK [NEW] := LINK[LOC];
LINK[LOC] :=NEW;
}
}
10. Inserting @end
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm InsertAtEnd(INFO, LINK, START, AVAIL, ITEM)
{
if(AVAIL = NULL)
write("Overflow");
else
NEW := AVAIL;
AVAIL := LINK [AVAIL];
INFO [NEW] := ITEM;
PTR := START;
while (LINK[PTR] != NULL)
{
PTR := LINK[PTR];
}
LINK [PTR] := NEW;
}
11. Deleting first node
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm DeleteFirst(INFO, LINK, START, AVAIL)
{
PTR := LINK [START];
if(PTR = NULL)
write("Underflow!");
else
PTR1 := LINK [PTR];
LINK [START] := PTR1;
//Returns deleted node to the AVAIL list.
LINK [PTR] := AVAIL;
AVAIL := PTR;
}
NULLNULL
Start
Node A Node B Node C End
20 30 4010
NULLNULL
AVAIL
PTR PTR1
12. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Deleting End node
Algorithm DeleteEnd(INFO, LINK, START, AVAIL)
{
PTR := START;
if(LINK[PTR] = NULL)
write("Underflow!");
else
while(LINK [PTR]!=NULL)
{
PTR1 := PTR;
PTR := LINK [PTR];
}
LINK [PTR1] := NULL;
//Returns deleted node to the AVAIL list.
LINK [PTR] := AVAIL;
AVAIL := PTR;
}
NULLNULL
Start
Node A Node B Node C End
20 30 4010
NULLNULL
AVAIL
PTR PTR1
NULL
13. Deleting specific node
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm DeleteSpecific(INFO, LINK, START, AVAIL, KEY)
{
PTR1 := START;
PTR := LINK [PTR1];
while(PTR != NULL)
{
if (INFO [PTR] != KEY) then
{
PTR1 := PTR;
PTR := LINK[PTR];
}
else
{
LINK [PTR1] := LINK [PTR];
}
//Returns deleted node to the AVAIL list.
LINK [PTR] := AVAIL;
AVAIL := PTR;
}
if(PTR = NULL)then
write("NODE with KEY does not exist");
}