0% found this document useful (0 votes)
8 views

CEN 235 4. Abstract Data Types - Queue and Stack

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CEN 235 4. Abstract Data Types - Queue and Stack

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Creating Structures with

Abstract Data Types


Abstract Data Types (ADT)

• It is often convenient to describe


algorithms and data structures in
terms of the operations
performed
– rather than in terms of details of
implementation.
• We separate the “concept” from
a particular implementation.
Abstract Data Types (ADT)

• Why use ADTs?

– Organisation & reducing


complexity
– Ease of coding
– Controlling how your data may
be accessed by a user
Stacks and Queues (and variations) are structures
that are frequently used in programs and often use
arrays as the underlying structure:

For example:

A queue of things todo if creating a todo list app.

A stack of web pages visited to go back in a web


browser.
Queue concepts
• Illustration (queue of persons):
BUS
STOP

5
Queue concepts
• A queue is a first-in-first-out sequence
of elements.
• Elements can be added only at one end
(the rear of the queue) and removed
only at the other end (the front of the
queue).
• The length/size of a queue is the
number of elements it contains.
• An empty queue has length zero.
6
Queue ADT: requirements
• Requirements:
1) It must be possible to make a queue empty.
2) It must be possible to test whether a queue is
empty.
3) It must be possible to obtain the length of a
queue.
4) It must be possible to add an element at the rear
of a queue.
5) It must be possible to remove the front element
from a queue.
6) It must be possible to access the front element in
a queue without removing it.
It must be possible to make a queue empty.
A function is needed that will set the values in the array to
a value that signifies it is clear and set the front and back
of the queue to the first index of the array.

It must be possible to test whether a queue is empty.

A function is required that will test if the queue is empty.

It must be possible to obtain the length of a queue.


A function or value is required that will keep track of
the number of values in the array.
It must be possible to add an element at the rear of a
queue.
a function is needed that will add an element to the array. The
position of the next element will need to be marked. What if we
reach the end of the array?

It must be possible to remove the front element from a


queue.
A function is required that will allow the removal of an element. The
end of the queue will need to be re-set to the next position. What
happens when you reach the end of the queue?

It must be possible to access the front element in a queue


without removing it.

A function is required that will read the top element value.


The Queue: Visualisation

int []MyQueue = new int[5];

0 1 2 3 4

MyQueue
Initialisation Length = 0
Front = 0

Back = 0

0 1 2 3 4

MyQueue

front back
Assign item to array[back]
addtoqueue
Increment back
Increment Length

0 1 2 3 4

MyQueue 6

front back

Front = 0
Back = 1
Length = 1
Assign item to array[back]
addtoqueue
Increment back
Increment Length

0 1 2 3 4

MyQueue 6 5

front back

Front = 0
Back = 2
length = 2
Remove from queue

0 1 2 3 4

MyQueue 6
5

front back

Increment front
Front = 1
decrement length
Back = 2
length = 1
Add to queue? The array is not full but we have reached
the length of the array

0 1 2 3 4

MyQueue 6 5 4
7 8

front
back

Front = 3 Assign item to array[back]


Back = 0 Increment back
length = 2 Increment length

If back = array length (i.e. 5)


Set back to 0
Add to queue

0 1 2 3 4

MyQueue 5 4
12 7 8

Assign item to array[back]


Increment back
Increment length
Front = 3
If back = array length
Back = 1
Set back to 0
length = 3
Add to queue?

0 1 2 3 4

MyQueue 12 3 46 7 8

If array full

exit
Assign item to array[back]
Increment back
Increment length
Front = 3
If back = array length
Back = 3
length = 5 Set back to 0
Remove an item

If length == 0
MyQueue
exit
else
Increment front
Size = 0 decrement length
Remove an item

0 1 2 3 4

MyQueue 12 3 46 7 8

Front = 4
If length == 0
exit
else
Increment front
decrement size

If front = array length


set front to 0
• Possible contract:
/////////////// Accessors ///////////////
boolean isEmpty();
// Return true if and only if this queue is empty.
int size();
// Return this queue’s length.
int getFirst();
// Return the element at the front of this queue.
/////////////// Modifiers ///////////////
void clear();
// Make this queue empty.
void addLast(int elem);
// Add elem as the rear element of this queue.
int removeFirst();
// Remove and return the front element of this queue.
• Java implementation:
public class Queue
private int[] elems;
private int front, rear, length;
/////////////// Constructor ///////////////

public Queue (int maxLength) {


elems = new int[maxLength];
front = rear = length = 0;
}
• Java implementation (continued):
public boolean isEmpty () {
return (length == 0);
}
public int size () {
return length;
}
public int getFirst () {
if (length == 0) throw …;
return elems[front];
}
public void clear () {
front = rear = length = 0;
}
public void addLast (int elem) {
if (length == elems.length) throw …;
elems[rear++] = elem;
if (rear == elems.length) rear = 0;
length++;
}
public int removeFirst () {
if (length == 0) throw …;
int frontElem = elems[front];
front++;
if (front == elems.length)
front = 0;
return frontElem;
}
}
Stack concepts (1)
• A stack is a last-in-first-out sequence of
elements.
• Elements can added and removed only
at one end (the top of the stack).
• The depth of stack is the number of
elements it contains.
• An empty stack has depth zero.

25
Stack concepts (2)
• Illustration (stack of books):
Initially: After remov- After adding After adding
ing a book: “Misérables”: “2001”:

2001
Rob Roy Misérables Misérables
War & Peace War & Peace War & Peace War & Peace
Moby Dick Moby Dick Moby Dick Moby Dick

26
Stack ADT: requirements
• Requirements:
1) It must be possible to make a stack empty.
2) It must be possible to add (‘push’) an element to
the top of a stack.
3) It must be possible to remove (‘pop’) the topmost
element from a stack.
4) It must be possible to test whether a stack is
empty.
5) It should be possible to access the topmost
element in a stack without removing it.
It must be possible to make a stack empty.

A function is needed that will set the values in the array to a value that
signifies it is clear and set the top of the stack to the first index of
the array

It must be possible to add (‘push’) an element to the


top of a stack.

a function is needed that will add an element to the array. The


position of the next element will need to be marked. What if we
reach the end of the array?

It must be possible to remove (‘pop’) the topmost


element from a stack.

A function is required that will allow the removal of an element. The


top of the stack will need to be re-set to the next position. What
happens when you reach the end of the stack?
It must be possible to test whether a
stack is empty.

A function is required that will test if the stack is


empty – a counter will be required that will keep
track of the number of elements in the array.

It should be possible to access the topmost


element in a stack without removing it.

A function is required that will read the top


element value.
• The stack: visualisation
• 1. create the stack

int []MyStack = new int[5];


Top element
represents the
element position at
[4] the top of the stack
Memory
[3]
Depth is the
MyStack [2] position that the
next element
Depth 0 will be added
[1] to. It is also the
number of the
[0] elements in the
initialise top_element to -1? array.
Add/push an item on the stack

[4]
Memory
[3]
MyStack [2]

[1]

5 Top
[0] element

Assign element
to depth position
Top_element 0
Increment depth
Depth 1
Increment top
element
Add/push an item on the stack

[4]
Memory
[3]
MyStack
[2] depth
Top
[1] 10
element

[0] 5
Assign element to
depth position
Increment depth
We now have a stack of:
Increment top element
top_element 1
Depth 2
pop an item from the stack

[4]
Memory [3]

MyStack [2]

[1] depth

Top
[0] 5 element

Depth 1 decrement the top element

Top-element 0 Decrement depth


push an item from the stack

Memory
[4] depth

MyStack [3] 4 Top


element
[2] 10

[1] 16

[0] 5 Assign the element


increment depth
We now have a stack of:
Top_element 3
Depth 4
push an item from the stack
depth

[4] 6 Top
Memory element
[3] 4
MyStack [2] 10

[1] 16

[0] 5
If depth = array length
stack full
We now have a stack of: Else
Top_element 4 Assign the element

Depth 5 increment depth


Pop from an empty stack

[4]
Memory
[3]
MyStack
[2]

[1]
If stack not empty
Depth 0 [0]
decrement the top
element
Decrement depth
• Points to note:

• To read the top most element without removing


– If the array is not empty
• Return array[topelement]

• To test whether a stack is full:


– Size will be the same as array.length

• To make a stack empty


– Set depth to 0
Set Topelement to -1?

• To test whether a stack is empty:


• Depth will be 0
Stack ADT: contract
• Possible contract
/////////////// Accessors ///////////////
bool isEmpty();
// Return true if and only if this stack is empty.
int getTop();
// Return the message at the top of this stack.
/////////////// Modifiers ///////////////
void clear();
// Make this stack empty.
void push(int elem);
// Add elem as the top element of this stack.
int pop();
// Remove and return the element at the top of this
stack.
Implementation using arrays
• Java implementation:
public class Stack {
private int[] elems;
private int depth;
/////////////// Constructor ///////////////
public Stack (int maxDepth) {
elems = new int[maxDepth];
depth = 0;
}
Implementation using arrays
/////////////// Accessors ///////////////
public boolean isEmpty () {
return (depth == 0);
}
public int getTop () {
if (depth == 0)
error(“stack empty”);
else
return elems[depth-1];
}
Implementation using arrays
/////////////// Modifiers ///////////////
public void clear() {
depth = 0;
}
public void push(int elem) {
if (depth == elems.Length)
error(“Stack full”);
else
elems[depth++] = elem;
}
Implementation using arrays
public int pop() {
if (depth == 0)
error(“Stack empty”);

else{
String topElem = elems[--depth];
return topElem;
}
}

You might also like