You are on page 1of 32

Singly Linked Lists

- Ed. 2, 3: Chapter 4
- Ed. 4.: Chapter 3
Wiley Gary

Bill

McFee

Scott Scott wants to find information about


computer networks.
D e f i n it i o n : A l i n k e d l i s t i s a c o l l e c i t o n o f n o d e s t h a t
t o g e t h e r f o r m a l i n e a r o r d e r in g .

n o d e : A c o m p o u n d o b je c t t h a t s t o r e s a r e f e r e n c e to a n
e l e m e n t a n d a r e f e r e n c e , c a ll e d n e x t , t o a n o t h e r n o d e .

E le m e n t

N ode
R e fe re n c e to a n
e le m e n t
R e fe re n c e to next
a n o th e r n o d e
head

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to

lin k : T h e n e x t re fe re n c e in s id e a n o d e is a lin k o r p o in te r to
a n o th e r n o d e .
W e c a n s ta rt fro m a g iv e n n o d e , a n d m o v e fro m it to th e n e x t
a n d s o o n . T h is is c a lle d lin k h o p p in g o r p o in te r h o p p in g .

head

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to
h e a d : T h e firs t n o d e o f a lin k e d lis t
ta il: T h e la s t n o d e o f a lin k e d lis t - it h a s a n u ll n e x t re fe re n c e .

head tail

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to

S u c h a lin k e d lis t is c a lle d a s in g ly lin k e d lis t.


Illustration of a linked list in memory:

50B0

5110 Toronto

50A0

5100
5090
node pointer to a
50F0
5080
5070
50D0 next node
50E0 Rome
0
5070 5110
Seattle
pointer to
50D0
5060
5080
50E0 an element
Baltimore
5060 50C0
50C0
5050
50B0

5110 Toronto

50A0

5100 node pointer to a


5090 next node
5070 50F0
5080 50D0
pointer to
0
50E0 Rome
an element
5070 5110
Seattle
5080 50D0
5060 50E0
Baltimore
5060 50C0
50C0
5050
head
50B0

5110 Toronto

50A0

5100
5090

50F0 node pointer to a


5070
5080 50D0 next node
50E0 Rome
0
5070 5110 pointer to
5080 50D0
Seattle
an element
5060 50E0

Baltimore
5060 50C0
50C0
5050
head
50B0

5110 Toronto

50A0

5100
node pointer to a
5090
next node
5070 50F0
5080 50D0

50E0 Rome pointer to


0
5070 5110 an element
Seattle
5080 50D0
5060 50E0
Baltimore
5060 50C0
50C0
5050
head
50B0

5110 Toronto

50A0

5100
5090

5070 50F0
5080 50D0

50E0 Rome
0
5070 5110
Seattle
5080 50D0
5060 50E0

Baltimore
5060 50C0
50C0
5050
head
Singly Linked Lists and Arrays
Singly linked list Array
Elements are stored in linear Elements are stored in linear
order, accessible with links. order, accessible with an
index.

Do not have a fixed size. Have a fixed size.

Cannot access the previous Can access the previous


element directly. element easily.

No binary search. Binary search.


Class Node
Here is an implementation of nodes in Java:

public class Node {


private Object element;
private Node next;

public Node() {
this( null, null );
}

public Node( Object e, Node n ) {


element = e;
next = n;
}
Object getElement() {
return element
}

Node getNext() {
return next;
}

void setElement( Object newElem ) {


element = newElem;
}

void setNext( Node newNext ) {


next = newNext;
}
}
Insertion of an Element at the
Head
B efore the insertion:

hea d

next next next

ele m ent ele m ent ele m ent


R om e S eattle To ronto
H ave a new node:

head

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to

Node x = new Node();


x.setElement(new String(“Baltimore”));
The following statement is not correct:
x.element = new String(“Baltimore”));
A fte r th e in s e rtio n :

head

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to

x.setNext(head);
head = x;
Deleting an Element at the Head
B efo re th e deletio n:

h ea d

n ex t n ex t n ex t n ex t

ele m en t ele m en t ele m en t ele m en t


B altim ore R om e S eattle T o ronto
R e m o v e th e n o d e fro m th e lis t:

head

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to

head = head.getNext();
A fte r th e d e le tio n :

head

next next next

e le m e n t e le m e n t e le m e n t
R om e S e a ttle T o ro n to
Insertion of an Element at the
Tail
Before the insertion:

head tail

next next next

element element element


Rome Seattle Toronto
H ave a new node:

head ta il

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
R om e S e a t t le T o ro n to B a l t im o r e

Node x = new Node( );


x.setElement(new String(“Baltimore”));
x.setNext(null);
tail.setNext(x);
tail = x;
A fte r th e in s e rtio n :

head ta il

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
R om e S e a tt le T o ro n to B a lt im o r e
How to keep “head” and “tail”?

public class Head_and_Tail {


Node head;
Node tail;

Head_and_Tail(Node x, Node y)
{
head = x;
tail = y;
}
}
How to keep “head” and “tail”?

public class GeneratingList {


Node head = null;
Node tail = null;

Public Head_and_Tail linked_list () {


Node x = null;
for (int i = 0; i < 10; i++)
{x = new Node(); x.setElement(new Integer(i));
if (i == 0 ) {x.setNext(null); tail = x;}
else x.setNext(head);
head = x;
}
return new Head_and_Tail(head, tail);}
}
Deleting an Element at the Tail
Deletion of an element at the tail of a singly linked list takes
more effort.

The difficulty is related with the fact that the last node does not
have a link to the previous node which will become the new
tail of the list.
Wiley Gary

Bill

McFee

Scott Scott: Who is McFee?


Gary: I don’t know.
B e fo re th e d e le tio n :

head ta il

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
R om e S e a tt le T o ro n to B a lt im o r e
R e m o v e th e n o d e : H o w c a n w e fin d th e n e w ta il?

head ta il ?

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
R om e S e a tt le T o ro n to B a lt i m o r e

should be removed
How to insert a new node in the middle of a singly linked
list?

How to remove a node which in the middle of a singly


linked list?
Data Structure Exercises 5.1
Write a Java program to create a linked list as shown below.

head tail

…… 

0 1 9
public class ListStack implements Stack {
private int size = 0;
private Node head = null;

public ListStack() {}
 
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void push( Object obj ){
Node x = new Node();
x.setElement(obj); x.setNet(head);
head = x; size++;
}
public object pop() throws StackEmptyException {
if( isEmpty() )
throw new StackEmptyException( "Stack is Empty." );

Object elem = head;


head = getNext(head); size--;
return elem;
}

public object top() throws StackEmptyException {


if( isEmpty() )
throw new StackEmptyException( "Stack is Empty." );
return head;;
}
}

You might also like