Queue and Stack - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Queue and Stack

Description:

Queue Class. Abstract. Container where elements are added to one end and removed from the other. ... Queue Class. Implementation (II) Pointer front/rear moves ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 19
Provided by: qya
Category:
Tags: queue | stack

less

Transcript and Presenter's Notes

Title: Queue and Stack


1
Queue and Stack
  • Abstract
  • Queue
  • Container where elements are added to one end and
    removed from the other.
  • Stack
  • Container where items are added and removed from
    one end

2
Queue and Stack
  • Queue First In First Out (FIFO)

Out
In
Stack Last in First Out (LIFO)
In
Out
3
Queue and StackPublic Methods
  • Queue
  • Add
  • Remove
  • isEmpty
  • isFull
  • Stack
  • Push
  • Pop
  • isEmpty
  • isFull

4
Queue and StackImplementation
  • Stack
  • private Object items
  • private int top
  • Queue
  • private Object items
  • private int front, rear, count

5
Queue Implementation
  • Make the array circular
  • Variables front, rear, count

front points to where the first element is
rear points to where next element goes
6
itemsrear obj rear (rear 1)
items.length count
obj itemsfront front (front 1)
items.length count --
7
public class Queue private Object items
private int front, rear, count public
Queue( int size ) items new
Objectsize front rear count 0
public boolean isEmpty() return
count 0 public boolean isFull ()
return count gt items.length
... // class Queue
8
public class Queue private Object items
private int front, rear, count ...
public void add ( Object x )
itemsrear x rear (rear 1)
items.length count public
Object remove() Object x
itemsfront front (front 1)
items.length count -- return x
// class Queue
9
Queue Class
  • front rear count 0

front
0
rear
1
4
3
2
10
Queue Class
  • Add A

Add B
front
front
0
0
A
A
1
4
B
1
4
3
rear
2
3
2
rear
11
Queue Class
  • Add C, D

Add E
front
front
0
rear
0
A
A
1
4
E
B
1
4
B
C
D
D
C
3
2
3
rear
2
12
Queue Class
  • After a while

Remove twice
0
rear
0
rear
1
4
x0
1
4
x2
front
front
3
2
3
2
13
Method numItems
  • As Implementer
  • public int numItems()
  • return count

14
Method numItems
  • As User
  • public int numItems(Queue q, int qSize)
  • (
  • Queue tq new Queue(qSize)
  • int num 0
  • while ( !q.isEmpty() )
  • tq.add( q.remove() )
  • num
  • while ( !tq.isEmpty() )
  • q.add( tq.remove() )
  • return num
  • Is the 2nd while loop necessary?

15
Method numItems
  • As User
  • public int numItems(Queue q, int qSize)
  • (
  • Queue tq new Queue(qSize)
  • int num 0
  • while ( !q.isEmpty() )
  • tq.add( q.remove() )
  • num
  • q tq
  • return num
  • Pass by Value!

16
Method numItems
  • As User
  • public int numItems(Queue q, int qSize)
  • (
  • Queue tq new Queue(qSize)
  • int num 0
  • while ( !q.isEmpty() )
  • tq.add( q.remove() )
  • num
  • while ( !tq.isEmpty() )
  • q.add( tq.remove() )
  • return num
  • The 2nd while loop is necessary!
  • Can you do it without tq?

17
Method numItems
  • As User
  • public int numItems(Queue q, int qSize)
  • (
  • Object tq new ObjectqSize
  • int num 0
  • while ( !q.isEmpty() )
  • tqnum q.remove()
  • for (int index 0 index lt num index )
  • q.add( tqindex )
  • return num

18
Schedule
  • Prog3 Grace time
  • 9 pm, Thursday
  • Prog4 Due Time
  • 11 pm, Oct 28
  • 1ReadOnly folder
  • StackMain.txt
  • TvectorMain.txt
  • Friday
  • No class!
Write a Comment
User Comments (0)
About PowerShow.com