作者在 2010-06-01 15:55:35 发布以下内容
#include<stdio.h>
#include<stdlib.h>
struct stacktype
{
int data;
struct stacktype *link;
};
typedef struct stacktype stack;
int push(stack **top,int x)
{
stack *p;
p=(stack *)malloc(sizeof(stack));
if(p==NULL)
{
printf("Memory alloc failed\n");
exit(-1);
}
p->link=NULL;
if(*top==NULL)
{
*top=p;
(*top)->data=x;
}
else
{
p->data=x;
p->link=*top;
*top=p;
}
return (*top);
}
int pop(stack **top)
{
int x;
if(*top==NULL)
{
printf("stack empty\n");
exit(0);
}
else
{
x=(*top)->data;
*top=(*top)->link;
//free(*top);
}
return(x);
}
int main(void)
{
stack * top;
int i,n,x;
top=NULL;
printf("Input stack stize n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input x\n");
scanf("%d",&x);
push(&top,x);
}
for(i=0;i<n;i++)
{
x=pop(&top);
printf("%5d",x);
}
printf("\n");
}
#include<stdlib.h>
struct stacktype
{
int data;
struct stacktype *link;
};
typedef struct stacktype stack;
int push(stack **top,int x)
{
stack *p;
p=(stack *)malloc(sizeof(stack));
if(p==NULL)
{
printf("Memory alloc failed\n");
exit(-1);
}
p->link=NULL;
if(*top==NULL)
{
*top=p;
(*top)->data=x;
}
else
{
p->data=x;
p->link=*top;
*top=p;
}
return (*top);
}
int pop(stack **top)
{
int x;
if(*top==NULL)
{
printf("stack empty\n");
exit(0);
}
else
{
x=(*top)->data;
*top=(*top)->link;
//free(*top);
}
return(x);
}
int main(void)
{
stack * top;
int i,n,x;
top=NULL;
printf("Input stack stize n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input x\n");
scanf("%d",&x);
push(&top,x);
}
for(i=0;i<n;i++)
{
x=pop(&top);
printf("%5d",x);
}
printf("\n");
}