Singly Linked Lists: - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3
Singly Linked Lists: - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3
- Ed. 2, 3: Chapter 4
- Ed. 4.: Chapter 3
Wiley Gary
Bill
McFee
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
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
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
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
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
5110 Toronto
50A0
5100
5090
Baltimore
5060 50C0
50C0
5050
head
50B0
5110 Toronto
50A0
5100
node pointer to a
5090
next node
5070 50F0
5080 50D0
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.
public Node() {
this( null, null );
}
Node getNext() {
return next;
}
hea d
head
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
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
head
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
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
head ta il
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
head ta il
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”?
Head_and_Tail(Node x, Node y)
{
head = x;
tail = y;
}
}
How to keep “head” and “tail”?
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
head ta il
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 ?
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?
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." );