編集 2: プログラムは現在動作しています。ご協力ありがとうございます 最後に 1 つ質問があります。プッシュのたびに top にメモリを割り当てます。ポップでそれを削除したい場合、このコードは機能しますか?
int pop()
{
cout << "About to pop: " << top->getData() << "\n";
  int retVal = top->getData();
  if(top->getLasty()!=NULL)
 {
  node* temp = top;
  top = top->getLasty();
  delete temp;
  cout << "new data is: " << top->getData() << "\n";
 }
return retVal;
}
};
作業コード:
#include <iostream>
using namespace std;
class node
{
  int data;
  node* lasty;
  public:
  node()
  {
   lasty = NULL;
   data = -1;
  }
  node(node* ptr,int dat)
    {
    lasty = ptr;
    data = dat;
    cout << "New node created! Data is " << dat << ", lasty's data is " << ptr->getData() << "\n";
    }
   node* getLasty()
   {
    return lasty;
   }
   int getData()
   {
     return data;
   }
   void setData(int x)
   {
    data = x;
   }
   void setLasty(node* ptr)
   {
    lasty = ptr;
   }
};
class stack
{
node* top;
public:
stack()
{
 cout << "New Stack created!\n";
 top = new node();
}
~stack()
{
delete top;
}
void push(int x)
{
cout << "Before pushing, top's data is " << top->getData() << "\n";
top = new node(top, x);
//node* temp = new node();
//temp->setData(x);
//temp->setLasty(top);
//top = temp;
cout << "TOP'S DATA IS: " << top->getData() << "\n";
cout << "TOP: " << "top lasty's data is: " << top->getLasty()->getData() << "\n\n";
}
int pop()
{
cout << "About to pop: " << top->getData() << "\n";
  int retVal = top->getData();
  if(top->getLasty()!=NULL)
 {
  top = top->getLasty();
  cout << "new data is: " << top->getData() << "\n";
 }
return retVal;
}
};
int main()
{
stack a;
 a.push(1);
 a.push(2);
 a.push(3);
 cout << a.pop();
 cout << a.pop();
    return 0;
}
また、この編集を送信するにはさらにテキストが必要であることが判明したため、このフィラー テキストを検討してください。