实验报告 基于C语言数字结构顺序栈的操作(含完整代码,运行截图)

一、实验目的

1. 掌握栈的顺序存储结构及其编程。

二、实验原理或意义

通过上机实验,加深对所学基本知识:掌握对顺序栈的基本操作。

三、实验重点

1. 进栈与出栈操作。

四、实验难点

1. 进栈与出栈操作。

五、实验方法(或技术)

上机实验时一边调试程序一边将实验报告上关于程序调试和运行结果的信息填写到实验报告上,实验完成时上交实验报告。

六、实验内容

1.顺序栈的操作编程

(1)根据程序环境的设置,完成顺序栈的结构体定义。

(2)编写创建顺序栈函数。

(3)编写元素进栈函数。

(4)编写出栈函数。

(5)编写取栈顶元素的函数。

(6)编写main函数,完成对上述函数的调用。

(7)编写数制转换函数,并在main函数中调用。

七、实验报告要求内容

1.实验目的

(1)掌握栈的顺序存储结构及其编程。

2.实验要求

(1)通过上机实验,加深对所学基本知识:掌握对顺序栈的基本操作;

(2)上机实验时一边调试程序一边将实验报告上关于程序调试和运行结果的信息填写到实验报告上,实验完成时上交实验报告。

3.实验代码

#include <stdio.h>

#include <stdlib.h>

#define Stack_Init_Size 10 // 初始化栈的最大长度

#define StackIncrement 10 // 若栈最大空间不够时,需要增加的长度

typedef int ElemType;

typedef int Status;

typedef struct {

ElemType* base; // 栈底指针

ElemType* top; // 栈顶指针

int stack_size; // 栈的最大长度

} SqStack;

// 初始化栈

Status InitStack(SqStack* S) {

// 分配初始空间

S->base = (ElemType*)malloc(Stack_Init_Size * sizeof(ElemType));   if (!S->base) {

exit(0);

}

S->top = S->base; /// 栈顶与栈底相同

S->stack_size = Stack_Init_Size; // 栈的最大长度等于初始长度

return 1;

}

// 判断栈是否为空,只需要判断栈顶指针与栈底指针是否相同即可

Status EmptyStack(SqStack* S) {

return S->base == S->top;

}

// 获取栈的实际长度,栈顶减去栈底指针即为栈的长度

Status LengthStack(SqStack* S) {

if (S->top == S->base) {

return 0;

}

return (Status)(S->top - S->base);

}

// 获取栈顶的元素,参数e用来存放栈顶的元素

Status GetTopStack(SqStack* S, ElemType* e) {

if (S->top == S->base) {

return 0;

}

*e = *(S->top - 1);

return 1;

}

// 进栈,参数e是要进栈的元素

Status PushStack(SqStack* S, ElemType e) {

// 若栈的最大长度不会够用时,重新开辟,增大长度

if (S->top - S->base >= S->stack_size) {

S->base = (ElemType*)realloc(S->base, (S->stack_size + StackIncrement)

* sizeof(ElemType));

if (!S->base) {

return 0;

}

// 栈顶指针为栈底指针加上栈之前的最大长度

S->top = S->base + S->stack_size;

// 栈当前的最大长度等于栈之前的最大长度与增加的长度之和

S->stack_size += StackIncrement;

}

*S->top++ = e; // 先赋值,后栈顶指针上移

return 1;

}

// 出栈,参数e用来存放出栈的元素

Status PopStack(SqStack* S, ElemType* e) {

if (S->base == S->top) {

return 0;

}

*e = *--S->top; // 栈顶指针先下移,后赋值

return 1;

}

// 销毁栈,释放栈空间,栈顶栈底指针置为NULL,长度置为0

Status DestroyStack(SqStack* S) {

free(S->base);

S->base = S->top = NULL;

S->stack_size = 0;

return 1;

}

// 遍历栈,依次打印每个元素

Status StackTraverse(SqStack* S) {

ElemType* p;

if (S->top == S->base) {

printf("Stack is NULL.\n");

return 0;

}

p = S->top;

// 由栈顶依次向下遍历

while (p > S->base) {

p--;

printf("%d ", *p);

}

printf("\n");

return 1;

}

int main() {

SqStack q, * S;

S = &q;

int i, n, e;

printf("Creat a NULL Stack :\n");

InitStack(S);

printf("input the length of the Stack :\n");

scanf("%d", &n);

for (i = 1; i <= n; i++) {

scanf("%d", &e);

PushStack(S, e);

}

printf("Is the stack NULL?\n");

if (EmptyStack(S)) {

printf("Yes!\n");

}

else {

printf("No!\n");

}

printf("The length of stack is %d.\n", LengthStack(S));

printf("The stack is :\n");

StackTraverse(S);

GetTopStack(S, &e);

printf("The top data is %d.\n", e);

printf("input the data to the stack :\n");

scanf("%d", &e);

PushStack(S, e);

printf("The new stack is :\n");

StackTraverse(S);

printf("Delete the top data : ");

e = PopStack(S, &e);

printf("%d\n", e);

printf("The new stack is :\n");

StackTraverse(S);

printf("Destroy the stack :\n");

DestroyStack(S);

StackTraverse(S);

return 0;

}

4.实验结果截图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值