栈(Stack)是限定只在表尾进行插入或删除操作的线性表。因此,对栈来说,表尾端有其特殊含义,称为栈顶(top),相应地,表头段称为栈底(bottom)。不含任何元素的空表称为空栈。
顺序栈是指利用顺序存储结构实现的栈,即利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素,同时附设指针top指示栈顶元素在顺序栈中的位置。base为栈底指针,初始化完成后,其栈底指针指向栈底。每当插入新的元素时指针top增1;删除栈顶元素时指针top减1,因此栈空时,top和base的值相等,都指向栈底,栈非空时,top始终指向栈顶元素的上一个位置。stacksize表示栈的可容纳元素的最大容量。
以下就是顺序栈的代码实现:
C语言(SequentialStack):
#ifndef SEQUENTIALSTACK_H_INCLUDED
#define SEQUENTIALSTACK_H_INCLUDED
#define MAX 255
#define TRUE 1
#define FALSE -1
typedef int ElemType;
typedef struct
{
volatile ElemType *base;
volatile ElemType *top;
volatile int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(ElemType*)malloc(sizeof(ElemType)*MAX);
S->top=S->base;
if(S->base!=NULL&&S->top!=NULL)
{
S->stacksize=MAX;
return TRUE;
}
else
{
return FALSE;
}
}
void DestroyStack(SqStack *S)
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=NULL;
}
void ClearStack(SqStack *S)
{
for(int i=0;i<MAX;i++)
{
S->base[i]=NULL;
}
S->top=S->base;
}
int StackEmpty(SqStack *S)
{
if(S->base==S->top)
{
return TRUE;
}
else
{
return FALSE;
}
}
int StackLength(SqStack *S)
{
return S->top-S->base;
}
ElemType GetTop(SqStack *S)
{
if(S->top!=S->base)
{
return *(S->top-1);
}
else
{
return FALSE;
}
}
int Push(SqStack *S,ElemType elem)
{
if(((S->top)-(S->base))==S->stacksize)
{
return FALSE;
}
else
{
S->top[0]=elem;
S->top++;
return TRUE;
}
}
ElemType Pop(SqStack *S)
{
if(S->top==S->base)
{
return FALSE;
}
else
{
--S->top;
return S->top[0];
}
}
void StackTraverse(SqStack *S)
{
int i=0;
while(i<(S->top-S->base))
{
printf("%d\t",S->base[i]);
i++;
}
printf("\n");
}
#endif // SEQUENTIALSTACK_H_INCLUDED
C语言测试用例:
#include <stdio.h>
#include <stdlib.h>
#include "SequentialStack.h"
int main()
{
SqStack list;
void (*voids)(SqStack*);
int (*ints)(SqStack*);
int (*inputs)(SqStack*,ElemType);
ElemType (*elems)(SqStack*);
while(1)
{
int op;
printf("1:初始化栈。2:销毁栈。3:清理栈。4:判断栈空。5:返回栈长。6:返回栈顶元素。7:入栈。8:出栈。9:遍历栈元素\n");
printf("请输入操作选项\n");
scanf("%d",&op);
switch(op)
{
case 1:
{
ints=InitStack;
if(ints(&list)==1)
{
printf("初始化成功\n");
}
else
{
printf("初始化失败,请检查内存空间是否充足\n");
}
break;
}
case 2:
{
voids=DestroyStack;
voids(&list);
printf("销毁成功\n");
break;
}
case 3:
{
voids=ClearStack;
voids(&list);
printf("清理成功\n");
break;
}
case 4:
{
ints=StackEmpty;
if(ints(&list)==1)
{
printf("栈空\n");
}
else
{
printf("栈不为空\n");
}
break;
}
case 5:
{
ints=StackLength;
printf("栈长为:%d\n",ints(&list));
break;
}
case 6:
{
elems=GetTop;
if(elems(&list)==-1)
{
printf("栈为空\n");
}
else
{
printf("栈顶元素为:%d\n",elems(&list));
}
break;
}
case 7:
{
inputs=Push;
int input;
printf("请输入入栈的元素:\n");
scanf("%d",&input);
if(inputs(&list,input)==-1)
{
printf("栈满\n");
}
else
{
printf("入栈成功,栈元素如下:\n");
voids=StackTraverse;
voids(&list);
}
break;
}
case 8:
{
elems=Pop;
ElemType temp=elems(&list);
if(temp==-1)
{
printf("栈空\n");
}
else
{
printf("出栈成功,出栈元素为:%d栈元素如下:\n",temp);
voids=StackTraverse;
voids(&list);
}
break;
}
case 9:
{
printf("栈元素如下:\n");
voids=StackTraverse;
voids(&list);
break;
}
}
}
return 0;
}
Java语言:
package DataStructure.LinearList;
class SequentialStackArchitecture {
protected int top;
protected int bottom;
protected Object []Elem;
protected int StackSize;
public SequentialStackArchitecture(int size)
{
this.top=0;
this.bottom=0;
this.StackSize=size;
this.Elem=new Object[size];
}
public SequentialStackArchitecture()
{
}
}
public class SequentialStack extends SequentialStackArchitecture{
private SequentialStackArchitecture Stack;
public void InitStack(int size)
{
Stack=new SequentialStackArchitecture(size);
}
public Boolean DestoryStack()
{
ClearStack();
this.Stack.Elem=null;
this.Stack.StackSize=this.Stack.top=this.Stack.bottom=0;
this.Stack=null;
return true;
}
public Boolean ClearStack()
{
while (this.Stack.top>this.Stack.bottom)
{
this.Stack.Elem[this.Stack.top-1]=null;
this.Stack.top--;
}
return true;
}
public Boolean StackEmpty()
{
if (this.Stack.top==this.Stack.bottom)
{
return true;
}
else
{
return false;
}
}
public int StackLength()
{
return this.Stack.top;
}
public Object GetTop()
{
if (this.Stack.top==0)
{
return -1;
}
else
{
return this.Stack.Elem[this.Stack.top-1];
}
}
public Boolean Push(Object elem)
{
if (this.Stack.top==this.Stack.StackSize)
{
return false;
}
else
{
this.Stack.Elem[this.Stack.top]=elem;
this.Stack.top++;
}
return true;
}
public Object Pop()
{
if (this.Stack.top==this.Stack.bottom)
{
return -1;
}
else
{
Object Get=this.Stack.Elem[this.Stack.top-1];
this.Stack.Elem[this.Stack.top-1]=null;
this.Stack.top--;
return Get;
}
}
public void StackTraverse()
{
for (int i=0;i<this.Stack.top;i++)
{
System.out.print(this.Stack.Elem[i]+"\t");
}
System.out.println("");
}
}