Lecture4 Csc0015 Module4 Week4
Lecture4 Csc0015 Module4 Week4
Stacks
• Learn about stacks
• Learn about different operations on stacks
• Learn difference between a static and dynamic stack.
• Understand how to implement a static and dynamic
stack.
• Know the existing C++ STL container stack.
• Learn how to apply and use this STL in programs.
• The Stack ADT
• Applications of Stack
• Stack Operations
• Types of Stack
• Static Stack
• Dynamic Stack
• The STL stack Container
• A stack is a data structure that stores and retrieves
items in a last-in-first-out (LIFO) manner.
• Computer systems use stacks during a
program’s execution to store function return
addresses, local variables, etc.
• Some calculators use stacks for performing
mathematical operations.
• Push
• causes a value to be stored in (pushed onto)
the stack
• Pop
• retrieves and removes a value from the
stack
• Suppose we have an empty integer stack that is
capable of holding a maximum of three values. With
that stack we execute the following push operations.
push(5);
push(10);
push(15);
The state of the stack after each of the
push operations:
Now, suppose we execute three consecutive
pop operations on the same stack:
• isFull: A Boolean operation needed for
static stacks. Returns true if the stack is full.
Otherwise, returns false.
• isEmpty: A Boolean operation needed for all
stacks. Returns true if the stack is empty.
Otherwise, returns false.
• Wittenberg, Lee.(2018). Data structures and algorithms in C++. s.l.: Mercury
Learning
• Baka, Benjamin(2017). Python data structures and algorithms : improve the
performance and speed of your applications. Birmingham, U.K : Packt
Publishing
• Downey, Allen.(2017). Think data structures : algorithms and information
retrieval in Java. Sebastopol, CA: O'Reilly
• Chang, Kung-Hua(2017). Data Structures Practice Problems for C++
Beginners. S.l : Simple and Example
• Hemant Jain(2017). Problem Solving in Data Structures & Algorithms Using
C++: Programming Interview Guide. USA: CreateSpace Independent
Publishing Platform
• Malik, D.S. (2018). C++ Programming: Program Design Including Data
Structures. USA: Cengage Learning
• Static Stacks
• Fixed size
• Can be implemented with an array
• Dynamic Stacks
• Grow in size as needed
• Can be implemented with a linked list
Member Variable Description
stackArray The class constructor accepts an integer argument, which specifies the
size of the stack. An integer array of this size is dynamically
allocated, and assigned to stackArray. Also, the variable top is
initialized to –1.
push The push function accepts an integer argument, which is pushed onto
the top of the stack.
pop The pop function uses an integer reference parameter. The value at
the top of the stack is removed, and copied into the reference
parameter.
Member Function Description
isFull Returns true if the stack is full and false otherwise. The stack is
full when top is equal to stackSize – 1.
isEmpty Returns true if the stack is empty, and false otherwise. The stack
is empty when top is set to –1.
Contents of IntStack.h
class IntStack
{
private:
int *stackArray;
int stackSize;
int top;
public:
IntStack(int size);
void push(int num);
void pop(int &num);
bool isFull();
bool isEmpty();
};
Contents of IntStack.cpp
#include <iostream>
#include "intstack.h"
using namespace std;
//*******************
// Constructor *
//*******************
IntStack::IntStack(int size)
{
stackArray = new int[size];
stackSize = size;
top = -1;
}
Contents of IntStack.cpp
//*************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************
bool IntStack::isFull()
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
Contents of IntStack.cpp
//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
bool IntStack::isEmpty()
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
Main Program
// This program demonstrates the IntStack class.
#include <iostream>
#include "intstack.h“
using namespace std;
int main()
{
IntStack stack(5);
int catchVar;
public:
DynIntStack()
{ top = NULL; }
void push(int num);
void pop(int &num);
bool isEmpty();
};
Contents of DynIntStack.cpp
#include <iostream>
#include "dynintstack.h“
using namespace std;
//************************************************
// Member function push pushes the argument onto *
// the stack. *
//************************************************
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************
Contents of DynIntStack.cpp
void DynIntStack::pop(int &num)
{
stackNode *temp;
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
Contents of DynIntStack.cpp
//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
bool DynIntStack::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
Main Program
// This program demonstrates the dynamic stack
// class DynIntClass.
#include <iostream>
#include "dynintstack.h“
using namespace std;
int main()
{
DynIntStack stack;
int catchVar;
Program Output
Pushing 5
Pushing 10
Pushing 15
Popping...
15
10
5
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main()
{
int x;
stack< int, vector<int> > iStack;
for (x = 2; x < 8; x += 2)
{
cout << "Pushing " << x << endl;
iStack.push(x);
}
Sample Program
cout << "The size of the stack is ";
cout << iStack.size() << endl;
for (x = 2; x < 8; x += 2)
{
cout << "Popping " << iStack.top() << endl;
iStack.pop();
}
}
Program Output
Pushing 2
Pushing 4
Pushing 6
The size of the stack is 3
Popping 6
Popping 4
Popping 2
• Wittenberg, Lee.(2018). Data structures and algorithms in C++. s.l.: Mercury
Learning
• Baka, Benjamin(2017). Python data structures and algorithms : improve the
performance and speed of your applications. Birmingham, U.K : Packt
Publishing
• Downey, Allen.(2017). Think data structures : algorithms and information
retrieval in Java. Sebastopol, CA: O'Reilly
• Chang, Kung-Hua(2017). Data Structures Practice Problems for C++
Beginners. S.l : Simple and Example
• Hemant Jain(2017). Problem Solving in Data Structures & Algorithms Using
C++: Programming Interview Guide. USA: CreateSpace Independent
Publishing Platform
• Malik, D.S. (2018). C++ Programming: Program Design Including Data
Structures. USA: Cengage Learning