数据结构作业:顺序栈、链栈的实现及应用(c语言/c++写法)

文章介绍了如何使用顺序栈和链栈进行基本操作,如清空、销毁、入栈、出栈和取栈顶元素,以及结合栈操作实现二进制、八进制和十六进制转换。还展示了如何用这两种数据结构实现计算函数t(n)的递归和非递归算法。
摘要由CSDN通过智能技术生成

本次作业内容:

编写程序任意输入栈长度和栈中的元素值,构造一个顺序栈,对其进行清空销毁入栈出栈以及取栈顶元素操作。

编写程序任意输入栈长度和栈中的元素值,构造一个链栈,对其进行清空销毁入栈出栈以及取栈顶元素操作。

结合堆栈操作,分别实现二、八、十六进制转换操作。

已知函数t(n)=2*t(n/3)+5*n 其中t(0)=1,n为整数。

程序实现:

(1)计算t(n)的递归算法。 

(2)分别用链式栈和顺序栈实现计算t(n)的非递归算法。

 顺序栈:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#define ERROR 0
#define OK 1
#define NOER -1
#define STAKE_size 100//初始分配量
using namespace std;

typedef int SElemType;
typedef int Status;
typedef struct LNode
{
    SElemType* base;
    SElemType* top;//栈顶
    int stacksize;//栈的长度
}SqStack;

Status Initstack(SqStack& S)//创建
{
    S.base = (SElemType*)malloc(sizeof(SElemType) * STAKE_size);
    if (!S.base) exit(NOER);
    S.top = S.base;
    S.stacksize = STAKE_size;
    return OK;
}

void Cleanstack(SqStack& S)//清空
{
    S.top = S.base;
}

void Destorystack(SqStack& S)//销毁
{
    S.stacksize = 0;
    free(S.base);
    S.base = S.top = NULL;
}

bool Empty(SqStack& S)
{
    if (S.top == S.base)return true;
    return false;
}

Status Gettop(SqStack S, SElemType& e)//获取栈顶元素,返回e
{
    if (S.top == S.base) return ERROR;//顺序栈,当栈顶元素==base时栈内没有元素
    e = *(S.top - 1);
    return OK;
}

Status Push(SqStack& S, SElemType e)//入栈
{
    if (S.stacksize <= S.top - S.base)//栈满,空间扩容
    {
        S.base = (SElemType*)realloc(S.base, sizeof(SElemType) * (S.stacksize + STAKE_size));
        if (!S.base) exit(NOER);//分配失败,退出
        S.top = S.base + S.stacksize;//这是?
        S.stacksize += STAKE_size;
    }
    *S.top++ = e;
    return OK;
}

Status Pop(SqStack& S, SElemType& e)//出栈
{
    if (S.top == S.base) return ERROR;
    e = *--S.top;
    return e;
}

void jz()
{
    int num, base;
    cout << "请输入一个十进制数:";
    cin >> num;
    cout << "请输入要转换的进制(2、8、16):";
    cin >> base;
    SqStack S;
    Initstack(S);
    char h[16] = { '0', '1', '2', '3', '4', '5', '6', '7','8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    while (num)
    {
        Push(S, num % base);
        num /= base;
    }
    cout << "转换后的结果为:";
    while (S.top != S.base)
    {
        SElemType e;
        Pop(S, e);
        if (base == 16)
            cout << h[e];
        else
            cout << e;
    }
    cout << endl;

    Destorystack(S);
}

int dg(int n)
{
    if (n == 0)return 1;
    else return 2*dg(n / 3) + 5 * n;
}

int stack_calculate(int n)
{
    SqStack T;
    SElemType ans = 1;
    Initstack(T);
    while (n)
    {
        Push(T, n);
        n /= 3;
    }
    while (!Empty(T))
    {
        SElemType e;
        Pop(T, e);
       ans = 2 * ans + 5 * e;
    }
    return ans;
}

int main()
{
    //jz();
    int n;
    cout << "输入你想要阶乘得到的数:\n";
    cin >> n;
    cout<<"递归答案" << dg(n) << endl;
    cout<<"栈" << stack_calculate(n);

    return 0;
}

 链栈:

#include <iostream>
#include <cstdlib>
#include <cstdio>

#define ERROR 0
#define OK 1
#define NOER -1
#define STAKE_size 100 // 初始分配量

using namespace std;

typedef int SElemType;
typedef int Status;

typedef struct SNode {//链栈
    SElemType data;
    struct SNode* next;
} SqStack, * linkStack;


Status Initstack(linkStack& S) // 创建
{
    S = NULL;
    return OK;
}

void Cleanstack(linkStack& S) // 清空
{
    linkStack p = S;
    while (p != NULL) {
        linkStack temp = p;
        p = p->next;
        free(temp);
    }
    S = NULL;
}

void Destorystack(linkStack& S) // 销毁
{
    Cleanstack(S);
}

bool Empty(linkStack S)
{
    return S == NULL;
}

Status Gettop(linkStack S, SElemType& e) // 获取栈顶元素,返回e
{
    if (Empty(S)) {
        return ERROR;
    }
    e = S->data;
    return OK;
}

Status Push(linkStack& S, SElemType e) // 入栈
{
    linkStack p = (linkStack)malloc(sizeof(SqStack));
    if (!p) {
        return ERROR;
    }
    p->data = e;
    p->next = S;
    S = p;
    return OK;
}

Status Pop(linkStack& S, SElemType& e) // 出栈
{
    if (Empty(S)) {
        return ERROR;
    }
    linkStack p = S;
    e = p->data;
    S = S->next;
    free(p);
    return OK;
}

int Lstack_calculate(int n)
{
    linkStack L;
    SElemType ans = 1;
    Initstack(L);
    while (n)
    {
        Push(L, n);
        n /= 3;
    }
    while (!Empty(L))
    {
        SElemType e;
        Pop(L, e);
        ans = 2 * ans + 5 * e;
    }
    return ans;
}

int main()
{
    int n;
    cout << "输入n的值:\n";
    cin >> n;
    cout<<Lstack_calculate(n);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值