C++実装チェーンスタック
C++実装チェーンスタックチェーンスタック を作成する最初から最後までチェーンテーブル を印刷する.インスタック 弾桟 スタックトップノードデータ を得る.
全体コード:
全体コード:
#include
using namespace std;
struct Node{//
int data;
Node *next;
};
class LStack{//
private:
Node *point;
int length;
public:
LStack(){//
point=NULL;//
this->length=0;
}
~LStack(){}
bool createStack(int size);//
bool printStack();//
bool pushStack(int value);//
bool popStack();//
int getTop();//
};
bool LStack::createStack(int n){//
if(n<0){
cout<<" "<<endl;
return false;
}
Node *pnew=NULL;
Node *ptemp=NULL;
this->length=n;
for(int i=0;i<n;++i){
pnew=new Node;//
this->point=pnew;//
cout<<" "<<i+1<<" :"<<endl;
cin>>pnew->data;//
pnew->next=ptemp;// ptemp, ptemp
ptemp=pnew;// ptemp ,
}
}
bool LStack::printStack(){//
Node *ptemp=this->point;//
int length=this->length;//
for(int i=0;i<length;++i){//
cout<<ptemp->data<<" ";
ptemp=ptemp->next;//
}
cout<<endl;
}
bool LStack::pushStack(int x){//
Node *ptemp=NULL;//
Node *pnew=new Node();//
pnew->data=x;// x
pnew->next=this->point;//
this->point=pnew;//
this->length++;//
return true;
}
bool LStack::popStack(){// ,
this->point=this->point->next;//
this->length--;
return true ;
}
int LStack::getTop(){//
if(this->length==0){
cout<<" "<<endl;
return -100;
}
return this->point->data;
}
int main(){
LStack stack;
stack.createStack(8);
stack.printStack();
stack.pushStack(9);
stack.printStack();
stack.popStack();
stack.printStack();
cout<<stack.getTop()<<endl;
return 0;
}