源代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{
int data;
node *next;
}Node,*pNode;
typedef struct
{
pNode top;
int count;
}LinkStack,*pLinkStack;
void init(pLinkStack s)
{ s->top=NULL;
s->count=0;
}
int isEmpty(pLinkStack s)
{
if(s->top==NULL)
{
return 1;
}
else
{
return 0;
}
}
void push(pLinkStack s,int a)
{
pNode p=(pNode)malloc(sizeof(Node));
p->data=a;
p->next=s->top;
s->top=p;s->count++;
}
int pop(pLinkStack s)
{ pNode p=s->top;
int a=p->data;
s->top=p->next;
s->count--;
free(p);
return a;
}
int ding(pLinkStack s)
{
return s->top->data;
}
int length(pLinkStack s)
{
return s->count;
}
int main()
{
int len, top, te;
LinkStack s;
init(&s);
push(&s, 1);
push(&s, 3);
push(&s, 5);
push(&s, 7);
top = ding(&s);
len = length(&s);
printf("top:%d,len:%d\n", top, len);
while (!isEmpty(&s))
{
te = pop(&s);
printf("删除的顶部元素为:%d\n", te);
}
system("pause");
return 0;
}
结果: