#include<stdio.h>
#include<stdlib.h>
struct sNode{
int data;
struct sNode *next;
};
struct sNode *create(){
struct sNode *s;
s=(struct sNode *)malloc(sizeof(struct sNode));
s->next=NULL;
return s;
}
int isEmpty(struct sNode *s){
return s->next==NULL;
}
void push(struct sNode *s,int num){
struct sNode *q;
q=(struct sNode *)malloc(sizeof(struct sNode));
q->data=num;
q->next=s->next;
s->next=q;
}
int pop(struct sNode *s){
struct sNode *q;
int temp;
if(isEmpty(s)){
printf("stack is empty\n");
exit(-1);
}
q=s->next;
temp=q->data;
s->next=s->next->next;
free(q);
return temp;
}
int main(){
struct sNode *s;
s=create();
int n=10;
for(int i=0;i<n;i++){
push(s,i);
}
for(int i=0;i<n;i++){
printf("%d\t",pop(s));
}
return 0;
}
C语言之链栈基本操作
最新推荐文章于 2022-05-13 11:59:23 发布