Presentation on theme: "VCE Software Development Theory Slideshows By Mark Kelly Vceit.com Stacks."— Presentation transcript:
VCE Software Development Theory Slideshows By Mark Kelly email@example.com 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…. 22 11
Visualising stacks Stacks can be visualised as top- down or bottom up. You will see both versions around, so be flexible. 22 11 22 Top-down visualisation Bottom-up visualisation Top >> <
"name": "Visualising stacks Stacks can be visualised as top- down or bottom up.",
"description": "You will see both versions around, so be flexible. 22 11 22 Top-down visualisation Bottom-up visualisation 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 4 3 2 1 0
An empty stack indexvalue 4 3 2 1 0 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 4 3 2 1 011 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 4 3 2 1 011 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 4 3 2 122 011 TOP = 2 (the array index where the next push will be made) TOP2
PUSH 33 indexvalue 4 3 233 122 011 TOP = 3 (the array index where the next push will be made) TOP3
PUSH 33 indexvalue 4 3 233 122 011 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 4 3 2 122 011 TOP = 2 (the array index where the next push will be made) TOP2
POP indexvalue 4 3 2 122 011 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 455 344 233 122 011 indexvalue 444 333 222 111 055 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* *http://www.vcaa.vic.edu.au/vcaa/vce/studies/infotech/softwaredevel/IT-softwaredev-samp-w.pdf
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 83 52 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
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 firstname.lastname@example.org 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