数组实现
#include<stdio.h>
#include<stdlib.h>
#define EmptyTOS -1
#define MinStackSize 5
#ifndef _Stack_h
struct StackRecord;
typedef struct StackRecord *Stack;
typedef int ElementType;
int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElement);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X,Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
ElementType TopandPop(Stack S);
#endif
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
int IsEmpty(Stack S)
{
return S->TopOfStack==EmptyTOS;
}
int IsFull(Stack S)
{
return S->TopOfStack==S->Capacity-1;
}
Stack CreateStack(int MaxElement)
{
Stack S;
if(MaxElement<MinStackSize)
{
printf("Stack size is too small\n");
}
S=(struct StackRecord*)malloc(sizeof(struct StackRecord));
if(S==NULL)printf("Out of space!\n");
S->Array=(ElementType*)malloc(sizeof(ElementType)*MaxElement);
if(S->Array==NULL)printf("Out of space!\n");
S->Capacity=MaxElement;
MakeEmpty(S);
return S;
}
void DisposeStack(Stack S)
{
if(S!=NULL)
{
free(S->Array);
free(S);
}
}
void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
void Push(ElementType X,Stack S)
{
if(IsFull(S))printf("Full Stack!\n");
else S->Array[++S->TopOfStack]=X;
}
ElementType Top(Stack S)
{
if(!IsEmpty(S))
{
return S->Array[S->TopOfStack];
}
printf("Empty Stack!\n");
return 0;
}
void Pop(Stack S)
{
if(IsEmpty(S))printf("Empty stack!\n");
else S->TopOfStack--;
}
ElementType TopandPop(Stack S)
{
if(!IsEmpty(S))
{
return S->Array[S->TopOfStack--];
}
printf("Empty Stack!\n");
return 0;
}
int main()
{
system("pause");
return 0;
}
链表实现
#include<stdio.h>
#include<stdlib.h>
#ifndef _Stack_h
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
typedef int ElementType;
int IsEmpty(Stack S);
Stack CreateStack();
void MakeEmpty(Stack S);
void DisposeStack(Stack S);
void Push(ElementType X,Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
void PrintStack(Stack S);
#endif
struct Node
{
ElementType Element;
PtrToNode Next;
};
int IsEmpty(Stack S)
{
return S->Next==NULL;
}
void MakeEmpty(Stack S)
{
if(S==NULL)
{
printf("Must use createStack first!\n");
}
else
{
while(!IsEmpty(S))
{
Pop(S);
}
}
}
Stack CreateStack()
{
Stack S;
S=(struct Node*)malloc(sizeof(struct Node));
if(S==NULL)
{
printf("out of space!\n");
}
S->Next=NULL;
MakeEmpty(S);
return S;
}
void DisposeStack(Stack S)
{
MakeEmpty(S);
free(S);
return;
}
void Push(ElementType X,Stack S)
{
PtrToNode Tmpcell;
Tmpcell=(struct Node*)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(!IsEmpty(S))
return S->Next->Element;
else printf("Empty Stack\n");
return 0;
}
void Pop(Stack S)
{
PtrToNode Firstcell;
if(IsEmpty(S))
{
printf("Empty Stack\n");
}
else
{
Firstcell=S->Next;
S->Next=S->Next->Next;
free(Firstcell);
}
}
void PrintStack(Stack S)
{
while(S->Next!=NULL)
{
printf("%d ",S->Next->Element);
S->Next=S->Next->Next;
}
}
int main()
{
system("pause");
return 0;
}