数据结构——栈

一、实验目的

  1. 理解和掌握栈的类型定义方法。
  2. 掌握栈中的基本运算,包括创建、判空及判满、出栈/入栈等基本操作。
  3. 学习利用栈解决实际问题

二、实验要求

【项目1 – 验证性实验:建立顺序栈、链栈】
【要求】:

1、头文件sqstack.h中定义数据结构并声明用于完成基本运算的函数。对应基本运算的函数包括:
2、在sqstack.cpp中实现这些函数
3、在main函数中完成测试,包括如下内容:

(1)初始化栈s
(2)判断s栈是否为空
(3)依次进栈元素a,b,c,d,e
(4)判断s栈是否为空
(5)输出栈长度
(6)输出从栈顶到栈底元素
(7)出栈,并输出出栈序列
(8)判断s栈是否为空
(9)释放栈

main.cpp程序代码:

#include <stdio.h>
#include "sqstack.h"
#include <iostream>
using namespace std;

int main()
{
    SqStack *s;
    ElemType e;
    InitStack(s);
    printf("初始化栈后,栈为:");
    if(StackEmpty(s))
        printf("Empty\n");
    else
        printf("Not Empty\n");
    Push(s,'a');
    Push(s,'b');
    Push(s,'c');
    Push(s,'d');
    Push(s,'e');
    printf("入栈完成后,栈为:");
    if(StackEmpty(s))
        printf("Empty\n");
    else
        printf("Not Empty\n");
    printf("栈的长度为:%d\n",StackLength(s));
    printf("从栈顶到栈底输出栈:");
    DispStack(s);
    printf("依次出栈:\n");
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    printf("出栈完成后,栈为:");
    if(StackEmpty(s))
        printf("Empty\n");
    else
        printf("Not Empty\n");
    DestroyStack(s);
printf("已销毁栈\n");

    return 0;
}

运行结果图:
在这里插入图片描述

1.头文件:listack.h,包含定义链栈数据结构的代码、宏定义、要实现算法的函数的声明;
2.源文件:listack.cpp,包含实现各种算法的函数的定义
3.在同一项目(project)中建立一个源文件(如main.cpp),编制main函数,完成相关的测试工作。 包括如下内容:

(1)初始化栈s
(2)判断s栈是否为空
(3)依次进栈元素a,b,c,d,e
(4)判断s栈是否为空
(5)输出栈长度
(6)输出从栈顶到栈底元素
(7)出栈,并输出出栈序列
(8)判断s栈是否为空
(9)释放栈

main.cpp程序代码:

#include <stdio.h>
#include "listack.h"
#include <iostream>
using namespace std;

int main()
{
    LiStack *s;
    ElemType e;
    InitStack(s);
    printf("初始化栈后,栈为:");
    if(StackEmpty(s))
        printf("Empty\n");
    else
        printf("Not Empty\n");
    Push(s,'a');
    Push(s,'b');
    Push(s,'c');
    Push(s,'d');
    Push(s,'e');
    printf("入栈a,b,c,d,e完成后,栈为:");
    if(StackEmpty(s))
        printf("Empty\n");
    else
        printf("Not Empty\n");
    printf("栈的长度为:%d\n",StackLength(s));
    printf("从栈顶到栈底输出栈:");
    DispStack(s);
    printf("依次出栈:\n");
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    Pop(s,e);
    printf("出栈元素为:%c\n",e);
    printf("出栈完成后,栈为:");
    if(StackEmpty(s))
        printf("Empty\n");
    else
        printf("Not Empty\n");
    DestroyStack(s);
    printf("已销毁栈\n");

    return 0;
}

运行结果图:
在这里插入图片描述
【项目2 – 栈的应用】

【题目1】编写一个函数,利用栈的特性实现括号匹配算法。输入为带括号的表达式字符串,函数返回是否匹配。其中括号包括()[]{}三种,请测试括号匹配和不匹配两种情况。

代码:
Sqstack.cpp:

#include <stdio.h>
#include <malloc.h>
#include "sqstack.h"

void InitStack(SqStack *&s)
{
    s=(SqStack *)malloc(sizeof(SqStack));
    s->top=-1;
}
void DestroyStack(SqStack *&s)
{
    free(s);
}
int StackLength(SqStack *s)  //返回栈中元素个数——栈长度
{
    return(s->top+1);
}
bool StackEmpty(SqStack *s)
{
    return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
    if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出
        return false;
    s->top++;
    s->data[s->top]=e;
    return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
    if (s->top==-1)     //栈为空的情况,即栈下溢出
        return false;
    e=s->data[s->top];
    s->top--;
    return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
    if (s->top==-1)         //栈为空的情况,即栈下溢出
        return false;
    e=s->data[s->top];
    return true;
}

void DispStack(SqStack *s)  //输出栈
{
    int i;
    for (i=s->top;i>=0;i--)
        printf("%c ",s->data[i]);
    printf("\n");
}
bool func(char *str)
{
    SqStack *s;
    InitStack(s);
    ElemType e;
    int i=0;
    char ch = *str;
    while(ch!='\0')
    {
        if(ch == '[' || ch == '(' || ch == '{')
            Push(s,ch);
        else if(ch == ')')
        {
            if(!GetTop(s,e))
                return false;
            if(e != '(')
                return false;
            else
                Pop(s,e);
        }
        else if(ch == ']')
        {
            if(!GetTop(s,e))
                return false;
            if(e != '[')
                return false;
            else
                Pop(s,e);
        }
        else if(ch == '}')
        {
            if(!GetTop(s,e))
                return false;
            if(e != '{')
                return false;
            else
                Pop(s,e);
        }
        i++;
        ch = str[i];
    }

    if(StackEmpty(s))
        return true;
    else
        return false;
}

Sqstack.h:

#ifndef SQSTACK_H_INCLUDED
#define SQSTACK_H_INCLUDED

#define MaxSize 100
typedef char ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int top;                //栈指针
} SqStack;                  //顺序栈类型定义

void InitStack(SqStack *&s);    //初始化栈
void DestroyStack(SqStack *&s);  //销毁栈
bool StackEmpty(SqStack *s);     //栈是否为空
int StackLength(SqStack *s);  //返回栈中元素个数——栈长度
bool Push(SqStack *&s,ElemType e); //入栈
bool Pop(SqStack *&s,ElemType &e); //出栈
bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素
void DispStack(SqStack *s);  //输出栈

bool func(char *str);
#endif // SQSTACK_H_INCLUDED

Main.cpp:

#include <stdio.h>
#include "sqstack.h"
#include <iostream>
using namespace std;

int main()
{
    char *str = "[(1+2)*(3-4)-1]*4";
    if(func(str))
        printf("Match\n");
    else
        printf("Not Match\n");

    return 0;
}

运行截图:
Match:
在这里插入图片描述
Not Match:
在这里插入图片描述
【题目2】
假设以键盘输入的方式输入一个正整数序列:比如x1, x2, x3,…,xn,要求算法实现:当ai≠0时,将ai进栈;当ai=0时,输出栈顶元素并出栈。用栈结构存储所输入的整数序列,并且要求算法的健壮性及能够应对异常情况(如果是顺序要判断栈满栈空,如果是链式栈要判断栈空等)并给出相应的提示系统信息。

试编程实现该程序。
① 输入的形式和输入值的范围:需要用户以键盘输入的方式输入一个正整数序列
② 输出的形式:通过运行程序后将最后的正整数序列输出。
③ 程序所能达到的功能:通过对栈的使用完成对正整数序列输出
④ 测试数据:13个元素 1203405607809
输出为:2468

实现这个算法,并完成测试。
程序代码:
Sqstack.cpp:

#include <stdio.h>
#include <malloc.h>
#include "sqstack.h"

void InitStack(SqStack *&s)
{
    s=(SqStack *)malloc(sizeof(SqStack));
    s->top=-1;
}
void DestroyStack(SqStack *&s)
{
    free(s);
}
int StackLength(SqStack *s)  //返回栈中元素个数——栈长度
{
    return(s->top+1);
}
bool StackEmpty(SqStack *s)
{
    return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
    if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出
        return false;
    s->top++;
    s->data[s->top]=e;
    return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
    if (s->top==-1)     //栈为空的情况,即栈下溢出
        return false;
    e=s->data[s->top];
    s->top--;
    return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
    if (s->top==-1)         //栈为空的情况,即栈下溢出
        return false;
    e=s->data[s->top];
    return true;
}

void DispStack(SqStack *s)  //输出栈
{
    int i;
    for (i=s->top;i>=0;i--)
        printf("%c ",s->data[i]);
    printf("\n");
}

Sqstack.h:

#ifndef SQSTACK_H_INCLUDED
#define SQSTACK_H_INCLUDED

#define MaxSize 100
typedef char ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int top;                //栈指针
} SqStack;                  //顺序栈类型定义

void InitStack(SqStack *&s);    //初始化栈
void DestroyStack(SqStack *&s);  //销毁栈
bool StackEmpty(SqStack *s);     //栈是否为空
int StackLength(SqStack *s);  //返回栈中元素个数——栈长度
bool Push(SqStack *&s,ElemType e); //入栈
bool Pop(SqStack *&s,ElemType &e); //出栈
bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素
void DispStack(SqStack *s);  //输出栈

#endif // SQSTACK_H_INCLUDED

main.cpp:

#include <stdio.h>
#include "sqstack.h"
#include <iostream>
using namespace std;

int main()
{
    ElemType temp;
    ElemType e;
    SqStack *s;
    InitStack(s);
    while(scanf("%d",&temp) && temp != -1)
    {
        if(temp == 0)
        {
            if(!Pop(s,e))
                printf("出栈失败\n");
            else
                {
                    printf("%d出栈\n",e);
                }

        }
        else if(temp != 0)
        {
            if(!Push(s,temp))
                printf("入栈失败\n");
            else
                {
                    printf("%d入栈\n",temp);
                }
        }
    }

    return 0;
}

运行结果图:
在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值