Difference between revisions of "Stacks"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Peek)
Line 1: Line 1:
 
A stack is a last in, first out data structure that can only have data added or removed from the top.
 
A stack is a last in, first out data structure that can only have data added or removed from the top.
The stack has limited size and the stack pointed will always point to the value at the top of the stack. An empty stack thus has a stack pointer value of 0. You can only push an item if the stack is not full. You can only pop an item if the stack is not empty.
+
The stack has limited size and the stack pointer will always point to the value at the top of the stack. An empty stack thus has a stack pointer value of 0. You can only push an item if the stack is not full. You can only pop an item if the stack is not empty.
 
==Check for Full==
 
==Check for Full==
 
==Check for Empty==
 
==Check for Empty==

Revision as of 09:04, 23 May 2017

A stack is a last in, first out data structure that can only have data added or removed from the top. The stack has limited size and the stack pointer will always point to the value at the top of the stack. An empty stack thus has a stack pointer value of 0. You can only push an item if the stack is not full. You can only pop an item if the stack is not empty.

Check for Full

Check for Empty

Push

When an item is pushed it is added to the top of the stack. if Bill is at value one and Ted is pushed, he would be added to value 2.

Pop

Using the example from before, if Bob was at value 1 and Bill was at value 2, the value from the top of the stack would be removed. so Bill would be removed, just leaving Bob at value 1.

Peek

Allows you to get the value from the top of the stack, without actually popping it.