链栈的实现

来源:互联网 发布:互盾数据恢复软件破解 编辑:程序博客网 时间:2024/06/11 04:12


#include <iostream>using namespace std;#define error 0#define ok 1typedef int Status;typedef int Elemtype;struct Node{Elemtype data;Node* next;};struct LinkStack{Node* top;int count;};Status Init(LinkStack* s){s->count=0;s->top=NULL;return ok;}Status Push(LinkStack* s,Elemtype e){Node* pNew=new Node;pNew->next=s->top;pNew->data=e;s->top=pNew;++s->count;return ok;}Status Pop(LinkStack* s,Elemtype* e){if(!s->top) return error;*e=s->top->data;Node* pDel=s->top;s->top=s->top->next;delete pDel;pDel=NULL;--s->count;return ok;}int GetLength(LinkStack s){return s.count;}int main(){LinkStack s;Elemtype eout;Init(&s);cout<<GetLength(s)<<endl;Push(&s,1);Push(&s,2);cout<<GetLength(s)<<endl;Pop(&s,&eout);cout<<GetLength(s)<<eout<<endl;}


0 0
原创粉丝点击