Академический Документы
Профессиональный Документы
Культура Документы
Definition of Stack Specifications for Stack Implementations of Stack Linked Stack Contiguous Stack Applications of Stack
1
LIFO (Stack)
Stack ADT
DEFINITION: A Stack of elements of type T is a finite sequence of elements of T, in which all insertions and deletions are restricted to one end, called the top. Stack is a Last In - First Out (LIFO) data structure. Basic operations: Construct a stack, leaving it empty. Push an element. Pop an element. Top an element.
3
top
push data
top
top
top
pop data
top
pop data
top
top data
top
top data
10
Implementations of Stack
Contiguous Implementation: use an array.
(May be Automatically or Dynamically Allocated Array)
11
Linked Stack
count 4
top
top
top ? count = ?
top count = 0
13
2. Update pointers and count: pNew->link = top (1) Point the new node to the top node. top = pNew (2) Point top to the new node. count = count + 1
count n+1 top
(2) pNew X X (1)
14
count 1 top
pNew
15
16
pNew
17
2.
3.
count 0 top
pDel
19
Pop Algorithm
<ErrorCode> Pop() Pops an element from the top of the stack Pre none Post If the stack is not empty, the element on the top has been removed; otherwise, the stack remains unchanged. Return success or underflow.
20
pDel
21
push
Physical
push push
top
data
n-2 n-1
max-2 max-1
x x x x
x x
pop pop
Conceptual
Stack top <integer> count <integer> data <array of <DataType>> End Stack
25
Stack is empty
top 0
top n-1
26
Create Stack
<void> Create()
// Specifications here are similar to specifications for Linked Stack
27
Push Stack
<ErrorCode> Push(val DataIn <DataType>)
Pop Stack
<ErrorCode> Pop()
Top Stack
<ErrorCode> Top(ref DataOut <DataType>)
30
Stack status
<boolean> isEmpty() 1. if (count = 0) 1. return TRUE 2. Else 1. return FALSE end isEmpty <boolean> isFull() 1. if (count = maxsize) 1. return TRUE 2. Else 1. return FALSE end isFull