CHAPTER 3
Data Structures
Algorithm 3.2.2 Initializing a Stack
This algorithm initializes a stack to empty. An empty stack has t = -1.
Input Parameters: None
Output Parameters: None
stack_init() {
t = -1
}
Algorithm 3.2.3 Testing for an Empty
Stack
This algorithm returns true if the stack is empty or false if the stack is not
empty. An empty stack has t = -1.
Input Parameters: None
Output Parameters: None
empty() {
return t == -1
}
Algorithm 3.2.4 Adding an Element to a
Stack
This algorithm adds the value val to a stack. The stack is represented
using an array data. The algorithm assumes that the array is not full. The
most recently added item is at index t unless the stack is empty, in which
case, t = -1.
Input Parameters: val
Output Parameters: None
push(val) {
t = t + 1
data[t] = val
}
Algorithm 3.2.5 Removing an Element
From a Stack
This algorithm removes the most recently added item from a stack. The
algorithm assumes that the stack is not empty. The most recently added
item is at index t.
Input Parameters: None
Output Parameters: None
pop() {
t = t – 1
}
评论0