Queue Using Circular Linked Lists - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Queue Using Circular Linked Lists

Description:

Queue Class. Make the array circular. 2. front points to where the ... public class Queue E public void add ( E x ) // add at the end. if ( front == null ) ... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 15
Provided by: qya
Category:

less

Transcript and Presenter's Notes

Title: Queue Using Circular Linked Lists


1
QueueUsing Circular Linked Lists
1
UWP - Qi Yang
2
Queue Class
  • Make the array circular

front points to where the first element is
rear points to where next element goes
3
Queue Using a Linked Listwith Two Pointers
. . .
front
4
  • add (at rear)
  • rear.next new Node ( x, null )
  • rear rear.next
  • remove (at front)
  • tmp front.info
  • front front.next

. . .
front
tmp
. . .
front
4
4
5
  • public class QueueltEgt
  •   . . .
  • public void add ( E x ) // add at the end
  • if ( front null )
  • front new Node ( x, null )
  • rear front
  • else
  • rear.next new Node ( x, null )
  • rear rear.next
  •  
  • public E remove() // remove from the front
  • if ( front null )

Can we remove the if statement? Assuming rear
is invalid (null) if front is null
5
5
6
  • Assuming rear is invalid (null) if front is null
  • public class QueueltEgt
  •   . . .
  • public void add ( E x )
  • if ( front null )
  • rear front new Node ( x, null )
  • else
  • rear rear.next new Node ( x, null )
  •  
  • public E remove()
  • if ( front null )
  • return null
  • E x front.info
  • front front.next
  • return x

6
6
7
Queue Using a Linked Listwith One Pointer
. . .
front
Method remove O(1)
Method add O(N)
. . .
Method remove How to find front? Circular
Linked List O(1)
Method add O(1)
rear.next is front!
8
Queue Using a Linked Listwith One Pointer
Queue with one item if (rear rear.next)
Empty queue if (rear null)
Queue with two or more items if (rear !
rear.next)
. . .
9
Empty Queue
Empty queue if (rear null)
Remove Do nothing Add rear new
Node( obj, null ) rear.next rear
Can it be one line? rear.next rear new
Node(obj, null) NO! Java examines rear.next
first rear.next is not a variable
10
Queue with One Item
Queue with one item if (rear rear.next)
Remove rear null Add new Node( obj,
rear ) rear.next new Node( obj, rear )
rear rear.next new Node( obj, rear
.next) rear.next new Node( obj, rear
.next) rear rear.next rear rear.next
new Node( obj, rear .next)
11
Queue with Two or More Items
Remove rear.next rear.next.next Add
new Node( obj, rear.next ) rear.next new
Node( obj, rear.next ) rear rear.next
rear rear.next new Node( obj, rear.next )
. . .
12
  • public class QueueltEgt
  • . . .
  • public void add ( E x )
  • if ( rear null )
  • rear new Node ( x, null )
  • rear.next rear
  • else
  • rear rear.next new Node ( x,
    rear.next )
  •  
  • . . .

12
12
13
  • public class QueueltEgt
  • . . .
  • public E remove()
  • if ( rear null )
  • return null
  • E x rear.next.info
  • if ( rear rear.next )
  • rear null
  • else
  • rear.next rear.next.next
  • return x

13
13
14
Quiz 6
  • Hashing
  • Division (remainder)
  • Collision
  • Linear Probe
  • Double hash
  • Buckets
  • Binary Search
  • Linked List
  • Add and remove
Write a Comment
User Comments (0)
About PowerShow.com