mastering stacks an introduction to stacks data structures

26
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Upload: hugo-gibbs

Post on 16-Jan-2016

247 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Mastering STACKSAN INTRODUCTION TO STACKSData Structures

Page 2: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Definition of a StackDefinition of a Stack

Before we start, see if you can fill in the blanks:A stack is basically just a Data StructureItems can be added (pushed) on to the stack Items can also be removed (popped or pulled) from the stackItems can be added or removed from only ONE ENDThe end is: the TOP of the Stack

Page 3: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Dynamic Data Structure• A stack is a dynamic data structure since the size of the

stack can vary according to the number of elements currently in the stack.

• Imagine you had a stack of letters that you needed to read.• Letter 1 is at the top of the stack. • Letter 3 is at the bottom.• Removing a letter: You would only have the option of removing Letter 1 from the stack (first) as it is on top. • Letter 2 would be next.

Letter 1

Letter 2Letter 3

Page 4: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

LIFO• It won’t take you long to guess why Stacks are referred

to as having a LIFO structure.

• LIFO stands for = LAST IN, FIRST OUT

• Stacks aren’t, if you think about it, very fair!• If you had received these letters in order.• The first one in, would be at the bottom!• The last one in, would be the first out (the first to be read)

Letter 1

Letter 2Letter 3

Page 5: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

STACK = unfair(!) = LIFO• It’s helpful for you to remember the LIFO mnemonic

• This will help you remember the way that stacks operate

• Stacks are incredibly useful structures in programming

• On the next slide we are going to look at how they can be implemented

Side note: They are different to the other data structure we will

study (queues) that are much more FAIR! Queues are first in, first out. The first person that arrives in theQueue will also be the first one out!

Side note: They are different to the other data structure we will

study (queues) that are much more FAIR! Queues are first in, first out. The first person that arrives in theQueue will also be the first one out!

Page 6: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Implementation of a StackImplementation of a Stack

A stack may be implemented using an:1.ARRAY and2.Two additional VARIABLES

Page 7: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Declaring Variables (creation of a stack)Declaring Variables (creation of a stack)

Two variables that are necessary for implementing a stack1.MAX STACK SIZE = Holding the size of the array

2.TOP = Holding a pointer that points to the top of the stack

Page 8: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Initialisation of the StackInitialisation of the Stack

Initialisation involves giving the relevant variables a Starting value. In this case …

… the TOP Variable (pointer) would be set to ZER0Top = 0

Page 9: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Diagrammatic representation Diagrammatic representation

ArthurMooseChip

MaxStacksize = 6

Top = 6

• The diagram shows an array that can hold a maximum of 6 elements with 3 items currently in the stack.• See if you can fill in the blanks (to initialise the two variables below)

6

5

4

3

2

1

Page 10: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Operations you could perform on a stackOperations you could perform on a stack

ArthurMooseChip

• PUSH (add an element to the stack)

• POP (PULL) (remove an element from the stack)

6

5

4

3

2

1

Page 11: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Pseudo code for pushing an element on to a stackPseudo code for pushing an element on to a stack

ArthurMooseChip

• PUSH (add an element to the stack)

6

5

4

3

2

1

Procedure PushIf top = maxstacksizeThen Write “Stack is full”ElseAdd 1 to topStack[top] : =NewItemEndIfEnd Proc

*See if you can fill in the blanks by following the logic

Page 12: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Pseudo code for popping an element from a stackPseudo code for popping an element from a stack

ArthurMooseChip

• POP (remove an element from the stack)

6

5

4

3

2

1

Procedure PopIf top = 0Then write ‘Stack is empty’ElsePoppedItem := Stack[top]Subtract 1 from topEnd ifEnd Proc

*See if you can fill in the blanks by following the logic

Page 13: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Suggested TaskSuggested TaskCreate the following interface in VB.NET (or any other high level language of your choice) and see if you can implement a fully functional stack, complete with push and pop functionality. *Use the algorithms provided on the previous slides to help you. Here are the first stages of interface design and the INPUTBOX code (Vb.Net) to get you started

1

2

3

4

*to add items in a listbox, you Can do so with code, or go to the properties of the list box and add them in “collection”

Page 14: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Uses of Stacks in Computing:Uses of Stacks in Computing:Store return addresses, parameters, and register contents when subroutines are called1

When the subroutine ends, the address that is at the top of the stack is POPPED and the computer would then continue execution from that address.

2

In Recursive functions - the values are held in a stack3

In evaluating mathematical expressions that are held in RPN (Reverse polish notation)(see power point on Reverse Polish Notation)

4

Page 15: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

• The simplest application of a stack is to reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack.

• Another application is an "undo" mechanism in text editors; this operation is accomplished by keeping all text changes in a stack.

Applications of a Stack #1Applications of a stack #1Applications of a stack #1

Page 16: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

• Backtracking. This is a process when you need to access the most recent data element in a series of elements. Think of a labyrinth or maze - how do you find a way from an entrance to an exit?

Once you reach a dead end, you must backtrack. But backtrack to where? to the previous choice point. Therefore, at each choice point you store on a stack all possible choices. Then backtracking simply means popping a next choice from the stack.

Applications of a Stack #2Applications of a stack #2Applications of a stack #2

Page 17: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Applications of a Stack #3

• Language processing:

• space for parameters and local variables is created internally using a stack.

• compiler's syntax check for matching braces is implemented by using stack.

• support for recursion

Applications of a stack #3Applications of a stack #3

Page 18: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Applications of a Stack #4

• Consider the following postfix expression

Here is a chain of operations

**see the presentation on Reverse Polish Notation

**Note, that division is not a commutative operation, so 2/3 is not the same as 3/2.

Applications of a stack #4Applications of a stack #4

Page 19: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Implementation of a Stack• Implementation

• In the standard library of classes, the data type stack is an adapter class, meaning that a stack is built on top of other data structures. The underlying structure for a stack could be an array, a vector, an Array List, a linked list, or any other collection. Regardless of the type of the underlying data structure, a Stack must implement the same functionality. This is achieved by providing a unique interface:

Implementation of a stackImplementation of a stack

Page 20: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Test yourself and get coding• *You can create these yourself in VB.Net (Console Application)

See if you can guess the output first by analysing the code.

Test yourself and get coding!Test yourself and get coding!

Page 21: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

What is the output?What is the output?

Page 22: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

AnswersAnswers

Page 23: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

AnalysisAnalysis

Declaring a New StackDeclaring a New Stack

Initialising the Stack (Pushing values onto the stack)Initialising the Stack (Pushing values onto the stack)

Printing the StackPrinting the Stack

Page 24: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

What is the output?What is the output?

Page 25: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

AnswersAnswers

Page 26: Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

Try the following exercises in the python IDE