Singly Linked Lists
Steve Paks
Curriculum Model

A

A

A

A

A

CS

Here I am
A

A

A

CS
A
Java Language Base

CS : Computer
Science
A : Application
Singly Linked Lists
โ€ข an ordered sequence of nodes with links
โ€“ the node do not reside in sequential locations
โ€“ the locations of the nodes may change on different runs

bat

cat

cat

sat

vat

NUL
L
Singly Linked Lists(Contโ€™d)
โ€ข Insertion
โ€“ get a node that is currently unused; let its address be paddr.
โ€“ set the data field of this node to mat
โ€“ set paddrโ€™s link field to point to the address found in the link field of the node
containing cat.
โ€“ set the link field of the node containing cat to point paddr.

ptr
bat

cat

sat

mat

vat

NULL
Singly Linked Lists(Contโ€™d)
โ€ข Deletion
โ€“ find the node that immediately precedes mat, which is cat
โ€“ set the link field of this node to point to matโ€™s link field

ptr
bat

cat

mat

sat

vat

NULL
Singly Linked Lists(Contโ€™d)
โ€ข Declarations
Class ListNode{
char data;
ListNode link = null;
}

โ€ข List insertion

(a)

first
(b)
first
first

second

first

20
second

first

20
second

first

20
second

first

20
second

(c)

โ€“ create a two-node list

ListNode create2(){
ListNode first, second;
first = new ListNode();
โˆ™โˆ™โˆ™โˆ™(a)
second = new ListNode(); โˆ™โˆ™โˆ™โˆ™(b)
second.link = null; โˆ™โˆ™โˆ™โˆ™(c)
second.data = 20; โˆ™โˆ™โˆ™โˆ™(d)
โˆ™โˆ™โˆ™โˆ™(e)
first.data = 10;
first.link = second; โˆ™โˆ™โˆ™โˆ™(f)
return first; โˆ™โˆ™โˆ™โˆ™(g)
}

second

(d)
(e)

(f)
ptr
(g)

10
10
10
Singly Linked Lists(Contโ€™d)
โ€ข List insertion(Contโ€™d)
โ€“ create a two-node list
void insert(){
ListNode temp = new ListNode(); โˆ™โˆ™โˆ™โˆ™(a)
temp.data = 50;
โˆ™โˆ™โˆ™โˆ™(b)
if(this.link != null){
temp.link = this.link.link; โˆ™โˆ™โˆ™โˆ™(c)
this.link.link = temp; โˆ™โˆ™โˆ™โˆ™(d)
} else {
temp.link = null; โˆ™โˆ™โˆ™โˆ™(cโ€™)
this.link = temp; โˆ™โˆ™โˆ™โˆ™(dโ€™)
}
}
Singly Linked Lists(Contโ€™d)
โ€ข List insertion(Contโ€™d)
โ€“ create a two-node list(Contโ€™d)

(b)

(a)
temp

50
temp

temp

ptr

(c)

10
first

50
temp

ptr

(d)

20
second

10
first

20
second
50
temp

(cโ€™)

ptr
(dโ€™)

50
temp
50
temp

Singly linked lists

  • 1.
  • 2.
    Curriculum Model A A A A A CS Here Iam A A A CS A Java Language Base CS : Computer Science A : Application
  • 3.
    Singly Linked Lists โ€ขan ordered sequence of nodes with links โ€“ the node do not reside in sequential locations โ€“ the locations of the nodes may change on different runs bat cat cat sat vat NUL L
  • 4.
    Singly Linked Lists(Contโ€™d) โ€ขInsertion โ€“ get a node that is currently unused; let its address be paddr. โ€“ set the data field of this node to mat โ€“ set paddrโ€™s link field to point to the address found in the link field of the node containing cat. โ€“ set the link field of the node containing cat to point paddr. ptr bat cat sat mat vat NULL
  • 5.
    Singly Linked Lists(Contโ€™d) โ€ขDeletion โ€“ find the node that immediately precedes mat, which is cat โ€“ set the link field of this node to point to matโ€™s link field ptr bat cat mat sat vat NULL
  • 6.
    Singly Linked Lists(Contโ€™d) โ€ขDeclarations Class ListNode{ char data; ListNode link = null; } โ€ข List insertion (a) first (b) first first second first 20 second first 20 second first 20 second first 20 second (c) โ€“ create a two-node list ListNode create2(){ ListNode first, second; first = new ListNode(); โˆ™โˆ™โˆ™โˆ™(a) second = new ListNode(); โˆ™โˆ™โˆ™โˆ™(b) second.link = null; โˆ™โˆ™โˆ™โˆ™(c) second.data = 20; โˆ™โˆ™โˆ™โˆ™(d) โˆ™โˆ™โˆ™โˆ™(e) first.data = 10; first.link = second; โˆ™โˆ™โˆ™โˆ™(f) return first; โˆ™โˆ™โˆ™โˆ™(g) } second (d) (e) (f) ptr (g) 10 10 10
  • 7.
    Singly Linked Lists(Contโ€™d) โ€ขList insertion(Contโ€™d) โ€“ create a two-node list void insert(){ ListNode temp = new ListNode(); โˆ™โˆ™โˆ™โˆ™(a) temp.data = 50; โˆ™โˆ™โˆ™โˆ™(b) if(this.link != null){ temp.link = this.link.link; โˆ™โˆ™โˆ™โˆ™(c) this.link.link = temp; โˆ™โˆ™โˆ™โˆ™(d) } else { temp.link = null; โˆ™โˆ™โˆ™โˆ™(cโ€™) this.link = temp; โˆ™โˆ™โˆ™โˆ™(dโ€™) } }
  • 8.
    Singly Linked Lists(Contโ€™d) โ€ขList insertion(Contโ€™d) โ€“ create a two-node list(Contโ€™d) (b) (a) temp 50 temp temp ptr (c) 10 first 50 temp ptr (d) 20 second 10 first 20 second 50 temp (cโ€™) ptr (dโ€™) 50 temp 50 temp