Professional Documents
Culture Documents
Stacks
Abstract Data Types (ADTs)
2
ADT For Stock Trade
The data stored are buy/sell orders
The operations supported are
order buy (stock, shares)
order sell(stock, shares )
void cancel(order)
Error conditions:
Buy/sell a nonexistent stock
Cancel a nonexistent order
3
Abstract Data Types
This is like having a "black box" for storing data.
You know what you can do with the data inside, but you don't
need to know how it's implemented.
Imagine a vending machine - you don't need to know how it
works inside; you just know you put money in, press a button,
and get a snack. In the same way, abstract data types let you
work with data without worrying about the nitty-gritty details
of how it's stored or managed.
Abstract Data Types
The user of data type does not need to know how that data
type is implemented
For example, we have been using Primitive values like int,
float, char data types only with the knowledge that these
data type can operate and be performed on without any
idea of how they are implemented.
ADT: Array
Data
A[0..99] : integer array
0 1 2 3 4 5 6 7 99
A 2 1 3 3 2 9 9 6 … 10
…
0 1 2 99
6
ADT: Array
Operations:
create
Insert value
Error Condition
Index out of bound
Null of uninitialized array
7
Stack
Pop Pop
Push
Stack
Linear Data Structure
Controlled by two operations: push and pop
Both operations take place from one end of stack usually
called Top.
Top points to current top position of stack.
Push operation adds an element to the top of stack.
Pop operation add an element to the top of stack.
The two operations implement the LIFO method.
What is this good for ?
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Saving local variables when one function calls another,
and this one calls another
Stack ADT
Data:
A finite sequence of nodes
Operations:
Create
Push: Insert element at top
Top: Return top element
Pop: Remove and return top element
IsEmpty: test for emptyness
Stack: ADT
Error Condition
operations pop and top cannot be performed if the stack is
empty
Exercise: Stacks
Describe the output of the following series of stack
operations
Push(8)
Push(3)
Pop()
Push(2)
Push(5)
Pop()
Pop()
Push(9)
Push(1)
Stacks
Stack can be implemented in two ways
Arrays
Linked List
Stack as an Array
Push Operation
Push Operation
Pop Operation
Pop Operation
Implementation
int arr[100], int top;
Stack() {
top = -1;}
push(int value) {
if (top >= MAX_SIZE - 1):
Print (stack is full)
else:
arr[++top] = value;}
}
Implementation
int pop() {
if (isEmpty()):
print stack is empty
else:
return arr[top--];
}
Implementation
int topValue() {
if (isEmpty()):
return -1;
else:
return arr[top];
}
bool isEmpty() {
return top == -1;
}
Implementation
int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
while (!stack.isEmpty())
print stack.top();
stack.pop();
}}
Stack
Printing Stack
Swap Stack
stack1.swap(stack2);
Stack.size();
Application of Stack
TO DO TASK
Write a C++ program that reverses the stack (using an
array) elements.
Write a C++ program to calculate the average value of the
stack (using an array) elements.
Write a C++ program to find the maximum element in a
stack (using an array).
Write a C++ program to find and remove the largest
element in a stack.
Write a C++ program to find the middle element of a
stack (using an array).