Chapter 3Chapter 3
linked listslinked lists
01/29/18 BY MS. SHAISTA QADIR 1
PRESENTED BY
Shaista Qadir
Lecturer king khalid university
COntentsCOntents
 linked list
 singly linked list
 insertiOn in singly linked list
 deletiOn in singly linked list
 searChing a singly linked list
 dOubly linked list
 insertiOn frOm dOubly linked list
 deletiOn frOm dOubly linked list
 searChing a dOubly linked list
 CirCular linked list
01/29/18 BY MS. SHAISTA QADIR 2
linked listlinked list
01/29/18 BY MS. SHAISTA QADIR 3
 A linked structure is a collection of nodes storing data and links
to other nodes.
 In this way, nodes can be located anywhere in memory, and
passing from one node of the linked structure to another is
accomplished by storing the reference(s) to other node(s) in the
structure.
 Although linked structures can be implemented in a variety of
ways, the most flexible implementation is by using a separate
object for each node.
 Types of Linked List:
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
singly linked listsingly linked list
01/29/18 BY MS. SHAISTA QADIR 4
 A sequence of nodes, each node holding some information
and a reference to another node in the list.
 If a node has a link only to its successor in this sequence,
the list is called a singly linked list.
 An example of such a list is shown in Figure , Note that only
one variable p is used to access any node in the list. The
last node on the list can be recognized by the null
reference field.
singly linked listsingly linked list
01/29/18 BY MS. SHAISTA QADIR 5
singly linked listsingly linked list
01/29/18 BY MS. SHAISTA QADIR 6
•Each node in the list in Figure is an instance of the following
class definition:
public class IntSLLNode
{
public int info;
public IntSLLNode next;
public IntSLLNode(int i)
{
this(i,null);
}
public IntSLLNode(int i, IntSLLNode n)
{
info = i; next = n;
}
}
singly linked listsingly linked list
01/29/18 BY MS. SHAISTA QADIR 7
• A node includes two data fields: info and next. The info field
is used to store information, and this field is important to the
user.
• The next field is used to link together nodes to form a linked
list.
• The definition of a node also includes two constructors.
• The second constructor takes two arguments, one to initialize
the info field and another to initialize the next field.
• The first constructor takes one argument and is defined in
terms of the second constructor.
• The reserved word this is used to refer to the current object,
and this word can appear anywhere this object can be used.
insertiOn in singly linkedinsertiOn in singly linked
listlist
01/29/18 BY MS. SHAISTA QADIR 8
• Insertion In Singly Linked List
a) At the Beginning
b) At the End
c) At an Arbitrary Location (any where between head and tail)
01/29/18 BY MS. SHAISTA QADIR 9
a) Adding a new node at the beginning of a linked list :
insertion in singly linkedinsertion in singly linked
listlist
01/29/18 BY MS. SHAISTA QADIR 10
a) Adding a new node at the beginning of a linked list :
1. An empty node is created.
2. The node’s info field is initialized to a particular integer.
3. The next field becomes a reference to the first node in the
list.
4. The new node precedes all the nodes on the list.
insertion in singly linkedinsertion in singly linked
listlist
01/29/18 BY MS. SHAISTA QADIR 11
b) Adding a new node at the end of a linked list :
insertion in singly linkedinsertion in singly linked
listlist
01/29/18 BY MS. SHAISTA QADIR 12
b) Adding a new node at the end of a linked list :
1. An empty node is created.
2. The node’s info field is initialized to an integer el.
3. The next field is set to null.
4. The next field of the last node of the list is a reference to
the newly created node.
5. The new node follows all the nodes of the list.
insertion in singly linkedinsertion in singly linked
listlist
01/29/18 BY MS. SHAISTA QADIR 13
c) Insertion at an arbitrary location
• To insert the node after the node with element 20
insertion in singly linkedinsertion in singly linked
listlist
01/29/18 BY MS. SHAISTA QADIR 14
c) Insertion at an arbitrary location
• To insert the node after the node with element 20
If (START == NULL) Then
2. Print: Linked-List is empty. It must have at least one node
3. Else
4. Set PTR = START, NEW = START
5. Repeat While (PTR != NULL)
6. If (PTR->INFO == N) Then
7. NEW = New Node
8. NEW->INFO = ITEM
9. NEW->LINK = PTR->LINK
10. PTR->LINK = NEW
11. Print: ITEM inserted
12. ELSE
13. PTR = PTR->LINK
[End of Step 6 If]
[End of While Loop]
[End of Step 1 If]
14. Exit .
Here START is a pointer variable
which contains the address of first
node.
NEW is a pointer variable which will
contain address of new node.
N is the value after which new node
is to be inserted and ITEM is the
value to be inserted.
INFO is data in the node and LINK is
address of next node.
insertion in singly linkedinsertion in singly linked
listlist
01/29/18 BY MS. SHAISTA QADIR 15
• Deletion algorithms (singly linked lists)
a) From the Beginning (head).
b) From the End.
c) At an Arbitrary Location (any where in the center).
deletion from singlydeletion from singly
linked listlinked list
01/29/18 BY MS. SHAISTA QADIR 16
deletion from singlydeletion from singly
linked listlinked list
a) Algorithm to delete a node from head
01/29/18 BY MS. SHAISTA QADIR 17
deletion from singlydeletion from singly
linked listlinked list
a) Algorithm to delete a node from head
deleteFromHead():
1.In this operation the information from the first node is
temporarily stored in a local variable el.
2. Then head is reset so that the second node becomes the
first node.
3. The former first node is abandoned to be processed later
by the garbage collector.
PROGRAM LOGIC:
public int deleteFromHead()
{
int el=head.info;
if(head==tail)
head=tail=null;
else head=head.next;
return el;
}
}
01/29/18 BY MS. SHAISTA QADIR 18
deletion from singlydeletion from singly
linked listlinked list
b) Algorithm to delete a node from the end.
01/29/18 BY MS. SHAISTA QADIR 19
deletion from singlydeletion from singly
linked listlinked list
b) Algorithm to delete a node from the end.
deleteFromTail ():
1. In this operation the information from the last node is temporarily
stored in a local variable el
2. Then after removing a node, tail should refer to the new tail of the
list, that is, tail has to be moved backward by one node.
3. The variable tmp is initialized to the head of the list, and then in
each iteration of the loop, it is advanced to the next node.
PROGRAM LOGIC:
public int deleteFromTail()
{ int el=tail.info;
if(head==tail)
head=tail=null;
Else
{ IntNode tmp;
for(tmp=head; tmp.next!=tail; tmp=tmp.next); // loop to find the node before tail
tail=tmp; tail.next=null;
}
return el;
}
01/29/18 BY MS. SHAISTA QADIR 20
deletion from singlydeletion from singly
linked listlinked list
c) Deleting a node from any arbitrary location linked list :
01/29/18 BY MS. SHAISTA QADIR 21
deletion from singlydeletion from singly
linked listlinked list
c) Deleting a node from any arbitrary location linked list :
1. If (START == NULL) Then
2. Print: Linked-List is empty. It must have at least one node
3. Else If (START->INFO == ITEM) Then
4. PTR = START
5. START = START->LINK
6. Delete PTR
7. Else
8. PTR = START, PREV = START
9. Repeat While (PTR != NULL)
10. If (PTR->INFO == ITEM) Then
11. PREV->LINK = PTR->LINK
12. Delete PTR
13. Else
14. PREV = PTR
15. PTR = PTR->LINK
[End of Step 10 If]
[End of While Loop]
16. Print: ITEM deleted
[End of Step 1 If]
17. Exit
•Here START is a pointer
variable which contains the
address of first node.PTR is a
pointer
•variable which will contain
address of node to be deleted.
PREV is the pointer variable
which
•contains the address of
previous node. ITEM is the
value to be deleted.
01/29/18 BY MS. SHAISTA QADIR 22
searching a singly linkedsearching a singly linked
listlist
Searching an element from any arbitrary location linked list :
1. Set PTR = START, LOC = 1
2. Repeat While (PTR != NULL)
3. If (ITEM == PTR->INFO) Then
4. Print: ITEM is present at location LOC
5. Return
6. Else
7. PTR = PTR->LINK
8. LOC = LOC + 1
9. [End of If]
10. [End of While Loop]
11. Print: ITEM is not present in the list
12. Exit
• The search operation
explained is to find if a data
is present in the
Linked List or not.
• START is the pointer
variable which corresponds
to the address of
first node.
• ITEM is the value to be
searched. LOC is used to
give node no.
which contains ITEM.
01/29/18 BY MS. SHAISTA QADIR 23
doubly linked listdoubly linked list
• It is linked data structure which has two reference fields. One
to successor (next node) and another to predecessor (previous
node).
• It has the advantage of forward and reverse traversal as against
Singly Linked List
01/29/18 BY MS. SHAISTA QADIR 24
insertion in doubly linkedinsertion in doubly linked
listlist
Adding a new node at the end of a doubly linked list :
01/29/18 BY MS. SHAISTA QADIR 25
insertion in Doubly linkeDinsertion in Doubly linkeD
listlist
Adding a new node at the end of a doubly linked list :
1. An empty node is created.
2. The node’s info field is initialized to an integer el.
3. The next field is set to null.
4. The previous field is set to the tail.
3. Tail is set to reference the new node.
4. To make the new node accessible from the predecessor, the next
field
of predecessor is set to refer the new node.
PROGRAM LOGIC:
public void addToTail(int el)
{ if(!isEmpty()) // returns true if the list is not empty by checking if head==null;
{
tail=new IntDLLNode(el,null,tail);
tail.prev.next=tail;
}
else
head=tail=new IntDLLNode(el);
}
01/29/18 BY MS. SHAISTA QADIR 26
Algorithm to delete a node from Tail
Deletion from a DoublyDeletion from a Doubly
linkeD listlinkeD list
01/29/18 BY MS. SHAISTA QADIR 27
Deletion from a DoublyDeletion from a Doubly
linkeD listlinkeD list
Algorithm to delete a node from Tail
removeFromTail ():
1. In this operation the information from the last node is temporarily
stored in a local variable el
2. Then after removing a node from tail, tail should refer to the new tail
of the list, that is, tail has to be moved backward by one node.
3. If there is only one in the list, after removing the tail node, the list
becomes empty or head=tail=null.
PROGRAM LOGIC:
public int removeFromTail()
{ int el=tail.info;
if(head==tail) // if only one node in the
list;
head=tail=null;
else
{ // if more than one node in the
list;
tail=tail.prev; tail.next=null;
}
01/29/18 BY MS. SHAISTA QADIR 28
Doubly linkeD listDoubly linkeD list
Methods for operating at the beginning of doubly linked list are easily
obtained from the methods discussed before.
EXERCISE
Write algorithms with illustration to
i) Insert a node as a head in a Doubly Linked List
ii) Insert a node at an arbitrary location in Doubly Linked List
iii) Delete a node from the head of Doubly Linked List
iv) Delete a node from arbitrary location in Doubly Linked List
01/29/18 BY MS. SHAISTA QADIR 29
Searching an element from any arbitrary location of doubly linked list :
1. Set PTR = START, LOC = 1
2. Repeat While (PTR != NULL)
3. If (ITEM == PTR->INFO) Then
4. Print: ITEM is present at location LOC
5. Return
6. Else
7. PTR = PTR->NEXT
8. LOC = LOC + 1
9. [End of If]
10. [End of While Loop]
11. Print: ITEM is not present in the list
12. Exit
searching a Doubly linkeDsearching a Doubly linkeD
listlist
• The search operation explained
is to find if a data is present in
the Linked List or not.
• START is the pointer variable
which corresponds to the
address of first node.
• ITEM is the value to be
searched.
• NEXT corresponds to the next
field of a node in Doubly Linked
List
• LOC corresponds to the node
number in which ITEM is
present
01/29/18 BY MS. SHAISTA QADIR 30
• Circular linked list is type of linked list where address of 1st
node is stored in the link part of last node.
circular linkeD listcircular linkeD list
01/29/18 BY MS. SHAISTA QADIR 31
• Circular linked list is type of linked list where address of 1st
node is stored in the link part of last node.
circular linkeD listcircular linkeD list
public void addToTail(int el)
{
if (isEmpty())
{
tail = new IntSLLNode(el);
tail.next = tail;
}
else {
tail.next = new IntSLLNode(el,tail.next);
tail = tail.next;
}
}
01/29/18 BY MS. SHAISTA QADIR 32
THANK YOUTHANK YOU

linked list

  • 1.
    Chapter 3Chapter 3 linkedlistslinked lists 01/29/18 BY MS. SHAISTA QADIR 1 PRESENTED BY Shaista Qadir Lecturer king khalid university
  • 2.
    COntentsCOntents  linked list singly linked list  insertiOn in singly linked list  deletiOn in singly linked list  searChing a singly linked list  dOubly linked list  insertiOn frOm dOubly linked list  deletiOn frOm dOubly linked list  searChing a dOubly linked list  CirCular linked list 01/29/18 BY MS. SHAISTA QADIR 2
  • 3.
    linked listlinked list 01/29/18BY MS. SHAISTA QADIR 3  A linked structure is a collection of nodes storing data and links to other nodes.  In this way, nodes can be located anywhere in memory, and passing from one node of the linked structure to another is accomplished by storing the reference(s) to other node(s) in the structure.  Although linked structures can be implemented in a variety of ways, the most flexible implementation is by using a separate object for each node.  Types of Linked List: 1. Singly Linked List 2. Doubly Linked List 3. Circular Linked List
  • 4.
    singly linked listsinglylinked list 01/29/18 BY MS. SHAISTA QADIR 4  A sequence of nodes, each node holding some information and a reference to another node in the list.  If a node has a link only to its successor in this sequence, the list is called a singly linked list.  An example of such a list is shown in Figure , Note that only one variable p is used to access any node in the list. The last node on the list can be recognized by the null reference field.
  • 5.
    singly linked listsinglylinked list 01/29/18 BY MS. SHAISTA QADIR 5
  • 6.
    singly linked listsinglylinked list 01/29/18 BY MS. SHAISTA QADIR 6 •Each node in the list in Figure is an instance of the following class definition: public class IntSLLNode { public int info; public IntSLLNode next; public IntSLLNode(int i) { this(i,null); } public IntSLLNode(int i, IntSLLNode n) { info = i; next = n; } }
  • 7.
    singly linked listsinglylinked list 01/29/18 BY MS. SHAISTA QADIR 7 • A node includes two data fields: info and next. The info field is used to store information, and this field is important to the user. • The next field is used to link together nodes to form a linked list. • The definition of a node also includes two constructors. • The second constructor takes two arguments, one to initialize the info field and another to initialize the next field. • The first constructor takes one argument and is defined in terms of the second constructor. • The reserved word this is used to refer to the current object, and this word can appear anywhere this object can be used.
  • 8.
    insertiOn in singlylinkedinsertiOn in singly linked listlist 01/29/18 BY MS. SHAISTA QADIR 8 • Insertion In Singly Linked List a) At the Beginning b) At the End c) At an Arbitrary Location (any where between head and tail)
  • 9.
    01/29/18 BY MS.SHAISTA QADIR 9 a) Adding a new node at the beginning of a linked list : insertion in singly linkedinsertion in singly linked listlist
  • 10.
    01/29/18 BY MS.SHAISTA QADIR 10 a) Adding a new node at the beginning of a linked list : 1. An empty node is created. 2. The node’s info field is initialized to a particular integer. 3. The next field becomes a reference to the first node in the list. 4. The new node precedes all the nodes on the list. insertion in singly linkedinsertion in singly linked listlist
  • 11.
    01/29/18 BY MS.SHAISTA QADIR 11 b) Adding a new node at the end of a linked list : insertion in singly linkedinsertion in singly linked listlist
  • 12.
    01/29/18 BY MS.SHAISTA QADIR 12 b) Adding a new node at the end of a linked list : 1. An empty node is created. 2. The node’s info field is initialized to an integer el. 3. The next field is set to null. 4. The next field of the last node of the list is a reference to the newly created node. 5. The new node follows all the nodes of the list. insertion in singly linkedinsertion in singly linked listlist
  • 13.
    01/29/18 BY MS.SHAISTA QADIR 13 c) Insertion at an arbitrary location • To insert the node after the node with element 20 insertion in singly linkedinsertion in singly linked listlist
  • 14.
    01/29/18 BY MS.SHAISTA QADIR 14 c) Insertion at an arbitrary location • To insert the node after the node with element 20 If (START == NULL) Then 2. Print: Linked-List is empty. It must have at least one node 3. Else 4. Set PTR = START, NEW = START 5. Repeat While (PTR != NULL) 6. If (PTR->INFO == N) Then 7. NEW = New Node 8. NEW->INFO = ITEM 9. NEW->LINK = PTR->LINK 10. PTR->LINK = NEW 11. Print: ITEM inserted 12. ELSE 13. PTR = PTR->LINK [End of Step 6 If] [End of While Loop] [End of Step 1 If] 14. Exit . Here START is a pointer variable which contains the address of first node. NEW is a pointer variable which will contain address of new node. N is the value after which new node is to be inserted and ITEM is the value to be inserted. INFO is data in the node and LINK is address of next node. insertion in singly linkedinsertion in singly linked listlist
  • 15.
    01/29/18 BY MS.SHAISTA QADIR 15 • Deletion algorithms (singly linked lists) a) From the Beginning (head). b) From the End. c) At an Arbitrary Location (any where in the center). deletion from singlydeletion from singly linked listlinked list
  • 16.
    01/29/18 BY MS.SHAISTA QADIR 16 deletion from singlydeletion from singly linked listlinked list a) Algorithm to delete a node from head
  • 17.
    01/29/18 BY MS.SHAISTA QADIR 17 deletion from singlydeletion from singly linked listlinked list a) Algorithm to delete a node from head deleteFromHead(): 1.In this operation the information from the first node is temporarily stored in a local variable el. 2. Then head is reset so that the second node becomes the first node. 3. The former first node is abandoned to be processed later by the garbage collector. PROGRAM LOGIC: public int deleteFromHead() { int el=head.info; if(head==tail) head=tail=null; else head=head.next; return el; } }
  • 18.
    01/29/18 BY MS.SHAISTA QADIR 18 deletion from singlydeletion from singly linked listlinked list b) Algorithm to delete a node from the end.
  • 19.
    01/29/18 BY MS.SHAISTA QADIR 19 deletion from singlydeletion from singly linked listlinked list b) Algorithm to delete a node from the end. deleteFromTail (): 1. In this operation the information from the last node is temporarily stored in a local variable el 2. Then after removing a node, tail should refer to the new tail of the list, that is, tail has to be moved backward by one node. 3. The variable tmp is initialized to the head of the list, and then in each iteration of the loop, it is advanced to the next node. PROGRAM LOGIC: public int deleteFromTail() { int el=tail.info; if(head==tail) head=tail=null; Else { IntNode tmp; for(tmp=head; tmp.next!=tail; tmp=tmp.next); // loop to find the node before tail tail=tmp; tail.next=null; } return el; }
  • 20.
    01/29/18 BY MS.SHAISTA QADIR 20 deletion from singlydeletion from singly linked listlinked list c) Deleting a node from any arbitrary location linked list :
  • 21.
    01/29/18 BY MS.SHAISTA QADIR 21 deletion from singlydeletion from singly linked listlinked list c) Deleting a node from any arbitrary location linked list : 1. If (START == NULL) Then 2. Print: Linked-List is empty. It must have at least one node 3. Else If (START->INFO == ITEM) Then 4. PTR = START 5. START = START->LINK 6. Delete PTR 7. Else 8. PTR = START, PREV = START 9. Repeat While (PTR != NULL) 10. If (PTR->INFO == ITEM) Then 11. PREV->LINK = PTR->LINK 12. Delete PTR 13. Else 14. PREV = PTR 15. PTR = PTR->LINK [End of Step 10 If] [End of While Loop] 16. Print: ITEM deleted [End of Step 1 If] 17. Exit •Here START is a pointer variable which contains the address of first node.PTR is a pointer •variable which will contain address of node to be deleted. PREV is the pointer variable which •contains the address of previous node. ITEM is the value to be deleted.
  • 22.
    01/29/18 BY MS.SHAISTA QADIR 22 searching a singly linkedsearching a singly linked listlist Searching an element from any arbitrary location linked list : 1. Set PTR = START, LOC = 1 2. Repeat While (PTR != NULL) 3. If (ITEM == PTR->INFO) Then 4. Print: ITEM is present at location LOC 5. Return 6. Else 7. PTR = PTR->LINK 8. LOC = LOC + 1 9. [End of If] 10. [End of While Loop] 11. Print: ITEM is not present in the list 12. Exit • The search operation explained is to find if a data is present in the Linked List or not. • START is the pointer variable which corresponds to the address of first node. • ITEM is the value to be searched. LOC is used to give node no. which contains ITEM.
  • 23.
    01/29/18 BY MS.SHAISTA QADIR 23 doubly linked listdoubly linked list • It is linked data structure which has two reference fields. One to successor (next node) and another to predecessor (previous node). • It has the advantage of forward and reverse traversal as against Singly Linked List
  • 24.
    01/29/18 BY MS.SHAISTA QADIR 24 insertion in doubly linkedinsertion in doubly linked listlist Adding a new node at the end of a doubly linked list :
  • 25.
    01/29/18 BY MS.SHAISTA QADIR 25 insertion in Doubly linkeDinsertion in Doubly linkeD listlist Adding a new node at the end of a doubly linked list : 1. An empty node is created. 2. The node’s info field is initialized to an integer el. 3. The next field is set to null. 4. The previous field is set to the tail. 3. Tail is set to reference the new node. 4. To make the new node accessible from the predecessor, the next field of predecessor is set to refer the new node. PROGRAM LOGIC: public void addToTail(int el) { if(!isEmpty()) // returns true if the list is not empty by checking if head==null; { tail=new IntDLLNode(el,null,tail); tail.prev.next=tail; } else head=tail=new IntDLLNode(el); }
  • 26.
    01/29/18 BY MS.SHAISTA QADIR 26 Algorithm to delete a node from Tail Deletion from a DoublyDeletion from a Doubly linkeD listlinkeD list
  • 27.
    01/29/18 BY MS.SHAISTA QADIR 27 Deletion from a DoublyDeletion from a Doubly linkeD listlinkeD list Algorithm to delete a node from Tail removeFromTail (): 1. In this operation the information from the last node is temporarily stored in a local variable el 2. Then after removing a node from tail, tail should refer to the new tail of the list, that is, tail has to be moved backward by one node. 3. If there is only one in the list, after removing the tail node, the list becomes empty or head=tail=null. PROGRAM LOGIC: public int removeFromTail() { int el=tail.info; if(head==tail) // if only one node in the list; head=tail=null; else { // if more than one node in the list; tail=tail.prev; tail.next=null; }
  • 28.
    01/29/18 BY MS.SHAISTA QADIR 28 Doubly linkeD listDoubly linkeD list Methods for operating at the beginning of doubly linked list are easily obtained from the methods discussed before. EXERCISE Write algorithms with illustration to i) Insert a node as a head in a Doubly Linked List ii) Insert a node at an arbitrary location in Doubly Linked List iii) Delete a node from the head of Doubly Linked List iv) Delete a node from arbitrary location in Doubly Linked List
  • 29.
    01/29/18 BY MS.SHAISTA QADIR 29 Searching an element from any arbitrary location of doubly linked list : 1. Set PTR = START, LOC = 1 2. Repeat While (PTR != NULL) 3. If (ITEM == PTR->INFO) Then 4. Print: ITEM is present at location LOC 5. Return 6. Else 7. PTR = PTR->NEXT 8. LOC = LOC + 1 9. [End of If] 10. [End of While Loop] 11. Print: ITEM is not present in the list 12. Exit searching a Doubly linkeDsearching a Doubly linkeD listlist • The search operation explained is to find if a data is present in the Linked List or not. • START is the pointer variable which corresponds to the address of first node. • ITEM is the value to be searched. • NEXT corresponds to the next field of a node in Doubly Linked List • LOC corresponds to the node number in which ITEM is present
  • 30.
    01/29/18 BY MS.SHAISTA QADIR 30 • Circular linked list is type of linked list where address of 1st node is stored in the link part of last node. circular linkeD listcircular linkeD list
  • 31.
    01/29/18 BY MS.SHAISTA QADIR 31 • Circular linked list is type of linked list where address of 1st node is stored in the link part of last node. circular linkeD listcircular linkeD list public void addToTail(int el) { if (isEmpty()) { tail = new IntSLLNode(el); tail.next = tail; } else { tail.next = new IntSLLNode(el,tail.next); tail = tail.next; } }
  • 32.
    01/29/18 BY MS.SHAISTA QADIR 32 THANK YOUTHANK YOU