2009-04-19 回答
#include"stdio.h"
#include"stdlib.h"
typedef struct Node
{
int data;
struct Node *next;
}NODE;
typedef struct
{
NODE *top;
}Stack;
#define TRUE 1
#define FALSE 0
#define OK 1
#define OVERFLOW -2
typedef int Status;
void InitStack(Stack &S)
{
S.top=(NODE *)malloc(sizeof(NODE));
if(!S.top)
exit(OVERFLOW);
S.top->next=NULL;
}
Status StackEmpty(Stack S)
{
if(S.top->next==NULL)
return(TRUE);
else return(FALSE);
}
void Push(Stack &S,int e)
{
NODE *p;
p=(NODE *)malloc(sizeof(NODE));
if(!p)
return;
p->data=e;
p->next=S.top->next;
S.top->next=p;
}
void Pop(Stack &S,int &e)
{
NODE *p;
if(StackEmpty(S))
return;
else
{
p=S.top->next;
e=p->data;
S.top->next=p->next;
free(p);
}
}
int Gettop(Stack &S)
{
NODE *p;
if(StackEmpty(S))
return(0);
else
{
p=S.top->next;
return(p->data);
}
}
#include "linksq.h"
void main()
{
int select;
int num=1,e;
Stack S;
InitStack(S);
do{
printf("\n1 Push 2 Pop\n");
printf("0 exit\n please select(0--2)\n");
scanf("%d",&select);
switch(select)
{
case 1:
printf("Push%2d",num);
Push(S,num++);
break;
case 2:
if(!StackEmpty(S))
{
Pop(S,e);
printf("Pop %2d ",e);
}
else
printf("Empty!\n");
break;
case 0:
break;
}
}
while(select);
}
这是c语言版的数据结构的一段程序