【项目1 - 建立顺序栈算法库】

定义顺序栈存储结构,实现其基本运算,并完成测试。 
  要求: 
  1、头文件sqstack.h中定义数据结构并声明用于完成基本运算的函数。对应基本运算的函数包括:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> InitStack(SqStack *&s);    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化栈</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> DestroyStack(SqStack *&s);  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//销毁栈</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> StackEmpty(SqStack *s);     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//栈是否为空</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> StackLength(SqStack *s);  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回栈中元素个数——栈长度</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> Push(SqStack *&s,ElemType e); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//入栈</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> Pop(SqStack *&s,ElemType &e); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//出栈</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> GetTop(SqStack *s,ElemType &e); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//取栈顶数据元素</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> DispStack(SqStack *s);  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//输出栈</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

2、在sqstack.cpp中实现这些函数 
3、在main函数中完成测试,包括如下内容: 
(1)初始化栈s 
(2)判断s栈是否为空 
(3)依次进栈元素a,b,c,d,e 
(4)判断s栈是否为空 
(5)输出栈长度 
(6)输出从栈顶到栈底元素 
(7)出栈,并输出出栈序列 
(8)判断s栈是否为空 
(9)释放栈 

代码:

#ifndef SQSTACK_H_INCLUDED
#define SQSTACK_H_INCLUDED
#include<iostream>
#include "malloc.h"
#define MaxSize 50
typedef int 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

#include<iostream>
#include<stdio.h>
#include"sqstack.h"
void InitStack(SqStack *&s)//初始化栈
{
    s=(SqStack *)malloc(sizeof(SqStack));
    s->top=-1;
}
void DestroyStack(SqStack *&s)  //销毁栈
{
    free(s);
}
bool StackEmpty(SqStack *s)     //栈是否为空
{
    return (s->top==-1);
}
int StackLength(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)  //输出栈
{
    if(s->top==-1)
        printf("你的栈为空");
    else
    {
        int i;
        for(i=s->top;i>-1;i--)
        {
            printf("%d\n",s->data[i]);

        }

    }


}

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

int main()
{
    SqStack *L;
    int i,length;
    ElemType a=1,b=2,c=3,d=4,e=5,x;
    InitStack(L);
    StackEmpty(L);
    Push(L,a);
    Push(L,b);
    Push(L,c);
    Push(L,d);
    Push(L,e);
    if(StackEmpty(L))
        return false;
    length=StackLength(L);
    printf("栈长为:%d\n",length);
    DispStack(L);
    printf("出栈序列:");
     for(i=0;i<length;i++)
    {
        Pop(L,x);
        printf("%d ",x);


    }
    if(StackEmpty(L))
        DestroyStack(L);
    else
        return false;

    return 0;
}

运行结果:

第一次实验: 题目1表相关算法的实验验证。 [实验目的] 验证单表及其上的基本操作。 [实验内容及要求] 1、 定义单表类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建单表; 2)插入操作:分别在当前结点后、表头、表尾插入值为x的结点; 3)删除操作:分别删除表头结点、表尾结点当前结点的后继结点; 4)存取操作:分别存取当前结点的值表中第k个结点的值; 5)查找操作:查找值为x的元素在单表中的位置(下标)。 题目2 分别给出堆、队列相关算法的实验验证。 [实验目的] 验证堆、队列及其上的基本操作。 [实验内容及要求](以队列为例) 1、 定义队列类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建队列; 2)插入操作:向队尾插入值为x的元素; 3)删除操作:删除队首元素; 4)存取操作:读取队首元素。 第二次实验 题目1 二叉树相关算法的实验验证。 [实验目的] 验证二叉树的接存储结构及其上的基本操作。 [实验内容及要求] 1、 定义接存储的二叉树类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建一棵二叉树,并对其初始化; 2)先根、中根、后根遍历二叉树(递归算法); 3)在二叉树中搜索给定结点的父结点; 4)搜索二叉树中符合数据域条件的结点; 5)从二叉树中删除给定结点及其左右子树。 题目2森林的遍历算法的实验验证。 [实验目的] 验证树森林的遍历算法。 [实验内容及要求] 1、 定义左儿子—右兄弟接存储的树类森林类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建树森林; 2)树森林的先根遍历的递归迭代算法; 3)树森林的后根遍历的递归迭代算法; 4)树森林的层次遍历算法。 题目3 二叉查找树的验证实验。 [实验目的] 验证二叉查找树及其相关操作。 [实验内容及要求] 1、 定义二叉查找树的类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)实现二叉查找树结构; 2) 实现二叉查找树的查找、插入删除等算法; 第三次实验 题目1 邻接表存储的图相关算法的实验验证。 [实验目的] 验证邻接表存的图及其上的基本操作。 [实验内容及要求] 1、 定义邻接表存储的图类。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建一个邻接表存储的图; 2)返回图中指定边的权值; 3)返回图中某顶点的第一个邻接顶点; 4)返回图中某顶点关于另一个顶点的下一个邻接顶点的序号; 5)插入操作:向图中插入一个顶点,插入一条边; 6)删除操作:从图中删除一个顶点,删除一条边。 题目2 图的遍历算法的实验验证。 [实验目的] 验证图的遍历算法。 [实验内容及要求] 1、 定义邻接表存储的图。 2、 实验验证如下算法的正确性、各种功能及指标: 1)创建一个图; 2)图的深度优先遍历的递归算法; 3)图的深度优先遍历的迭代算法; 4)图的广度优先遍历算法。 第四次实验 折半插入排序,堆排序,快速排序 请阅读说明文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值