69,369
社区成员
发帖
与我相关
我的任务
分享
#ifndef Stack_H
#define Stack_H
typedef int Elementyp;
struct Node;
typedef struct Node *Ptrtonode;
typedef Ptrtonode Stack;
int Isempty(Stack S);
void Push(Elementyp X,Stack S);
void Pop(Stack S);
Elementyp Topofstack();
Stack CreateEmptystack(void);
void MakeEmpty(Stack S);
Elementyp Top(Stack S);
#endif
#include <stdio.h>
#include <string.h>
#include "stack.h"
struct Node
{
Elementyp data;
Ptrtonode Next;
};
Stack CreateEmptystack(void)
{
Stack S;
S=malloc(sizeof(struct Node));
S->Next=NULL;
MakeEmpty(S);
return S;
}
void MakeEmpty(Stack S)
{
if(S==NULL)
printf("must use cerate stack first!");
else
{
while(!Isempty(S))
Pop(S);
}
}
void Push(Elementyp X,Stack S)
{
Ptrtonode P;
P=malloc(sizeof(struct Node));
P->data=X;
P->Next=S->Next;
S->Next=P;
}
void Pop(Stack S)
{
Ptrtonode P;
P=S->Next;
S->Next=S->Next->Next;
free(P);
}
int Top(Stack S)
{
if(!Isempty(S))
return S->Next->data;
else
{
printf("error: stack empty!");
return 0;
}
}
int Isempty(Stack S)
{
return S->Next==NULL;
}
#include <stdio.h>
#include "stack.c"
#include "stack.h"
int main(int argc, char *argv[])
{
int c,temp;
Stack S;
S=CreateEmptystack();
while((c=getchar())!=EOF)
{
Push(c,S);
}
while(!Isempty(S))
{
temp=Top(S);
printf("%d\t",temp);
Pop(S);
}
return 0;
}