栈的特点及其基本操作

您所在的位置:网站首页 堆栈有哪些操作 栈的特点及其基本操作

栈的特点及其基本操作

2024-01-14 08:46| 来源: 网络整理| 查看: 265

栈(stack)是一个特殊的线性表,是限定在一端(通常是表尾)进行插入和删除操作的线性表 表尾称为栈顶,表头称为栈底(Base),基本操作为PUSH(入栈),POP(出栈).

特点是**LIFO(Last In First out)后进先出,**在进行PUSH和POP操作的时候也是要这个模式进行访问。

对于顺序栈而言,栈的存储方式与一般线性表的顺序存储结构完全相同,利用一组地址连续的存储单元一次存放自栈底到栈顶的数据元素,栈底一般在地地址端,附设top指针,指示栈顶元素在顺序栈的位置,另设base指针,指示栈底元素在顺序栈中的位置。 但是,为了方便操作,通常top指示真正的栈顶元素之上的下标地址,用stacksize来指示栈课使用的最大容量。 空栈的标志是base==top也就是栈顶和栈底重合,不存储任何数据。 栈满的标志是top-base==stacksize 上溢(overflow):栈已经满,又要压入元素 下溢(underflow):栈已经空,又要弹出元素 上溢是一种错误,使问题的处理无法进行,而下溢一般认为是一种结束条件,也就是问题的处理结束

栈的基本表现

struct SqStack { SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL SElemType *top; // 栈顶指针 int stacksize; // 当前已分配的存储空间,以元素为单位 }; // 顺序栈

栈的基本操作函数及其实现: ①nitStack(&S)初始化一个栈

Status InitStack(SqStack &S) { // 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE S.base= new SElemType[STACK_INIT_SIZE]; if(!S.base)//如果分配失败了 { exit(ERROR); } S.top=S.base; S.stacksize=STACK_INIT_SIZE; }

②DestoryStack(&S)销毁一个栈

Status DestoryStack(SqStack &S) { if(S.base) { delete S.base;//释放内存 S.stacksize=0; S.base=S.top=NULL;//重置栈顶和栈底指针值 } return OK; }

③StackEmpty(S)判断一个栈是否为空

Status StackEmpty(Sqstack S) { if(S.top==S.base) { return OK; } else { return ERROR; } }

④StackLength(S)计算一个栈的长度

int StackLength(SqStack S) { // 返回栈S的元素个数 return S.top-S.base; }

⑤GetTop(S,&e)获取栈顶元素

Status GetTop(SqStack S,SElemType &e) { // 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR if(S.top==S.base) { return ERROR; } e=*S.top; return OK; }

⑥ClearStack(&S)清空栈**

Status ClearStack(SqStack &S) { if(S.base) { S.top=S.base; } return OK; }

⑦Push(&S,e)入栈操作

Status Push(SqStack &S,SElemType e) { // 在栈S中插入元素e为新的栈顶元素 /*判断栈是否满*/ /*压入元素e*/ /*栈顶指针++*/ if(S.top-S.base==S.stacksize) { return ERROR; } *S.top=e; S.top++; return OK; }

⑧Pop(&S,e)出栈操作

Status Pop(SqStack &S,SElemType &e) { // 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR if(S.top==S.base) { return ERROR; } --S.top; e=*S.top; return OK; }

⑨取得栈顶元素

Status GetTop(SqStack S,SElemType &e) { // 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR if(S.top==S.base) { return ERROR; } S.top--; e=*S.top; S.top++; return OK; }

⑩遍历栈并打印

Status StackTraverse(SqStack S) { // 从栈顶到栈底依次输出栈中的每个元素 SElemType *p = (SElemType *)malloc(sizeof(SElemType)); p =S.top; //请填空 if(p==S.base)printf("The Stack is Empty!"); //请填空 else { printf("The Stack is: "); p--; while(p+1!=S.base) //请填空 { printf("%d ", *p); p--; } } printf("\n"); return OK; }

完整代码如下

#include #include #define OK 1 #define ERROR 0 #define STACK_INIT_SIZE 100 // 存储空间初始分配量 #define STACKINCREMENT 10 // 存储空间分配增量 typedef int SElemType; // 定义栈元素类型 typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等 struct SqStack { SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL SElemType *top; // 栈顶指针 int stacksize; // 当前已分配的存储空间,以元素为单位 }; // 顺序栈 Status InitStack(SqStack &S) { // 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE S.base= new SElemType[STACK_INIT_SIZE]; if(!S.base)//如果分配失败了 { return ERROR; } S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; } Status Push(SqStack &S,SElemType e) { // 在栈S中插入元素e为新的栈顶元素 /*判断栈是否满*/ /*压入元素e*/ /*栈顶指针++*/ if(S.top-S.base==S.stacksize) { return ERROR; } *S.top=e; S.top++; return OK; } Status Pop(SqStack &S,SElemType &e) { // 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR if(S.top==S.base) { return ERROR; } --S.top; e=*S.top; return OK; } Status GetTop(SqStack S,SElemType &e) { // 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR if(S.top==S.base) { return ERROR; } S.top--; e=*S.top; S.top++; return OK; } int StackLength(SqStack S) { // 返回栈S的元素个数 return S.top-S.base; } Status ClearStack(SqStack &S) { if(S.base) { S.top=S.base; } return OK; } Status StackTraverse(SqStack S) { // 从栈顶到栈底依次输出栈中的每个元素 SElemType *p = (SElemType *)malloc(sizeof(SElemType)); p =S.top; //请填空 if(p==S.base)printf("The Stack is Empty!"); //请填空 else { printf("The Stack is: "); p--; while(p+1!=S.base) //请填空 { printf("%d ", *p); p--; } } printf("\n"); return OK; } Status StackEmpty(SqStack S) { if(S.top==S.base) { return OK; } else { return ERROR; } } Status DestoryStack(SqStack &S) { if(S.base) { delete S.base;//释放内存 S.stacksize=0; S.base=S.top=NULL;//重置栈顶和栈底指针值 } return OK; } int main() { int a; SqStack S; SElemType x, e; if(InitStack(S)) // 判断顺序表是否创建成功,请填空 { printf("A Stack Has Created.\n"); } while(1) { printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n"); scanf("%d",&a); switch(a) { case 1: scanf("%d", &x); if(!Push(S,x)) printf("Push Error!\n"); // 判断Push是否合法,请填空 else printf("The Element %d is Successfully Pushed!\n", x); break; case 2: if(!Pop(S,e)) printf("Pop Error!\n"); // 判断Pop是否合法,请填空 else printf("The Element %d is Successfully Poped!\n", e); break; case 3: if(!GetTop(S,e))printf("Get Top Error!\n"); // 判断Get Top是否合法,请填空 else printf("The Top Element is %d!\n", e); break; case 4: printf("The Length of the Stack is %d!\n",StackLength(S)); //请填空 break; case 5:StackTraverse(S); break; case 0: return 1; } } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3