Sep 11, 2013 at 12:56pm UTC
Whats wrong in the code?
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
#include <iostream>
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]<<endl;
}
}
void stack::pop(int &x)
{
if (top==0)
cout<"Stack empty.\n" ;
else
cout<<st[--top]<<endl;
}
int main()
{
stack sc(10);
//sc.set_values();
//sc.get_values();
for (int i=0; i<10; ++i){
sc.push(rand()%10);
}
cout<<endl;
for (int j=0; j<10; ++j){
sc.pop(j);
}
system ("Pause" );
return 0;
}
Last edited on Sep 11, 2013 at 12:58pm UTC
Sep 11, 2013 at 12:59pm UTC
Question is vague.
What's the wrong output. What are you getting/expecting?
Sep 11, 2013 at 1:16pm UTC
Ok coder. Plz run the code and see the output.
Sep 11, 2013 at 1:18pm UTC
ihutch, i'm expecting the poped values in order to pushed values.
Sep 11, 2013 at 1:46pm UTC
I understood what you said, coder. Anyway, thanks again for support .