create()
- create the array, initialize stack size.
empty()
- 0 == stack size
push()
- check for overflow, then add the element.
if stack size == stack capacity stack overflow data[stack size++] = new value
pop()
- check for underflow, then remove the element.
if stack size == 0 stack underflow data[--stack size]
top()
- check for underflow, then return the element.
if stack size == 0 stack underflow return data[stack size - 1]
if stack size == stack capacity stack capacity += 1000 new array = new ElementType [stack capacity] copy(new array, array) new array = array delete array
stack capacity *= 2
.
if stack capacity/stack size < 0.5 stack capacity /= 2 new array = new ElementType [stack capacity] copy(new array, array) array = new array delete [] array
create()
- initialize the dummy head.
head.next = null
empty()
- null == head.next
push()
head.next = new ElementNode(value, head.next)
pop()
- check for underflow, then remove the element.
if head.next == null stack underflow old = head.next head.next = old->next delete old
top()
- check for underflow, then return the element.
if head.next == null stack underflow return head.next->value
push(v) if free_list.next == null free_list.next = new ElementNode(v, null) node = free_list.next free_list.next = node->next node->next = head.next head.next = node pop() if null == head.next stack underflow node = head.next head.next = node->next node->next = free_list.next free_list.next = node
This page last modified on 13 November 2003.