Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
#include<bits stdc++.h="">

using namespace std;
int n=100;
class stAck
{
int*arr;int top;
public:
stAck(){
arr=new int[n]; top == -1;
}
void push(int x)
{
if(top == n-1)
{
cout<<" Stack overflow"<<endl; return="" ;
="" }
="" top++;
="" arr[top]="x;
" void="" pop()
="" {
="" if(top="=" -1)
="" cout<<"no="" elements="" to="" be="" popped"<<endl;
="" return;
="" top--;
=""
="" int="" top()
="" cout<<"="" no="" display"<<endl;="" -1;
="" arr[top];
="" bool="" empty()
="" top="=-1;
" }
};

int="" main()
{
="" stack="" st;
="" st.push(1);
="" st.push(2);
="" st.push(3);
="" cout<<st.top()<<endl;
="" st.pop();
="" st.pop();="" cout<<endl<<endl;
="" cout<<st.empty()<<endl;
="" 0;
}

<b="">What I have tried:

the if condition under pop is not working
Posted
Updated 6-Jul-21 22:14pm
Comments
Greg Utas 6-Jul-21 20:14pm    
No one can read this code. It wouldn't even compile. Most likely, something happened when you pasted it in. Please edit it so that it is legible. Just before the code starts, use <pre lang="c++">, and use </pre> where it ends.

If you clean up your code you can see that there is a function top that conflicts with the variable top. Rename the Variable to topi or something else.

Since the class stack already exists in c ++, I would think about a different name; e.g. mystack.

You should also think about whether the size of the field should be passed as a parameter to the constructor.

C++
class mystack {
public:
	mystack(int n);
	void push(int x);
	int pop();
	int top();
private:
	int* arr;
	int  topi;
	int  size;
};
 
Share this answer
 
v2
I have fixed the code for you
C++
#include <iostream>

using namespace std;

int n=100;
class stAck
{
  enum
  {
    SIZE = 100,
    EMPTY = -1
  };
  int * arr;
  int tp;
public:
  stAck()
  {
    arr = new int[SIZE];
    tp = EMPTY;
  }
  ~stAck()
  {
    delete [] arr;
  }

  void push(int x)
  {
    if (tp == (SIZE-1))
    {
      cout<<" Stack overflow"<<endl; return;
    }
    tp++;
    arr[tp]=x;
  }

  bool empty()
  {
    return ( tp == EMPTY);
  }

  void pop()
  {
    if ( empty() )
    {
      cout<<"no elements to be popped"<<endl;
      return;
    }
    tp--;
  }

  int  top()
  {
    if ( empty() )
    {
      cout<<"no item at the top of the empty stack" << endl;
      return EMPTY;
    }
    return arr[tp];
  }
}; //<- end of the stAck class


int main()
{
  stAck st;
  st.push(1);
  st.push(2);
  st.push(3);
  cout << st.top() << endl;
  st.pop();
  st.pop();
  cout<<st.empty()<<endl;
  st.pop();
  cout<<st.empty()<<endl;
}


You may also write
C++
#include <iostream>

using namespace std;

template <size_t N>
class stAck
{
  enum
  {
    EMPTY = -1
  };
  int arr[N];
  int tp;
public:
  stAck()
  {
    tp = EMPTY;
  }

  void push(int x)
  {
    if (tp == (N-1))
    {
      cout << " Stack overflow" << endl;
      return;
    }
    tp++;
    arr[tp] = x;
  }

  bool empty()
  {
    return ( tp == EMPTY);
  }

  void pop()
  {
    if ( empty() )
    {
      cout<<"no elements to be popped"<<endl;
      return;
    }
    tp--;
  }

  int  top()
  {
    if ( empty() )
    {
      cout<<"no item at the top of the empty stack" << endl;
      return EMPTY;
    }
    return arr[tp];
  }
}; //<- end of the stAck class


int main()
{
  stAck<5> st;
  st.push(1);
  st.push(2);
  st.push(3);
  cout << st.top() << endl;
  st.pop();
  st.pop();
  cout<<st.empty()<<endl;
  st.pop();
  cout<<st.empty()<<endl;
}


Or, even better, use the stack class provided by the C++ standard library

C++
#include <iostream>
#include <stack>

using namespace std;

int main()
{
  stack<int> st;
  st.push(1);
  st.push(2);
  st.push(3);
  cout << st.top() << endl;
  st.pop();
  st.pop();
  cout<<st.empty()<<endl;
  st.pop();
  cout<<st.empty()<<endl;
}
 
Share this answer
 
Comments
Patrice T 8-Jul-21 2:19am    
+5
CPallini 8-Jul-21 2:21am    
Thank you.
There are several Lines that are no valid C++.
return = "";
= ""
= "" top++;
= "" arr[top] = "x;
 
Share this answer
 
Comments
Richard MacCutchan 7-Jul-21 3:25am    
That is a peculiarity of the editor here, the original is most likely correct.
Stefan_Lang 7-Jul-21 6:50am    
Has to be combined peculiarity of the editor here, and whatever program that source came from. I've never managed to create such artifacts when pasting code here. But, yes, this happens way to often to be an accident.

That said, no matter what causes this, why are there so many people who don't bother to check whether their posting is even marginally readable?
Richard MacCutchan 7-Jul-21 7:31am    
why are there so many people who don't bother to check whether their posting is even marginally readable?
They probably assume their mothers will do it. :D
Patrice T 8-Jul-21 2:18am    
Because text of question was correct when question was posted.
Within CP question posting process, it happen that the process makes changes in text.
The posting process particularly don't like pieces of code not embedded in <pre> tags.
And this mess is not visible in preview.
Bugs and Suggestions[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900