Lecture 16 - Stacks
Lecture 16 - Stacks
12. Stacks
12-Stacks 1
Stack
• A stack is a special kind of list
– Insertion and deletions takes place at one end called top
• Other names
– Push down list
– Last In First Out (LIFO)
12-Stacks 2
Stack Examples
• Books on floor
• Dishes on a shelf
12-Stacks 3
Stack ADT
• Stack ADT emphasizes specific operations
– Top of the stack is the most recent object pushed onto the
stack
12-Stacks 4
Stack ADT – Operations (1)
• Graphically, the stack operations are viewed as follows:
12-Stacks 5
Stack ADT – Operations (2)
• MAKENULL(S)
– Make Stack S be an empty stack
• TOP(S)
– Return the element at the top of stack S
• POP(S)
– Remove the top element of the stack
• PUSH(S,x)
– Insert the element x at the top of the stack
• EMPTY(S)
– Return true if S is an empty stack and return false otherwise
12-Stacks 6
Push and Pop Operations of Stack
12-Stacks 7
Applications (1)
• Many applications
– Parsing code
Matching parenthesis
XML (e.g., XHTML)
– Tracking function calls
– Dealing with undo/redo operations
12-Stacks 8
Use of Stack in Function Calls (1)
• When a function begins execution an activation record is
created to store the current execution environment for that
function
12-Stacks 10
Use of Stack in Function Calls (2)
• Each invocation of a function has its own activation record
12-Stacks 11
Runtime Stack Example (1)
void main(){
int a=3;
f1(a); // statement A
cout << endl;
}
12-Stacks 12
Runtime Stack
• When a function is called …
– Copy of activation record pushed onto run-time stack
– Arguments copied into parameter spaces
– Control transferred to starting address of body of function
Function
Return Local
Parameter value
value variables Return
s address
12-Stacks 13
Runtime Stack Example (2)
void main(){
int a=3;
f1(a); // statement A
cout << endl;
}
12-Stacks 14
Static and Dynamic Stacks
• Two possible implementations of stack data structure
12-Stacks 15
Array-based Implementation
12-Stacks 16
Array Implementation – First Solution (1)
• Elements are stored in contiguous cells of an array
Last Element
Empty
maxlengt
h
12-Stacks 17
Array Implementation – First Solution (2)
1
3
2
2
1
1
• Problem
– Every PUSH and POP requires moving the entire array up and
down
12-Stacks 18
Array Implementation – Better Solution (2)
Empty
Idea
• Anchor the bottom of the stack at the bottom of the array
• Let the stack grow towards the top of the array
• Top indicates the current position of the first stack element
12-Stacks 19
Array Implementation – Code (1)
class IntStack
{
private:
int *stackArray;
int stackSize;
int top;
public:
IntStack(int);
~IntStack( );
bool push(int);
bool pop(int &);
bool isFull();
bool isEmpty();
};
12-Stacks 20
Array Implementation – Code (2)
• Constructor
IntStack::IntStack(int size) //constructor
{
stackArray = new int[size];
stackSize = size;
top = -1;
}
• Destructor
IntStack::~IntStack(void) //destructor
{
delete [] stackArray;
}
12-Stacks 21
Array Implementation – Code (3)
• isFull function
bool IntStack::isFull(void)
{
if (top == stackSize - 1)
return true;
else
return false;
// return (top == stackSize-1);
}
• isEmpty function
bool IntStack::isEmpty(void)
{
return (top == -1);
}
12-Stacks 22
Array Implementation – Code (4)
• push function inserts the argument num onto the stack
top++;
stackArray[top] = num;
return true;
}
12-Stacks 23
Array Implementation – Code (5)
• Pop function removes the value from top of the stack and
returns it as a reference
num = stackArray[top];
top--;
return true;
}
12-Stacks 24
Using Stack (1)
int main()
{
IntStack stack(4);
}
12-Stacks 25
Using Stack (2)
int main()
{
IntStack stack(4);
int catchVar;
}
12-Stacks 26
Using Stack (3)
int main()
{
IntStack stack(4); num 20
int catchVar;
}
12-Stacks 27
Using Stack (4)
int main()
Output:
{
IntStack stack(4); Pushing Integers
int catchVar; Popping…
20
cout << "Pushing Integers\n"; 15
stack.push(5); 10
stack.push(10);
stack.push(15);
5
stack.push(20);
12-Stacks 29
Pointer-based Implementation of Stacks
• Stack can expand or shrink with each push or pop
operation
• Push and pop operate only on the head node, i.e., the first
node of the list
Top x y z .
12-Stacks 30
Pointer Implementation – Code (1)
class Stack
{
struct node
{
int data;
node *next;
}*top;
public:
Stack();
~Stack();
void Push(int newelement);
bool Pop(int &);
bool IsEmpty();
void makeNull();
};
12-Stacks 31
Pointer Implementation – Code (2)
• Constructor
Stack::Stack()
{
top = NULL;
}
bool Stack::IsEmpty()
{
return (top == NULL);
}
12-Stacks 32
Pointer Implementation – Code (3)
• Push function inserts a node at the top/head of the stack
newptr->next=top;
top=newptr;
}
12-Stacks 33
Pointer Implementation – Code (4)
• Pop function deletes the node from the top of the stack
and returns its data by reference
delete tempptr;
return true;
}
12-Stacks 34
Pointer Implementation – Code (5)
• Destructor
Stack::~Stack()
{
makeNull();
}
void Stack::makeNull()
{
int x;
while( Pop(x) );
}
10-Queues 35
Any Question So Far?
12-Stacks 36