#include<stdio.h>
#include<stdlib.h>
struct sNode{
int data;
struct sNode *next;
};
struct sNode *create(){
struct sNode *s;
s=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=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;
}
|