栈是只允许在一端插入和删除的线性表,只允许插入和删除的一端称为栈顶,另一端称为栈底。栈遵循先进后出的规则。实现栈可以使用顺序表,也可以使用链表。接下来是关于这两种实现方式的一些简单操作的函数。
顺序栈:
头文件:
SqStack.h
#ifndef __SQSTACK_H__
#define __SQSTACK_H__
#define TRUE 1
#define FALSE 0
#define SIZE 10
typedef int StackData;
typedef struct _stack
{
StackData data[SIZE];
int top;
}Stack;
//置空栈
int InitStack (Stack *s);
//判断为空
int Stack_Empty (Stack *s);
//判断栈满
int Stack_Full (Stack *s);
//入栈
int Push (Stack *s,StackData x);
//出栈
int Pop (Stack *s,StackData *x);
//取栈顶
int GetTop (Stack *s,StackData *x);
#endif //__SQSTACK_H__
源文件:
SqStack.c
#include <stdio.h>
#include "SqStack.h"
#include "error.h"
//置空栈
int InitStack (Stack *s)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
s -> top = -1;
return TRUE;
}
//判断为空
int Stack_Empty (Stack *s)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
return s -> top == -1;
}
//判断栈满
int Stack_Full (Stack *s)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
return s -> top == (SIZE - 1);
}
//入栈
int Push (Stack *s,StackData x)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
if (Stack_Full (s))
{
errno = FULL_STACK;
return FALSE;
}
s -> data[s -> top + 1] = x;
s -> top++;
//s -> data[++s -> top] = x;
return TRUE;
}
//出栈
int Pop (Stack *s,StackData *x)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
if (Stack_Empty (s))
{
errno = EMPTY_STACK;
return FALSE;
}
*x = s -> data[s -> top];
s -> top--;
//*x = s -> data[s -> top--];
return TRUE;
}
//取栈顶
int GetTop (Stack *s,StackData *x)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
if (Stack_Empty (s))
{
errno = EMPTY_STACK;
return FALSE;
}
*x = s -> data[s -> top];
return TRUE;
}
链式栈:
头文件:
LinkStack.h
#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__
#define TRUE 1
#define FALSE 0
#define SIZE 10
typedef int StackData;
typedef struct _node
{
StackData data;
struct _node *next;
}Node;
typedef struct _linkStack
{
Node *top;
}LinkStack;
//创建链式栈
LinkStack * Create_Stack ();
//判断栈空
int StackEmpty (LinkStack *s);
//进栈
int Push (LinkStack *s,StackData x);
//出栈
int Pop (LinkStack *s,StackData *x);
//获取栈顶元素
int GetTop (LinkStack *s,StackData *x);
//销毁栈
int Destory(LinkStack *s);
#endif //__LINKSTACK_H__
源文件:
LinkStack.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkStack.h"
#include "error.h"
//创建链式栈
LinkStack * Create_Stack ()
{
LinkStack * s = (LinkStack *)malloc(sizeof(LinkStack)/sizeof(char));
if (s == NULL)
{
errno = MALLOC_ERROR;
return NULL;
}
s->top = NULL;
return s;
}
//判断栈空
int StackEmpty (LinkStack *s)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
return s->top == NULL;
}
//进栈
int Push (LinkStack *s,StackData x)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
Node * node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
errno = MALLOC_ERROR;
return FALSE;
}
node->data = x;
node->next = s->top;
s->top = node;
return TRUE;
}
//出栈
int Pop (LinkStack *s,StackData *x)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
if (StackEmpty (s))
{
errno = EMPTY_STACK;
return FALSE;
}
Node *p = s->top;
*x = p->data;
s->top = p->next;
free(p);
return TRUE;
}
//获取栈顶元素
int GetTop (LinkStack *s,StackData *x)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
if (StackEmpty (s))
{
errno = EMPTY_STACK;
return FALSE;
}
*x = s->top->data;
return TRUE;
}
//销毁栈
int Destory(LinkStack *s)
{
if (s == NULL)
{
errno = ERROR;
return FALSE;
}
int x;
while (StackEmpty (s) != TRUE)
{
Pop (s,&x);
}
free(s);
return TRUE;
}
对于其中的一些错误信息,我返回特定的错误码,在错误处理文件中处理,这样可以使程序更具可读性,也容易维护。
关于栈的更多的功能,可以大家一起去实现。