#include<iostream.h>
struct Node
{
int data;
struct Node *next;
};


class Linkstack
{
private:
Node *top;
public:
Linkstack();
~Linkstack(){}
void push(int x);
int get();
int pop();
};


Linkstack::Linkstack()
{
top=NULL;
}


int Linkstack::get()
{
return top->data;
}


int Linkstack::pop()
{

int x;
Node *p;
if(top==NULL)throw"下溢";
x=top->data;
p=top;
top=top->next;
delete p;
return x;
}


void Linkstack::push(int x)
{
Node *s;
s=new Node;
s->data=x;
s->next=top;
top=s;
}




int main()
{
Linkstack L;
cout<<"******************"<<endl;
cout<<"      链栈"<<endl;
cout<<"******************"<<endl;
cout<<endl;
cout<<"@请对1,2,3进行入栈操作:"<<endl;
L.push(1);
L.push(2);
L.push(3);
cout<<endl;
cout<<"@查看栈顶元素"<<endl;
cout<<L.get()<<endl;
cout<<endl;
cout<<"@执行一次出栈操作:"<<endl;
    L.pop();
cout<<endl;
cout<<"@执行一次出栈后栈顶元素"<<endl;
cout<<L.get()<<endl;
return 0;

}

实验二 链栈

相关文章:

  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-14
  • 2021-08-23
  • 2021-09-11
  • 2021-07-21
  • 2021-07-03
  • 2021-06-23
  • 2021-11-21
相关资源
相似解决方案