Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
Implementing a stack
How do you program a stack?
-
To recap the main operations of stacks and how they work, read the ADT overview
-
When programming a stack, the main operations listed below must be included in your program.
-
The main operations for stacks are:
Main operations
|
Operation |
Pseudocode |
Python |
Java |
|---|---|---|---|
|
isEmpty() |
|
|
|
|
isFull() |
|
|
|
|
push(value) |
|
|
|
|
pop() |
|
|
|
|
peek() |
|
|
|
|
size() |
|
|
|
-
In pseudocode, we assume a record-based stack with fields like
items,top, andcapacity -
Python uses built-in lists or a class with
.push()and.pop()methods -
Java uses
Stack<E>fromjava.util.Stack
Stack in operation (Pseudocode)
TYPE Stack DECLARE items : ARRAY[0:4] OF INTEGER DECLARE top : INTEGER DECLARE capacity : INTEGER
ENDTYPE // Initialise stack
DECLARE s : Stack
s.top ← -1
s.capacity ← 5 // Push 3 values onto the stack
CALL push(s, 10)
CALL push(s, 20)
CALL push(s, 30) // Peek at the top item
OUTPUT "Top item is ", peek(s) // Pop 2 values from the stack
DECLARE val : INTEGER
val ← pop(s)
OUTPUT "Popped value: ", val val ← pop(s)
OUTPUT "Popped value: ", val // Output current stack size
OUTPUT "Current size: ", size(s)
-
Defines a Stack structure with a fixed-size array of 5 items, a
toppointer, and acapacity -
Initialises the stack with
top ← -1to indicate it is empty -
Pushes 3 values (
10,20,30) onto the stack one by one:-
Each
pushincreasestopand adds the value to theitemsarray
-
-
Peeks at the current top of the stack:
-
Returns
30without removing it
-
-
Pops two values off the stack:
-
First pop returns
30, second returns20 -
Each
popdecreasestopby 1
-
-
Outputs the current size of the stack:
-
After popping twice, only one value (
10) remains → size is1
-
Python
class Stack: def __init__(self, capacity): self.stack = [] self.capacity = capacity def isEmpty(self): return len(self.stack) == 0 def isFull(self): return len(self.stack) == self.capacity def push(self, value): if not self.isFull(): self.stack.append(value) else: print("Stack is full") def pop(self): if not self.isEmpty(): return self.stack.pop() else: return None def peek(self): if not self.isEmpty(): return self.stack[-1] else: return None def size(self): return len(self.stack) # Stack in operation
s = Stack(5) # Push 3 values
s.push(10)
s.push(20)
s.push(30) # Peek at top
print("Top item is", s.peek()) # Pop 2 values
print("Popped value:", s.pop())
print("Popped value:", s.pop()) # Output current size
print("Current size:", s.size())
Java
import java.util.Stack; public class StackExample { public static void main(String[] args) { Stack<Integer> s = new Stack<>(); // Push 3 values s.push(10); s.push(20); s.push(30); // Peek at top System.out.println("Top item is " + s.peek()); // Pop 2 values System.out.println("Popped value: " + s.pop()); System.out.println("Popped value: " + s.pop()); // Output current size System.out.println("Current size: " + s.size()); }
}
Worked Example
Study the pseudocode function PushAnimal():
FUNCTION PushAnimal(DataToPush : STRING) RETURNS BOOLEAN IF AnimalTopPointer = 20 THEN RETURN FALSE ELSE Animal[AnimalTopPointer] ← DataToPush AnimalTopPointer ← AnimalTopPointer + 1 RETURN TRUE ENDIF
ENDFUNCTION
Write program code for the function PushAnimal()[3]
Answer
-
Function header (and close where appropriate) with parameter, checking if full (
AnimalTopPointer = 20) and returning false [1 mark] -
If not full, inserting parameter value into
AnimalTopPointer[1 mark] -
…incrementing pointer and returning true [1 mark]
Example program code:
Java
public static Boolean PushAnimal(String DataToPush){ if(AnimalTopPointer == 20){ return false; }else{ Animal[AnimalTopPointer] = DataToPush; AnimalTopPointer++; return true; }
}
VB.NET
Function PushAnimal(DataToPush) If AnimalTopPointer = 20 Then Return False Else Animal(AnimalTopPointer) = DataToPush AnimalTopPointer = AnimalTopPointer + 1 Return True End If
End Function
Python
def PushAnimal(DataToPush): global AnimalTopPointer global ColourTopPointer if AnimalTopPointer == 20: return False else: Animal.append(DataToPush) AnimalTopPointer +=1 return True
Responses