数组存储
代码一
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 20
typedef char ElementType;
typedef struct SNode *Stack;
struct SNode{
ElementType Data[MaxSize];
int Top;
};
//栈的顺序存储实现
Stack InitStack()
{
Stack S;
if((S=(Stack)malloc(sizeof(SNode)))==NULL)
{
printf("分配空间失败");
}
S->Top=-1;
return S;
}
void Push( Stack PtrS, ElementType item )
{
if ( PtrS->Top == MaxSize-1 )
{
printf("堆栈满");
return;
}
else
{
PtrS->Data[++(PtrS->Top)] = item;
return;
}
}
ElementType Pop( Stack PtrS )
{
if ( PtrS->Top == -1 )
{
printf("堆栈空");
return 0; /* ERROR是ElementType的特殊值,标志错误*/
}
else
return ( PtrS->Data[(PtrS->Top)--] );
}
int IsEmpty(Stack PtrS)
{
if(PtrS->Top == -1)
{
return 0;
}
else
return 1;
}
ElementType Top(Stack PtrS)
{
return PtrS->Top == -1 ? 0 : PtrS->Data[PtrS->Top];
}
int main()
{
Stack s;
s=InitStack();
return 0;
}
代码二
typedef struct stack1
{
char sign[1000];
int top;
}*Stack1;
//有头结点
Stack1 create()
{
Stack1 head = (Stack1)malloc(sizeof(struct stack1));
head->top = -1;
return head;
}
//入栈
void push1(char x, Stack1 stk)
{
stk->top++;
stk->sign[stk->top] = x;
}
//出栈并返回出栈元素
char pop1(Stack1 stk)
{
char x = stk->sign[stk->top];
stk->top--;
return x;
}
char GetTop1(Stack1 stk)
{
return stk->sign[stk->top];
}
链表存储
struct StackRecord;
typedef struct StackRecord *Stack;
typedef char ElementType;
#define EmptyTOS (-1)
#define MinStackSize (5)
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
void MakeEmpty(Stack S)
{
S->TopOfStack = EmptyTOS;
}
Stack CreateStack(int MaxElements)
{
Stack S;
if (MaxElements < MinStackSize)
printf("Stack size is too small");
S = malloc(sizeof(struct StackRecord));
if (S == NULL)
printf("Out of space!!!");
S->Array = malloc(sizeof(ElementType) * MaxElements);
if (S->Array == NULL)
printf("Out of space!!!");
S->Capacity = MaxElements;
MakeEmpty(S);
return S;
}
void DisposeStack(Stack S)
{
if (S != NULL)
{
free(S->Array);
free(S);
}
}
int IsEmpty(Stack S)
{
return S->TopOfStack == EmptyTOS;
}
int IsFull(Stack S)
{
return S->TopOfStack == S->Capacity - 1;
}
void Push(ElementType X, Stack S)
{
if (IsFull(S))
printf("Full stack");
else
S->Array[++S->TopOfStack] = X;
}
ElementType Top(Stack S)
{
if (!IsEmpty(S))
return S->Array[S->TopOfStack];
printf("Empty stack");
return 0;
}
void Pop(Stack S)
{
if (IsEmpty(S))
printf("Empty stack");
else
S->TopOfStack--;
}
ElementType TopAndPop(Stack S)
{
if (!IsEmpty(S))
return S->Array[S->TopOfStack--];
printf("Empty stack");
return 0;
}
代码二
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 100
typedef struct stack
{
int topofstack;
char *array;
}stack;
stack *create(void);
void push(char ch, stack *s);
void pop(stack *s);
char top(stack *s);
stack* create(void)
{
stack*s=(stack*)malloc(sizeof(stack));
s->array=(char*)malloc(sizeof(char)*max);
s->topofstack=-1;
return s;
}
void push(char ch, stack *s)
{
s->array[++s->topofstack]=ch;
}
void pop(stack*s)
{
if (s->topofstack==-1)
return;
s->topofstack--;
}
char top(stack *s)
{
return s->topofstack==-1?0: s->array[s->topofstack];
}