VCE Software Development Theory Slideshows By Mark Kelly Vceit.com Stacks.

Slides:



Advertisements
Similar presentations
Stacks and Queues. Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list – Can store just about.
Advertisements

Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
Programming 8086 – Part IV Stacks, Macros
Chapter 9: Data Structures I
Stack & Queues COP 3502.
CPU Review and Programming Models CT101 – Computing Systems.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structures Michael J. Watts
Topic 15 Implementing and Using Stacks
Data Structures & Algorithms
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Summary of lectures (1 to 11)
Topic 15 Implementing and Using Stacks
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Sorting, Stacks, Queues Bryce Boe 2013/11/06 CS24, Fall 2013.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Stacks and Queues Introduction to Computing Science and Programming I.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Pointers, Stacks and Memory Addressing Computer Science and Programming Concepts.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Foundation of Computing Systems Lecture 3 Stacks and Queues.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ Stack ◦ Queue & Priority Queue 2.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 6.6, 11, 16 Stacks 1CSCI 3333 Data Structures.
Stack ADT (Abstract Data Type) N …
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structures Michael J. Watts
COSC160: Data Structures: Lists and Queues
Copy Constructor / Destructors Stacks and Queues
Arrays.
The Stack.
Chapter 15 Lists Objectives
Data Structures and Algorithms
Principles of Computing – UFCFA3-30-1
Pointers and Linked Lists
Stacks, Queues, and Deques
Data Structures and Database Applications Stacks in C#
Tonga Institute of Higher Education
ITEC 2620M Introduction to Data Structures
CSE 214 – Computer Science II Stacks
Data Structures and Algorithms
Data structures.
Recall: stacks and queues
Topic 15 Implementing and Using Stacks
Stacks CS-240 Dick Steflik.
Program and memory layout
Program and memory layout
Program and memory layout
Stacks, Queues, and Deques
Computer Organization and Assembly Language
DATA STRUCTURES IN PYTHON
(The Stack and Procedures)
Presentation transcript:

VCE Software Development Theory Slideshows By Mark Kelly Vceit.com Stacks

Contents What are stacks? Logical stacks Implementing stacks Visualising stacks Stack pointers Other stack commands Common stack errors

What are they? Simple, sequential, temporary data storage structures Used by CPUs to store things like procedure calls and data on recursive function calls. Used in low-level programming Related to: queues, buffers First proposed in 1955, and patented in 1957 by the German Friedrich L. Bauer.

Coin stacks You PUSH coins into the holder. You POP coins from the holder. The first coin added is the last coin out. In a stack, this is called FIRST IN LAST OUT (FILO) or LAST IN FIRST OUT (LIFO) Coins can only be pushed or popped from the top.

Magazine stacks You PUSH shells into the magazine at its only opening at the top. You POP shells from the top of the magazine. The first shell added is the last shell out. Shells can only be added or removed from the top.

Book stacks Think of a data stack like a stack of books. The oldest items added to the stack are at the bottom, the newest additions at the top. Books can only be added or removed at the top of the stack.

Important Only the top item on the stack is accessible at any given time.

Example To store 2 numbers temporarily… The PUSH command adds an item to the stack PUSH 11 … 11

Example PUSH 22 … Notice how the most recently added number is at the top? Values can only be added or removed from the top of the stack 22 11

POP To retrieve a value from the stack, use the POP command. E.g. POP x takes the next value from the stack and stores it in variable x…

Example And the stack now look like this… 11

Visualising stacks Stacks can be visualised as top- down or bottom up. You will see both versions around, so be flexible Top-down visualisation Bottom-up visualisation Top >> <<Top

Implementing a stack While a logical (theoretical)stack can happily shuffle existing stacks items along to make room for a new item, this is inefficient to do in real life Such memory manipulation is slow, processor- intensive and unnecessary.

Stack pointers In a real, working stack, instead of moving stack items up and down, you just change a stack pointer. The value stored in the stack pointer refers to the address of top of the stack. Existing stack items stay where they are.

Stack Pointers The stack pointer (SP, or top) is a value that points to the location (in RAM or in an array or linked list) of the top of the stack. The top of the stack is where the next item will be pushed.

Implementing stacks Stacks often implemented in arrays or linked lists (which also use pointers instead of physically moving data around) A CPUs stack is implemented in RAM locations.

Consider a stack created with an array of 5 elements. Lets call it bottom up since well visualise the top of the stack being at the bottom. This stack slots are numbered 0 to 4. More on this later. Note: some stacks numbering starting at 1. indexvalue

An empty stack indexvalue TOP0 The arrow points to the top of the stack TOP = 1 (the array index where the next push will be stored)

PUSH 11 indexvalue TOP = 1 (the array index where the next item will be pushed) TOP1 In this stack implementation, the stack pointer is updated after an operation and points to the slot to use for the next operation.

PUSH 11 indexvalue TOP0 In other stack implementations, the pointer points to the current top value and is updated before the next stack operation. TOP=0 (the array index where the current top item is)

Be careful Since stacks are implemented with different basic behaviours, you need to approach a stack problem carefully. – Some stacks are shown filling from the bottom, others from the top. – In some, the first element is 0; in others its 1. – Some update the stack pointer after each push or pop. Other update before an operation. – Some refer to the stack pointer as Top.

PUSH 22 indexvalue TOP = 2 (the array index where the next push will be made) TOP2

PUSH 33 indexvalue TOP = 3 (the array index where the next push will be made) TOP3

PUSH 33 indexvalue TOP = 3 (the array index where the next push will be made) TOP3 Notice that the existing stack items stay where they are. Only the stack pointer changes. But 33 is at the top of the stack. Last in, first out.

POP indexvalue TOP = 2 (the array index where the next push will be made) TOP2

POP indexvalue TOP = 2 (the array index where the next push will be made) TOP2 POP retrieved the item at the top of the stack (index 3) and decremented TOP to point to the next available item.

Other stack commands In addition to PUSH and POP, some stacks have extra commands they understand PEEK – fetch the item at the top of the stack but leave it on the stack. Dont change the SP. SWAP – swap the top 2 items on the stack. Equivalent to: POP x (x is a variable where the popped item is put) POP y PUSH x PUSH y

Other stack commands ROTATE – rolls the stack items around by 1 place. Some implementations have flavours: rotate left, and rotate right. indexvalue indexvalue Before rotateAfter rotate

Common Stack Errors Stack Empty – when you try to POP when the stack is empty. Stack overflow – when you try to PUSH a value into a stack that has no more free space available.

From VCAAs sample exam questions* *

Decoding the depiction of the stack There are no real standards for depicting stacks. You have to determine things that the question writer assumed when they show their stacks behaviour. First: is the stack top-down or bottom up? Is the top of the stack at the 92 end (top-down) or the 52 end (bottom-up)?

Lets assume that the events in the table are in chronological order. Push 23 occurred before push 18. Lets also assume that Top is a variable containing the value of the stack pointer: the current active stack position. How does the table relate to the stack? We can only assume that the stack is shown as it is after the events in the table have been carried out.

??? We can only wonder why the bottom 2 stack items are bolded. The question does not explain this convention.

Lets see if we can read it. According to the table, after the PUSH 23, the stack pointer (Top) is 2. In the stack, 23 appears in slot 3 (counting from both the top and the bottom). Therefore if tops value is 2 and its in position 3, we know the index number of the stacks first slot is zero. So the stacks slots are numbered zero to 4, not 1 to 5.

Relearning how to count In computing, counting often starts with item 0 instead of item 1. E.g. array indexes (and Visual Basic listboxes) often begin with 0, so an array with 100 slots would be numbered 0 to 99. Forgetting this and trying to count to the number of items (e.g. 100) is a common error.

If Top=1, and the stack is bottom-up, the current stack value is 83. The next free slot for a push is Top + 1 (i.e. slot 2). Apparently this stack updates the pointer immediately before a push/pop, rather than after it. Other stacks update the SP immediately after an operation.

CONFUSED BY THIS STACK? (as I was) The 23,75 and 92 shown in the stack dont actually exist yet if youre following the history of operations in the table. At the start of the tables events - before the push 23 - the stack actually looks like this: Stack But it may finally explain the mysterious bolding of the 2 values in the question!

If we push 23, it goes into stack slot numbered 2 (the one physically third from the bottom). TOP is updated to equal 2. In the table, you can see the stacks state after the push: top=2 and the output is Item added (23).

In the next operation, PUSH 18, the value 18 is put on the stack in slot 3. So in the table you fill in 3 into the empty cell. 3

The next operation is a POP so 18 is removed from the stack (which explains why the displayed stack does not show it) and the pointer is decremented (reduced by 1) to 2. 3

3 The next operation is a PUSH 75, so 75 goes into slot 3 and the pointer is incremented to 3 again. So we put 3 into the empty TOP and the output would be Item added (75) 3 Item Added 75

3 The next operation is a PUSH 92, so 92 goes into slot 4 and the pointer is incremented to 4. So the output to add to the table would be Item added (92) 3 Item Added 75 Item Added 92

3 The final operation, Push 47 causes a problem. The stack pointer is already at its maximum value. It cannot be incremented – in other words the stack is full. 3 Item Added 75 Item Added 92 3Stack full

Why check for stack overflow? Checking for stack overflow is an essential part of a stack implementation. Failing to check for overflow could lead to serious problems.

Why check for stack overflow? If the stack is implemented in an array, overflow would create a runtime error when undeclared elements of an array were accessed.

Why check for stack overflow? If the stack is in RAM, writing outside the stacks permitted area could overwrite and corrupt program code or other data. (This is deliberately done in some code injection hacking exploits to gain unauthorised entry into some software)

Why check for stack overflow? If the stack is in RAM, writing outside the stacks permitted area could overwrite and corrupt program code or other data.

Why check for stack overflow? Tip: If your language supports dynamic arrays (which can be resized during runtime) this is a neat way to create resizable stacks and avoid overflow. In VB, use REDIM PRESERVE.

3 The attempt to push 47 would, according to the explanation, result in the output Stack full. The stack pointer remains unchanged. 3 Item Added 75 Item Added 92 3Stack full

3 A lot of thinking for 6 marks! One hopes the real exam wont have such a question with so many mysteries and vagaries that the poor student has to interpret before attempting to answer the question. 3 Item Added 75 Item Added 92 3Stack full

To summarise StackStarts atPush 23Push 18PopPush 75Push 92Push TOP=

Homework Write a program that simulates the behaviour of the stack in the sample exam question, including its behaviour when its full or empty. It can be top-down if thats easier for you. Test its behaviour with the data in the sample question. Add further test data to verify its stack empty behaviour. Create a testing table to record your testing.

Extension tasks Make the stacks contents & SP visible onscreen. Add the PEEK command. Add the SWAP command. Add the ROTATE command.

By Mark Kelly vceit.com These slideshows may be freely used, modified or distributed by teachers and students anywhere on the planet (but not elsewhere). They may NOT be sold. They must NOT be redistributed if you modify them. VCE IT THEORY SLIDESHOWS