Computer-Science-A-level-Ocr
-
3-3-networks8 主题
-
3-2-databases7 主题
-
3-1-compression-encryption-and-hashing4 主题
-
2-5-object-oriented-languages7 主题
-
2-4-types-of-programming-language4 主题
-
2-3-software-development5 主题
-
2-2-applications-generation6 主题
-
2-1-systems-software8 主题
-
1-3-input-output-and-storage2 主题
-
1-2-types-of-processor3 主题
-
1-1-structure-and-function-of-the-processor1 主题
-
structuring-your-responses3 主题
-
the-exam-papers2 主题
-
8-2-algorithms-for-the-main-data-structures4 主题
-
8-1-algorithms10 主题
-
7-2-computational-methods11 主题
-
7-1-programming-techniques14 主题
-
capturing-selecting-managing-and-exchanging-data
-
entity-relationship-diagrams
-
data-normalisation
-
relational-databases
-
hashing
-
symmetric-vs-asymmetric-encryption
-
run-length-encoding-and-dictionary-coding
-
lossy-and-lossless-compression
-
polymorphism-oop
-
encapsulation-oop
-
inheritance-oop
-
attributes-oop
-
methods-oop
-
objects-oop
-
capturing-selecting-managing-and-exchanging-data
-
6-5-thinking-concurrently2 主题
-
6-4-thinking-logically2 主题
-
6-3-thinking-procedurally3 主题
-
6-2-thinking-ahead1 主题
-
6-1-thinking-abstractly3 主题
-
5-2-moral-and-ethical-issues9 主题
-
5-1-computing-related-legislation4 主题
-
4-3-boolean-algebra5 主题
-
4-2-data-structures10 主题
-
4-1-data-types9 主题
-
3-4-web-technologies16 主题
-
environmental-effects
-
automated-decision-making
-
computers-in-the-workforce
-
layout-colour-paradigms-and-character-sets
-
piracy-and-offensive-communications
-
analysing-personal-information
-
monitoring-behaviour
-
censorship-and-the-internet
-
artificial-intelligence
-
the-regulation-of-investigatory-powers-act-2000
-
the-copyright-design-and-patents-act-1988
-
the-computer-misuse-act-1990
-
the-data-protection-act-1998
-
adder-circuits
-
flip-flop-circuits
-
simplifying-boolean-algebra
-
environmental-effects
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 |
Responses