Stack = Last one first
ADT = abstract data type (user defined data type)
Info is hidden when things are abstract.
for (int i = 0, i < 6, i++)
cin >> table[i];
for (i=5,i >= 0 , i--)
- Everything is a character (every strike on a
keyboard is a character)
Assume the following:
23
char t;
cin.get (t)
cout <, t //will give u the
value 2
void main()
{
int num;
char t;
cin.get(t)
if t(>=’0’) && (t<=’9’){
cin >> num;
cout << “number”;
else cout << “not number”;
// output = 3
ENTER A POSTFIX NOTATION
//ENTER A POSTFIX NOTATION
#include<iostream.h>
void push (int stack[],int &top,int x){
stack[top++]=x;
}//push
int pop (int stack [],int &top){
return
stack [--top]; }//pop
int evaluate(int num1, int num2, char t){
switch(t){
case'+':return
num2+num1;break;
case'-':return
num2-num1;break;
case'/':return
num2/num1;break;
case'*':return
num2*num1;break;
}//switch
}//evaluate
int main (){
int
stack [10],top=0,x,num1,num2,value;
char t;
cout<<"please
enter a postfix notation"<<endl;
while(cin.get(t)){
if((t>='0')&&(t<='9')){
cin.putback(t);cin>>x;
push(stack,top,x);}//if
else
if ((t=='+') || (t=='-')||(t=='/')||(t=='*')){
num1=pop(stack,top);
num2=pop(stack,top);
value=evaluate
(num1,num2,t);
push
(stack,top,value);
}//else
if
if
(t=='\n') break;
}//while
cout<<pop(stack,top);
return
0;
}//main
//OUTPUT IS 12 5 3 + *
Choice b is a better form of programming because it
has structure to it.
Infix Evaluation
2 3 4 * +
Algorithm = If digit then PUSH
else
if
operator
pop
pop
evaluate
push
else
no more, pop
DISPLAY
How would make a
stack?
Array





176 786 664
0 1
2 3
Stack
number
[0] = 176
number
[1] = 786
number
[2] = 664
number
[i]
i++;
stack
[i]
i++;
change
i to top
stack[top]
= X
top++;
1)
1)
Write a
function called Push?
Int stack [10];
Int top = 0;
Int x;
Push (){
Stack [top] = x
Top++;
}
main()
{
push();
}//main
2)
2)
2 ways
to use stack are Array and Pointer
SIMPLE STACK MENU
#include<iostream.h>
void push (int mystack[],int & mytop,int item){
mystack[mytop]=item;
mytop=mytop+1;}
//PUSH
int pop(int mystack[],int &mytop){
mytop=mytop-1;
int
item=mystack[mytop];
return
item; }//POP
int isempty(int mytop){
if(mytop==0)
return 1;
else
return 0;}//ISEMPTY
int isfull(int mytop,const int MAXSTACKSIZE){
if(mytop==MAXSTACKSIZE)return
1;
else
return 0;}//ISFULL
void main(){
const
int MAXSTACKSIZE=5;
int
mystack[MAXSTACKSIZE];int mytop,x,option;
mytop=0;//STACK
TOP INITILIZATION
do{cout<<"SELECT
ONE OF THE FOLLOWING OPTIONS:"<<endl;
cout<<"1-PUSH
2-POP 3-EMPTY 4-ISFULL 5-QUIT:";
cin>>option;
switch(option){
case 1:
if (isfull(mytop,MAXSTACKSIZE))
cout<<"YOU
CANT PUSH"<<endl;
else{
cout<<"ENTER THE NUMBER THAT WILL BE PUSHED:";
cin>>x;
push(mystack,mytop,x);}//ELSE
break;
case 2:
if(isempty(mytop))
cout<<"YOU
CANT POP"<<endl;
else{
cout<<"THE NUMBER POOPED IS:"<<pop(mystack,mytop);
cout<<endl;}//ELSE
break;
case 3:
if(isempty(mytop)==1)
cout<<"STACK
EMPTY."<<endl;
else
cout<<"STACK IS NOT EMPTY."<<endl;
break;
case 4:
if(isfull(mytop, MAXSTACKSIZE)==1)
cout<<"STACK
FULL."<<endl;
else
cout<<"STACK IS NOT FULL."<<endl;
break;
case 5:
cout<<"THANK YOU FOR USING OUR MENU!"<<endl;
break;
default:cout
<<"ENTER CORRECT OPTIONS"<<endl;}//switch
}while
(option !=5);
}//MAIN