Help me to fix a silly problem in stack
Sep 14, 2013 at 12:52pm UTC
I am almost done with the following code; need a single argument to find the desired output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <iostream>
#include <ctime>
using namespace std;
class stack{
private :
int *st;
int s;
int top;
public :
stack(int );
//void set_values();
//void get_values();
void push(int );
void pop(int &);
};
stack::stack(int x)
{
s=x;
top=0;
st=new int [s];
}
/*
void stack::set_values()
{
for(int i=0; i<s; ++i)
st[i]=rand()%10;
}
void stack::get_values()
{
for(int i=0; i<s; ++i)
cout<<st[i]<<endl;
}
*/
void stack::push(int x)
{
if (top==s)
cout<<"Stack full.\n" ;
else {
st[top]=x;
cout<<st[top]<<" " ;
++top;
}
}
void stack::pop(int &x)
{
if (top==0)
cout<<"Stack empty.\n" ;
else {
st[top]=x;
cout<<st[top]<<" " ;
--top;
}
}
int main()
{
stack s1(10);
srand (time(0));
//sc.set_values();
//sc.get_values();
cout<<"Pushed values are:\n" ;
for (int i=0; i<10; ++i)
s1.push(rand()%10);
cout<<"\n\nPoped values are:\n" ;
for (int j=0; j<10; ++j)
s1.pop();
cout<<endl;
system ("Pause" );
return 0;
}
In the main()[line no.72], what should I put here as argument...
...so that I can send the pushed values to the function...
1 2 3 4 5 6 7 8 9 10
void stack::pop(int &x)
{
if (top==0)
cout<<"Stack empty.\n" ;
else {
st[top]=x;
cout<<st[top]<<" " ;
--top;
}
}
...and the function will cout the
poped values .
Sep 14, 2013 at 1:49pm UTC
Better, why use this function when you can create a new stack frame using assembly then use push\pop assembly instructions in order to add values to the stack.
Topic archived. No new replies allowed.