Computer Science AS OCR
-
1-1-structure-and-function-of-the-processor as5 主题
-
1-2-types-of-processor as3 主题
-
1-3-input-output-and-storage as2 主题
-
2-1-systems-software as8 主题
-
2-3-software-development as5 主题
-
2-4-types-of-programming-language as4 主题
-
3-1-compression-encryption-and-hashing as3 主题
-
3-2-databases as3 主题
-
3-3-networks as8 主题
-
3-4-web-technologies as13 主题
-
html as
-
css as
-
css-styling as
-
javascript as
-
variables-and-constants-in-javascript as
-
outputs-in-javascript as
-
selection-in-javascript- as
-
for-loops-in-javascript- as
-
while-loops-in-javascript- as
-
strings-in-javascript- as
-
operators-in-javascript- as
-
nested-statements-in-javascript as
-
functions-and-procedures-in-javascript as
-
html as
-
4-1-data-types as8 主题
-
4-2-data-structures as4 主题
-
4-3-boolean-algebra as1 主题
-
5-1-computing-related-legislation as4 主题
-
5-2-moral-and-ethical-issues as9 主题
-
6-1-thinking-abstractly as3 主题
-
6-2-thinking-ahead as1 主题
-
6-3-thinking-procedurally as3 主题
-
6-4-thinking-logically as2 主题
-
6-5-thinking-concurrently as2 主题
-
7-1-programming-techniques as9 主题
-
8-1-standard-algorithms-and-big-o-notation as8 主题
stacks as
Exam code:H046
Stacks
What is a stack?
-
Stacks are a last in first out (LIFO) data structure
-
Items can only be added to or removed from the top of the stack
-
Stacks are key structures in the computing world as they are used to reserve an action, such as go back a page or undo
-
A real-life example of a stack would be a pile (or stack) of plates at a buffet; you can only take a plate from the top, and if you need to reach the bottom one you have to remove all the others first
-
It is often implemented using an array
-
Where the maximum size required is known in advance, static stacks are preferred as they are easier to implement and make more efficient use of memory
-
A stack has 6 main operations, which can be seen below
|
Operation |
Description |
|---|---|
|
|
This checks is the stack is empty by checking the value of the top pointer |
|
|
This adds a new value to the end of the list, it will need to check the stack is not full before pushing to the stack. |
|
|
This returns the top value of the stack. First check the stack is not empty by looking at the value of the top pointer. |
|
|
This removes and returns the top value of the stack. This first checks if the stack is not empty by looking at the value of the top pointer. |
|
|
Returns the size of the stack |
|
|
Checks if the stack is full and returns the Boolean value, this compares the stack size to the top pointer. |
-
Stacks use a pointer which points to the top of the stack, where the next piece of data will be added (pushed) or the current piece of data can be removed (popped)

Adding & Removing Data From a Stack
Pushing data to a stack
-
When pushing (adding) data to a stack, the data is pushed to the position of the pointer
-
Once pushed, the pointer will increment by 1, signifying the top of the stack
-
Since the stack is a static data structure, an attempt to push an item on to a full stack is called a stack overflow
Visual example
-
This would push Bunny onto the empty stack

-
This would push Dog to the top of the stack

-
This would push Ant to the top of the stack

Popping data from a stack
-
When popping (removing) data from a stack, the data is popped from the position of the pointer
-
Once popped, the pointer will decrement by 1, to point at the new top of the stack
-
Since the stack is a static data structure, an attempt to pop an item from an empty stack is called a stack underflow
Visual example
-
This would pop Ant From the top of the stack. The new top of the stack would become Dog

-
Note that the data that is ‘popped’ isn’t necessarily erased; the pointer ‘top’ moves to show that Ant is no longer in the stack and depending on the implementation, Ant can be deleted or replaced with a null value, or left to be overwritten
Worked Example
Stacks and queues are both data structures.
A stack is shown below before a set of operations are carried out on it.
Draw what the stack shown below would look like after the following operations:
pop(), push(“A”), push(“B”), pop(), push(“E”), push(“F”)
|
Pointer |
Data |
|
→ |
Z |
|
|
Y |
|
|
X |
2 marks
Answer:
-
Start by using the operation pop() to remove Z from the top of the stack
|
Pointer |
Data |
|
→ |
Y |
|
|
X |
-
Use the operation push(“A”) to add A to the top of the stack
|
Pointer |
Data |
|
→ |
A |
|---|---|
|
|
Y |
|
|
X |
-
Then use the operation push(“B”) to add B to the top of the stack
|
Pointer |
Data |
|
→ |
B |
|---|---|
|
|
A |
|
|
Y |
|
|
X |
-
Then use pop() to remove the top item in the stack
|
Pointer |
Data |
|
→ |
A |
|---|---|
|
|
Y |
|
|
<p class=”tex |
Responses