本次作业内容:
编写程序任意输入栈长度和栈中的元素值,构造一个顺序栈,对其进行清空、销毁、入栈、出栈以及取栈顶元素操作。
编写程序任意输入栈长度和栈中的元素值,构造一个链栈,对其进行清空、销毁、入栈、出栈以及取栈顶元素操作。
结合堆栈操作,分别实现二、八、十六进制转换操作。
已知函数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;
}