链式栈的出栈入栈操作c++描述
//节点
class node{
public :
int data;
node * next;
node * prev;
};
//双向链表
#include"node.h"
class stack{
private :
node * head;//首节点
node * tail;//尾节点
public:
stack();
void push(int data);
node * put();
void print();//循序打印
};
//实现
#include<iostream>
#include "stack.h"
using namespace std;
stack::stack(){
head=new node();//初始化,不做数据处理
tail=new node();
head->next=tail;
head->prev=NULL;
tail->next=NULL;
tail->prev=head;
}
void stack::push(int data){
node * n=new node();
n->data=data;
n->next=head->next;
n->prev=head;
head->next->prev=n;
head->next=n;
}
void stack::print(){
node * n=head;
while(n->next->next!=NULL){
cout<<n->next->data<<" ";
n=n->next;
}
}
node * stack::put(){
node * n=head->next;
if(n->next==NULL){
cout<<"空栈"<<endl;
return NULL;
}
head->next=n->next;
n->next->prev=head;
n->next=NULL;
n->prev=NULL;
return n;
}
int main(){
stack * s=new stack();
cout<<"输出:";
s->print();
s->push(1);
s->push(2);
s->push(3);
s->push(4);
s->push(5);
s->print();
cout<<endl;
cout<<"出栈:";
node *n=s->put();
if(n!=NULL){
cout<<n->data<<endl;
}
cout<<"输出:";
s->print();
cout<<endl;
return 1;
}