CS 162 Spring 2005 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CS 162 Spring 2005

Description:

{ Private - used only in the class. Static - doesn't need to reference surrounding class ... Makes adding a new link harder, as must maintain both forward and ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 25
Provided by: timoth90
Category:
Tags: idea | spring

less

Transcript and Presenter's Notes

Title: CS 162 Spring 2005


1
CS 162 - Spring 2005
  • Linked Lists

2
Characteristics 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

3
A typical Link Class
  • private static class Link
  • Link(Object v,Link n)
  • value v next n
  • public Object value
  • public Link next

4
Typically an inner class
  • Class List
  • private static class Link
  • Private - used only in the class
  • Static - doesnt need to reference surrounding
    class

5
Some 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

6
Pointer to head, null terminator
firstLink
8
4
6
3
Null
7
Pointer to head, Sentinel
firstLink
8
4
6
3
Sentinel
8
Head Pointer, Tail Pointer, Sentinel
LastLink, firstLink
8
4
6
3
Sentinel
9
Head Pointer, Tail Pointer, Null terminator
LastLink, firstLink
8
4
6
3
Null
10
Head Pointer, Tail Pointer, Null terminator,
Double Links
LastLink, firstLink
8
4
6
3
Null
11
Simplest 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

12
Code 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

13
Pointer to head, null terminator
firstLink
8
4
6
3
Null
14
How 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.

15
But 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

16
Head Pointer, Tail Pointer, Null terminator,
Double Links
LastLink, firstLink
8
4
6
3
Null
17
Class 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 () ...

18
Elements 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

19
Empty test - just see if link is null
  • public class ListQueue
  • public boolean isEmpty ()
  • return firstLink null

20
Elements Removed from Front
  • class ListQueue
  • public void dequeue ()
  • if (isEmpty())
  • throw new NoSuchElementException()
  • firstLink firstLink.link
  • if (firstLink null) // ie, removing last
  • lastLink null

21
What 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.

22
Sentinel
  • 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

23
Head Pointer, Tail Pointer, Sentinel
LastLink, firstLink
8
4
6
3
Sentinel
24
Now Worksheet
  • Now do linked list stack and queue
  • Will do other linked list structures in 261
Write a Comment
User Comments (0)
About PowerShow.com