#include<iostream>
using namespace std;
#define ElemType int
typedef struct Node
{
ElemType elem;
struct Node *next;
}Node,*linklist;
int init(linklist &head)
{
head=(Node*)malloc(sizeof(Node));
if(!head)
{
cout<<"栈头分配空间失败"<<endl;
exit(-1);
}
head->next=NULL;
return 1;
}
int push(linklist &head,ElemType e)
{
Node *p=(Node*)malloc(sizeof(Node));
if(!p)
{
cout<<"结点存储空间分配失败"<<endl;
exit(-1);
}
p->elem=e;
p->next=head->next;
head->next=p;
return 1;
}
int pop(linklist &head,ElemType &e)
{
linklist tmp;
tmp=head->next;
e=tmp->elem;
head->next=tmp->next;
free(tmp);
return 1;
}
//遍历
void print(linklist head)
{
Node *p;
p=head;
while(p->next!=NULL)
{
p=p->next;
cout<<p->elem<<endl;
}
}
void main()
{
Node *head;
init(head);
push(head,1);
push(head,2);
push(head,3);
print(head);
int e;
pop(head,e);
cout<<"e:";
cout<<e<<endl;
print(head);
system("pause");
}
栈的链式存储结构
最新推荐文章于 2022-07-26 22:11:17 发布