Title: CS 162 Spring 2005
1CS 162 - Spring 2005
2Characteristics of Linked Lists
- Elements are held in objects termed Links
- Links are 1-1 with elements, allocated and
released as necessary. - Each link points to next link in sequence,
sometimes to previous link. - Elements can be easily inserted into middle
- Lots of variations on a simple idea
3A typical Link Class
- private static class Link
-
- Link(Object v,Link n)
- value v next n
- public Object value
- public Link next
4Typically an inner class
- Class List
-
- private static class Link
-
-
-
- Private - used only in the class
- Static - doesnt need to reference surrounding
class
5Some variations in Linked lists
- Use null as terminator, or special value for end
- Use single or double links?
- Pointer to first element, or pointer to first and
last
6Pointer to head, null terminator
firstLink
8
4
6
3
Null
7Pointer to head, Sentinel
firstLink
8
4
6
3
Sentinel
8Head Pointer, Tail Pointer, Sentinel
LastLink, firstLink
8
4
6
3
Sentinel
9Head Pointer, Tail Pointer, Null terminator
LastLink, firstLink
8
4
6
3
Null
10Head Pointer, Tail Pointer, Null terminator,
Double Links
LastLink, firstLink
8
4
6
3
Null
11Simplest Example, List Stack
- List stack is the simplest data structure that
can be implemented using linked list idea - Keep pointer to first element (null if empty)
- Elements are added or removed from front
- can only access first element
12Code for list stack
- class ListStack
- private Stacklink firstLink null
- public boolean isEmpty()return firstLink
null - public void push (Object v)
- firstLink new Link(v, firstLink)
- public void pop ()
- if (isEmpty())
- throw new NoSuchElementException()
- firstLink firstLink.link
-
13Pointer to head, null terminator
firstLink
8
4
6
3
Null
14How fast is List Stack?
- Compare to VectorStack
- push - list O(1) always, vector(1) expected
- pop - list O(1) always, vector same
- top - list O(1) always, vector same
- In practice vector is slightly faster in real
timinings.
15But what about queues?
- Remember a vector queue is hard to do, because
you can't add to the beginning without sliding
things up. - With lists it is easy. Just keep a pointer to
both the front AND the back. - Elements added to the back, removed from front
16Head Pointer, Tail Pointer, Null terminator,
Double Links
LastLink, firstLink
8
4
6
3
Null
17Class Structure for List Queue
- class ListQueue
- private Link firstLink null
- private Link lastLink null
- public boolean isEmpty() ...
- public void enqueue (Object newelement) ...
- public Object frontElement() ...
- public void dequeue () ...
18Elements are Added to end
- class Listqueue
- public void enqueue(Object newElement)
- lastLink new Link(newElement, lastLink)
- if (firstLink null) // ie, list was
empty - firstLink lastLink
19Empty test - just see if link is null
- public class ListQueue
- public boolean isEmpty ()
-
- return firstLink null
-
20Elements Removed from Front
- class ListQueue
- public void dequeue ()
- if (isEmpty())
- throw new NoSuchElementException()
- firstLink firstLink.link
- if (firstLink null) // ie, removing last
- lastLink null
21What about a deque?
- What if we want to add and remove elements from
both front and back? - Need to use links going both forward and
backwards - Makes adding a new link harder, as must maintain
both forward and backward links.
22Sentinel
- Code becomes much easier if we use a Sentinel - a
special node that contains no value, and simply
marks the end of the list - That way, insertions come either before first
element, or before sentinel - See code in chapter on-line
23Head Pointer, Tail Pointer, Sentinel
LastLink, firstLink
8
4
6
3
Sentinel
24Now Worksheet
- Now do linked list stack and queue
- Will do other linked list structures in 261