Reading
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Stack
---------------------------------------------------------------------------------------------------------------------------------------------------------------
A stack is a very simple container
it is first in last out (FILO)
imagine you have to store some numbers and you write them on a plate and stack them up
you add 5, then 50 , then 7
you now have 3 plates the bottom one has 5 written on it, on top of that is the plate with 50 on it and finally on top is the plate with 7
this is a stack , the rules are you can only add or remove to the top of the stack.
you can add as many values as you like but the only one you can take off is the one at the top.
if you want to get to the one at the bottom of the stack you must remove the top plate over and over until you get to the last one
so this is why it is FILO the first value inserted is the last one to be removed.
adding a value is called a PUSH
removing a value is called a POP
a stack is commonly the first type of container to be disucssed in programing as it is the simplest to implement
all you need is an array and an integer counter
File: simple_stack.cpp
class stack
{
int data[10];
int couter;
stack() { counter =0;}
int Top()
{
if (counter ==0 )
throw "the stack is empty";
return data[counter-1];
}
void Push(int value)
{
if (counter < 10)
{
data[counter]=value;
counter ++;
}
}
void Pop()
{
if (counter >0)
counter --;
}
int Size() {return counter;}
};
int main()
{
stack temp;
// counter is initialized to 0
// this is both the number of values in the stack as well as the next available index
cout << temp.Size() << endl; // displays 0
temp.Push(5); // stores 5 into the array data at index 0 (counter) , counter is incremented to 1
temp.Push(34); // stores 34 into the array data at index 1 (counter) , counter is incremented to 2
temp.Push(22); // stores 22 into the array data at index 2 (counter) , counter is incremented to 3
cout << temp.Size() << endl; // displays 3
cout << temp.Top() << endl ; // displays 22 the top value on the stack
temp.Pop(); // decrements counter to 2
temp.Push(88); // stores 88 into the array data at index 2 (counter) , counter is incremented to 3
cout << temp.Top() << endl ; // displays 88 the top value on the stack
temp.Pop(); // decrements counter to 2
cout << temp.Top() << endl ; // displays 34 the top value on the stack
temp.Pop(); // decrements counter to 1
cout << temp.Top() << endl ; // displays 5 the top value on the stack
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Macros
---------------------------------------------------------------------------------------------------------------------------------------------------------------
File: main2.cpp
#include "cmpslib19.h"
// include the standard template library stack
#include <stack>
#include "fraction.h"
// MACRO
// a macro is basically kinda like search and replace
// we have used __LINE__ and __PRETTY_FUNCTION before, these are provided by the compler
// __LINE__ is replaced by the actual line number of code
// you can define your own macros
#define Player_Lives 3
// anywhere we put Player_Lives in our code the compiler will replace it with 3 before it compiles your code
// we can also wrap a define in logic.. this will set ELEMTYPE if it has not already been set
#ifndef ELEMTYPE
#define ELEMTYPE int
#endif
// if you comment out this line the line with " x contains the value" will be skipped
#define DEBUG 1
int main()
{
ELEMTYPE temp;// temp will be of type int
cout << "temp is of type " << GetClassName(temp) << endl;
cout << "Player_Lives: " << Player_Lives << endl;
#ifdef DEBUG
cout << "if DEBUG is defined to any value this will show " << endl;
#endif
return 0;
}
if we compile like normal
g++ -Wall -o runme2 main2.cpp
./runme2
temp is of type int
Player_Lives: 3
if DEBUG is defined to any value this will show
temp is of type int the default for ELEMTYPE
but we can pass in a value when we compile it will use that
to do this we use the -D flag
g++ -Wall -o runme3 -D ELEMTYPE=CFraction main2.cpp
./runme3
temp is of type CFraction
Player_Lives: 3
if DEBUG is defined to any value this will show
NOTICE that temp is now CFraction instead of int
---------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------