实验三 栈和队列
一、实验目的
1、掌握栈的结构特性及其入栈,出栈操作;
2、掌握队列的结构特性及其入队、出队的操作,掌握循环队列的特点及其操作。
二、实验内容和要求
1、完成顺序栈基本操作的函数代码及程序调试。
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
typedef int ElemType;//定义栈中元素类型
#define InitSize 10 //定义顺序栈初始存储空间大小
#define IncSize 4 //定义顺序栈增量存储空间大小
typedef struct SeqStack //定义栈结构
{
ElemType *base; //顺序栈存储空间基地址
int top; //栈顶位置,同时也表示栈中元素个数
int Stack_size; //栈当前空间大小,以元素为单位
///////////栈的基本操作////////////
bool Inc(SeqStack *s);//当栈满是增加空间(5分)
bool Inc(SeqStack* s) {
if ((s->base) - (s->top) > s->Stack_size) {
s->base = (ElemType*)malloc(sizeof(ElemType) * (s->Stack_size + IncSize));
if (!s->base)exit(0);
s->Stack_size += IncSize;
}
}
void InitStack(SeqStack *s);//初始化栈(5分)
void InitStack(SeqStack* s) {
s->base = (ElemType*)malloc(sizeof(ElemType) * InitSize);
if (!s->base) {
printf("InitStack错误/n");
return;
}
s->top = s->base;
s->Stack_size = InitSize;
printf("初始化成功,栈最大值为10\n");
return;
}
截图:
bool IsFull(SeqStack *s);//判断栈是否已满(5分)
bool IsFull(SeqStack* s) {
if (s->top - s->base >= s->Stack_size) {
printf("栈已满\n");
}
else
printf("栈未已满\n");
return true;
}
截图:
bool IsEmpty(SeqStack *s);//判断栈是否为空(5分)
bool IsEmpty(SeqStack* s) {
if (s->top ==s->base) {
printf("栈为空\n");
}
else
printf("栈未空\n");
return true;
}
截图:
void Push(SeqStack *s,ElemType x);//入栈(5分)
void Push(SeqStack* s, ElemType x) {
if ((s->top) - (s->base) >= s->Stack_size) {
printf("栈已满,无法入栈\n");
return;
}
*s->top++ = x;
return;
}
截图:
void Show(SeqStack *s);//显示栈中所有元素(5分)
void Show(SeqStack* s) {
if (s->base == s->top) {
printf("栈空,Show错误");
return;
}
printf("栈中所有元素为:\n");
do {
printf("%d ", *(s->base));
s->base++;
} while (s->top!= s->base);
cout << endl;
return;
}截图:
void Pop(SeqStack *s);//出栈(5分)
void Pop(SeqStack* s) {
if (s == NULL) {
printf("栈为空,无法pop\n");
return;
}
s->top--;//只需下移top
return;
}
截图:
bool GetTop(SeqStack *s,ElemType *v); //获得栈顶元素(5分)
void GetTop(SeqStack* s) {
if (s == NULL) {
printf("栈为空,无栈顶元素\n");
return;
}
printf("%d", *(s->top - 1));
return ;
}
截图:
int Length(SeqStack *s);//返回栈中元素个数(5分)
int Length(SeqStack* s) {
return (s->top)-(s->base);
}
截图:
void Clear(SeqStack *s);//清除栈中元素(5分)
void Clear(SeqStack* s) {
if (s->base)s->top = s->base;
return;
}
截图:
void Destroy(SeqStack *s);//销毁栈
void Destroy(SeqStack* s) {
if (s->base) {
free(s->base);
s->Stack_size = 0;
s->base = NULL;
s->top = NULL;
}
printf("栈销毁成功\n");
return;
}截图:
要求:实现上述函数的功能,并利用主函数测试功能。
2、阅读并运行程序。
(1)应用一:括号匹配。完善并调试程序,并将运行结果截图。(10分)
bool check_kh(char* s)
{
SeqStack st;
InitStack(&st);
ElemType item;
while (*s != '\0')
{
if (*s == '(' || *s == '[')
{
Push(&st,*s);
}
if (*s == ')')
{
GetTop(&st, &item);
if (item!='(')
return false;
else
Pop(&st);
}
if (*s == ']')
{
GetTop(&st, &item);
if (item!='[')
return false;
else
Pop(&st);
}
s = s + 1;
}
if (IsEmpty(&st))
return true;
else
return false;
} if(IsEmpty(&st))
return true;
else
return false;
}
程序测试结果截图:(5分)
(2)应用二: 简单行编辑器 (10分)
void LineEdit()
{
char c;
SeqStack st;
InitStack(&st);
printf("输入字符,以$作为结束标志.\n");
while (GetTop(&st)!='$')
{
if (c == '#')
Pop(&st);
else if (c == '@')
Clear(&st);
else
Push(&st, c);
}
Show(&st);
}程序测试结果截图:(5分)
- 设计算法编写程序,把一个十进制整数转换成为二至九之间的任一进制数输出。(20分)
//类的定义
#include<iostream>
using namespace std;
const int StackSize=100;
class SeqStack
{
public:
SeqStack(){top=-1;} /*初始化一个空栈*/
~SeqStack(){};
void Push(int x); /*将x入栈*/
int Getpop(){if(top!=-1) return data[top];}; /*弹出栈顶元素*/
int Pop(); /*出栈*/
int Empty(){ /*判断栈是否为空*/
if(top==-1)
{
return 1;
}
else{
return 0;
}
}
void Decimaltor(int m,int r);
private:
int data[StackSize]; /*存放栈元素的数组*/
int top;
};
成员函数
void SeqStack::Push(int x)
{
if(top==StackSize) throw "上溢";
else{
data[++top]=x;
}
}
int SeqStack::Pop()
{
if(top==-1) throw"下溢";
else{
int x;
x=data[top--];
return x;
}
}
void SeqStack::Decimaltor(int n,int r)
{
top=-1;
int k;
while(n!=0)
{
k=n%r;
Push(k);
n=n/r;
}
while(top!=-1)
{
cout<<Pop()<<'\t';
}
}
主函数
int main()
{
int n,r;
cout<<'\n'<<"请输入一个十进制数"<<endl;
cin>>n;
cout<<'\n';
cout<<"需要转化为几进制:"<<endl;
cin>>r;
cout<<'\n';
SeqStack one;
one.Decimaltor(n,r);
cout<<'\n'<<endl;
return 0;
}
三、实验小结
本文介绍了栈和队列的基本概念及其实现方法,通过实验深入理解这两种数据结构的特性和操作方式。包括栈的基本操作如入栈、出栈等,并通过实际案例如括号匹配和简易编辑器来展示栈的应用场景。
2374

被折叠的 条评论
为什么被折叠?



