Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.

Similar presentations


Presentation on theme: "Mastering STACKS AN INTRODUCTION TO STACKS Data Structures."— Presentation transcript:

1 Mastering STACKS AN INTRODUCTION TO STACKS Data Structures

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

3 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 2 Letter 3

4 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 2 Letter 3

5 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 the Queue 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 the Queue will also be the first one out!

6 Implementation of a Stack A stack may be implemented using an: 1.ARRAY and 2.Two additional VARIABLES

7 Declaring Variables (creation of a stack) Two variables that are necessary for implementing a stack 1.MAX STACK SIZE = Holding the size of the array 2.TOP = Holding a pointer that points to the top of the stack

8 Initialisation of the Stack Initialisation involves giving the relevant variables a Starting value. In this case … … the TOP Variable (pointer) would be set to ZER0 Top = 0

9 Diagrammatic representation Arthur Moose Chip 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) 654321654321

10 Operations you could perform on a stack Arthur Moose Chip PUSH (add an element to the stack) POP (PULL) (remove an element from the stack) 654321654321

11 Pseudo code for pushing an element on to a stack Arthur Moose Chip PUSH (add an element to the stack) 654321654321 Procedure Push If top = maxstacksize Then Write “Stack is full” Else Add 1 to top Stack[top] : =NewItem EndIf End Proc *See if you can fill in the blanks by following the logic

12 Pseudo code for popping an element from a stack Arthur Moose Chip POP (remove an element from the stack) 654321654321 Procedure Pop If top = 0 Then write ‘Stack is empty’ Else PoppedItem := Stack[top] Subtract 1 from top End if End Proc *See if you can fill in the blanks by following the logic

13 Suggested Task Create 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”

14 Uses of Stacks in Computing: Store return addresses, parameters, and register contents when subroutines are called 1 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 stack 3 In evaluating mathematical expressions that are held in RPN (Reverse polish notation) (see power point on Reverse Polish Notation) 4

15 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 #1 Applications of a stack #1

16 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 #2 Applications of a stack #2

17 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 #3

18 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 #4

19 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 stack

20 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!

21 What is the output?

22 Answers

23 Analysis Declaring a New Stack Initialising the Stack (Pushing values onto the stack) Printing the Stack

24 What is the output?

25 Answers

26 Try the following exercises in the python IDE


Download ppt "Mastering STACKS AN INTRODUCTION TO STACKS Data Structures."

Similar presentations


Ads by Google