实验二 链栈

#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;

}


猜你喜欢

转载自blog.csdn.net/laufen_j/article/details/80081900