#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
struct Node
{
ElementType Element;
PtrToNode Next;
};
int IsEmpty(Stack S);
Stack CreateStack();
void MakeEmpty(Stack S);
void Push(ElementType x,Stack S);
void Pop(Stack S);
ElementType Top(Stack S);
int IsEmpty(Stack S)
{
return S->Next==NULL;
}
void MakeEmpty(Stack S)
{
if(S==NULL)
{
printf("out of space");
}
else
while(S->Next!=NULL)
Pop(S);
}
Stack CreateStack()
{
Stack S;
S=malloc(sizeof(struct Node));
if(S==NULL)
printf("out of Space");
else
{
S->Next==NULL;
return S;
}
}
void Push(ElementType x,Stack S)
{
Stack TmpCell;
TmpCell=malloc(sizeof(struct Node));
if(TmpCell==NULL)
printf("out of space \n");
else
{
TmpCell->Element=x;
TmpCell->Next=S->Next;
S->Next=TmpCell;
}
}
ElementType Top(Stack S)
{
if(S->Next==NULL)
{
printf("Empty stack \n");
return 0;
}
return S->Next->Element;
}
void Pop(Stack S)
{
Stack FirstCell;
if(S->Next==NULL)
printf("Empty stack \n");
else
{
FirstCell=S->Next;
S->Next=FirstCell->Next;
free(FirstCell);
}
}
void print(Stack S)
{
Stack TmpCell;
if(S->Next==NULL)
printf("kong \n");
else
{
TmpCell=S->Next;
while(TmpCell)
{
printf("%d ",TmpCell->Element);
TmpCell=TmpCell->Next;
}
printf("\n");
}
}
void main()
{
Stack s;
s=CreateStack();
print(s);
int i;
for(i=0;i<10;i++)
Push(i,s);
print(s);
Pop(s);
print(s);
printf("top=%d \n",Top(s));
}
1901



被折叠的 条评论
为什么被折叠?



