Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Web Based Services, Lecture Notes - Computer Science - 4, Study notes of Computers and Information technologies

Stacks, LIFO ,functional requirements of stack , examples

Typology: Study notes

2010/2011

Uploaded on 09/09/2011

asdlol2
asdlol2 🇬🇧

4.4

(8)

233 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Data Structures G5029
Kingsley Sage
Room 5C16, Pevensey III
khs20@sussex.ac.uk
© University of Sussex 2006
Lecture 2
Lecture 2
Stacks
The usual analogy is the “stack of plates”.
A way of buffering a stream of objects, in which the the Last In
is the First Out (LIFO).
What might we use a stack for?
Functional requirements of a stack.
Two basic methods, push and pop.
The first plate begins the pile, the next is placed on top of the
first and so on.
A plate may be removed from the pile at any time, but only from
the top.
Pushing a plate onto the pile increases the number on the pile.
Popping a plate from the pile decreases the number on the pile.
pf3
pf4
pf5

Partial preview of the text

Download Web Based Services, Lecture Notes - Computer Science - 4 and more Study notes Computers and Information technologies in PDF only on Docsity!

Data Structures G

Kingsley Sage

Room 5C16, Pevensey III

khs20@sussex.ac.uk

© University of Sussex 2006

Lecture 2

Lecture 2

 Stacks

– The usual analogy is the “stack of plates”.

– A way of buffering a stream of objects, in which the the Last In

is the First Out (LIFO).

– What might we use a stack for?

 Functional requirements of a stack.

– Two basic methods, push and pop.

– The first plate begins the pile, the next is placed on top of the

first and so on.

– A plate may be removed from the pile at any time, but only from

the top.

– Pushing a plate onto the pile increases the number on the pile.

– Popping a plate from the pile decreases the number on the pile.

Stacks

 Say we want to build a calculator to

evaluate (3 + (2 * 5)) / 2

 Work your way from the outside of the

expression to the inside putting the operands

(numbers and mathematical operators) onto

the stack

Stacks – calculator example

Evaluating (3+(2*5)) / 2

The stack has 7 elements

on it and 2 is at the

top of the stack.

To evaluate, put the item

at the head into a variable

and pop each element in turn

performing the specified

calculation on the variable.

Finished when the stack is

empty.

Interfaces

 Notice that clients of the SimpleStack class only have access to the stack through the public methods isEmpty(), isFull(), push() and pop().  The actual stack array and the pointer top are private and cannot be directly manipulated by the client.  Arrays are not the only way to implement a stack – we can also use linked lists (later in the course).  the Java construct of the interface:

public interface Stack {

public boolean isEmpty();

public void push(Object items);

public Object pop();

Implementation

 Now we shall see how we can use the stack interface to create a new stack class StackArray.  StackArray will be able to deal with the situation when a stack needs to be re-sized because it is full. This will make use of a System Java method to copy one stack to another new one.

System.arraycopy(stack, 0, newStack, 0, stack.length);

// This is equivalent to …

for (int i=0; i<stack.length;i++) {

newStack[i] = stack[i];

import java.util.NoSuchElementException; public class StackArray implements Stack { private Object[] stack; private int top; public StackArray() { stack = new Object[1]; top = 0; } public boolean isEmpty() { return (top==0); } public void push(Object item) { if (top==stack.length) { // expand the stack Object[] newStack = new Object[2*stack.length]; System.arraycopy(stack,0,newStack,0,stack.length); stack = newStack; } stack[top++] = item; } public Object pop() { if (top==0) { throw new NoSuchElementException(); } else { return stack[--top]; } } }

Demonstration

 Now we can see StackArray in use:.  Can’t instantiate Stack directly, as it’s only an interface.  Need to “object”-ify items to put them on the stack.

import DataStructures.*;

public class StackDemo {

public static void main(String[] args) {

Stack s = new StackArray();

for (int i = 0; i < 8; i++) {

a.push(new Integer(i));

while (!s.isEmpty()) {

System.out.println(s.pop());