stack_array.h
#ifndef _stack_array_h
#define EmptyTOS (-1)
#define MinStackSize (5)
#define TRUE 1
#define FALSE 0
typedef int ElementType;
typedef struct StackArrayNode
{
int Capacity;
int TopOfStack;
ElementType *Array;
}StackArrayNode,*Stack;
int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreatStack(int MaxStackSize);
void FreeStack(Stack S);
void Push(ElementType x, Stack S);
void Pop(Stack S);
ElementType Top(Stack S);
ElementType TopandPop(Stack S);
#endif
stack_array.c
#include <stdio.h>
#include <stdlib.h>
#include "stack_array.h"
int IsEmpty(Stack S)
{
if(S->TopOfStack == EmptyTOS)
return TRUE;
else
return FALSE;
}
int IsFull(Stack S)
{
if(S->TopOfStack == S->Capacity)
return TRUE;
else
return FALSE;
}
Stack CreatStack(int MaxStackSize)
{
Stack S;
if(MaxStackSize < MinStackSize)
printf("Stack is too small\n");
S = (Stack)malloc(sizeof(struct StackArrayNode));
if(S == NULL)
exit(FALSE);
S->Array = (ElementType *)malloc(sizeof(ElementType)*MaxStackSize);
if(S->Array == NULL)
exit(FALSE);
S->Capacity = MaxStackSize;
S->TopOfStack = EmptyTOS;
}
void FreeStack(Stack S)
{
if(S != NULL)
{
free(S->Array);
free(S);
}
}
void Push(ElementType x, Stack S)
{
if(IsFull(S))
printf("Stack is full\n");
else
{
S->TopOfStack ++;
S->Array[S->TopOfStack] = x;
}
}
void Pop(Stack S)
{
if(IsEmpty(S))
printf("Stack is empty\n");
else
{
S->TopOfStack --;
}
}
ElementType Top(Stack S)
{
if(!IsEmpty(S))
return S->Array[S->TopOfStack];
printf("Stack is Empty\n");
return FALSE;
}
ElementType TopandPop(Stack S)
{
if(!IsEmpty(S))
return S->Array[S->TopOfStack--];
printf("Stack is Empty\n");
return FALSE;
}
int main()
{
Stack S;
S = CreatStack(10);
Push(1,S);
Push(2,S);
Push(4,S);
printf("%d\n",Top(S));
Pop(S);
printf("%d\n",Top(S));
Pop(S);
printf("%d\n",Top(S));
}
stack.h
#ifndef _Stack_h
#define TRUE 1
#define FALSE 0
typedef int ElementType;
typedef struct StackNode
{
ElementType data;
struct StackNode *next;
}StackNode,*Stack;
int isEmpty(Stack S);
Stack CreateStack(void);
void MakeEmpty(Stack S);
void Push(ElementType x, Stack S);
void Pop(Stack S);
ElementType Top(Stack S);
#endif /*_Stack_h */
stack.c
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
int isEmpty(Stack S)
{
if(S->next == NULL)
return TRUE;
else
return FALSE;
}
void MakeEmpty(Stack S)
{
if(S == NULL)
printf("fata error");
else
{
while(!isEmpty(S))
Pop(S);
}
}
Stack CreateStack(void)
{
Stack S = (Stack)malloc(sizeof(struct StackNode));
if(S == NULL)
printf("fata error");
S->next = NULL;
MakeEmpty(S);
return S;
}
void DeleteStack(Stack S)
{
MakeEmpty(S);
free(S);
}
void Push(ElementType x, Stack S)
{
Stack TmpStack = (Stack)malloc(sizeof(struct StackNode));
if(TmpStack == NULL)
printf("fata error");
else
{
TmpStack->data = x;
TmpStack->next = S->next;
S->next = TmpStack;
}
}
void Pop(Stack S)
{
Stack TmpStack;
if(isEmpty(S))
printf("Stack is Empty");
else
{
TmpStack = S->next;
S->next = S->next->next;
free(TmpStack);
}
}
ElementType Top(Stack S)
{
if(isEmpty(S))
return FALSE;
else
return S->next->data;
}
int main()
{
Stack S = CreateStack();
Push(1,S);
Push(2,S);
Push(4,S);
printf("%d\n",Top(S));
Pop(S);
printf("%d\n",Top(S));
Pop(S);
printf("%d\n",Top(S));
}
参考自《数据结构与算法分析》
原文地址:http://blog.csdn.net/zwhlxl/article/details/44176655