Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
146 views

Lecture4 Csc0015 Module4 Week4

The document discusses stacks and their implementation in C++. It covers: 1) Static and dynamic stacks, with static stacks using arrays and dynamic stacks using linked lists. 2) Common stack operations like push, pop, isEmpty, and isFull. 3) Implementing both static and dynamic stacks as C++ classes, with member variables and functions for each operation.

Uploaded by

NoLife AJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

Lecture4 Csc0015 Module4 Week4

The document discusses stacks and their implementation in C++. It covers: 1) Static and dynamic stacks, with static stacks using arrays and dynamic stacks using linked lists. 2) Common stack operations like push, pop, isEmpty, and isFull. 3) Implementing both static and dynamic stacks as C++ classes, with member variables and functions for each operation.

Uploaded by

NoLife AJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

MODULE 4

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 A pointer to int. When the constructor is executed, it uses


stackArray to dynamically allocate an array for storage.

stackSize An integer that holds the size of the stack.

top An integer that is used to mark the top of the stack.


Member Function 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. *
//*************************************************

void IntStack::push(int num)


{
if (isFull())
{
cout << "The stack is full.\n";
}
else
{
top++;
stackArray[top] = num;
}
}
Contents of IntStack.cpp
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************

void IntStack::pop(int &num)


{
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else
{
num = stackArray[top];
top--;
}
}
Contents of IntStack.cpp
//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//***************************************************

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;

cout << "Pushing 5\n";


stack.push(5);
cout << "Pushing 10\n";
stack.push(10);
cout << "Pushing 15\n";
stack.push(15);
cout << "Pushing 20\n";
stack.push(20);
cout << "Pushing 25\n";
stack.push(25);
Main Program (continued)
cout << "Popping...\n";
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
}
Program Output
Pushing 5
Pushing 10
Pushing 15
Pushing 20
Pushing 25
Popping...
25
20
15
10
5
• In the program, the constructor is called with the
argument 5. This sets up the member variables as
shown below. Since top is set to –1, the stack is
empty
• The figure below shows the state of the member
variables after the push function is called the first time
(with 5 as its argument). The top of the stack is now at
element 0.
• The figure below shows the state of the member
variables after all five calls to the push function. Now
the top of the stack is at element 4, and the stack is
full.
• Notice that the pop function uses a reference
parameter, num. The value that is popped off the stack
is copied into num so it can be used later in the
program. The figure on the next slide depicts the state
of the class members, and the num parameter, just
after the first value is popped off the stack.
• A dynamic stack is built on a linked list instead of an
array.
• A linked list-based stack offers two advantages over
an array-based stack.
• No need to specify the starting size of the stack. A dynamic
stack simply starts as an empty linked list, and then expands by
one node each time a value is pushed.
• A dynamic stack will never be full, as long as the system has
enough free memory.
Contents of DynIntStack.h
class DynIntStack
{
private:
struct StackNode
{
int value;
StackNode *next;
};
StackNode *top;

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. *
//************************************************

void DynIntStack::push(int num)


{
stackNode *newNode;

// Allocate a new node & store Num


newNode = new stackNode;
newNode->value = num;
Contents of DynIntStack.cpp
// If there are no nodes in the list
// make newNode the first node
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top
{
newNode->next = top;
top = newNode;
}
}

//****************************************************
// 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;

cout << "Pushing 5\n";


stack.push(5);
cout << "Pushing 10\n";
stack.push(10);
cout << "Pushing 15\n";
stack.push(15);
Main Program
cout << "Popping...\n";
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
cout << "\nAttempting to pop again... ";
stack.pop(catchVar);
}

Program Output
Pushing 5
Pushing 10
Pushing 15
Popping...
15
10
5

Attempting to pop again... The stack is empty.


• The STL stack container may be implemented as a
vector, a list, or a deque (which you will learn
about later in this chapter).
• Because the stack container is used to adapt
these other containers, it is often referred to as a
container adapter.
• Here are examples of how to declare a stack of
ints, implemented as a vector, a list, and a
deque.
• stack< int, vector<int> > iStack; // Vector stack
• stack< int, list<int> > iStack; // List stack
• stack< int > iStack; // Default – deque stack
The STL Stack Container
Sample Program
// This program demonstrates the STL stack
// container adapter.

#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

You might also like