当前位置:网站首页>实验二 栈和队列
实验二 栈和队列
2022-06-21 20:32:00 【续繁华又何处】
一、实验目的
- 掌握栈这种数据结构特性及其主要存储结构,并能在现实生活中灵活运用。
- 掌握队列这种数据结构特性及其主要存储结构,并能在现实生活中灵活运用。
- 了解和掌握递归程序设计的基本原理和方法。
二、实验内容
- 栈的顺序存储结构及实现。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20 /* 数组最大界限 */
typedef int ElemType; /* 数据元素类型 */
typedef struct
{ ElemType a[MAXSIZE]; /* 一维数组子域 */
int top; /* 栈顶指针子域 */
}SqStack; /* 栈的顺序结构体类型 */
SqStack s1;
/* 函数声明 */
void init_s(SqStack *s);
void out_s(SqStack s);
void push(SqStack *s,ElemType e);
ElemType pop(SqStack *s);
/* 主函数 */
main()
{ int k; ElemType e,x; char ch;
init_s( &s1); /* 初始化一个空栈 */
do { printf("\n\n\n");
printf("\n\n 1. 数据元素e进栈 ");
printf("\n\n 2. 出栈一个元素,返回其值");
printf("\n\n 3. 结束程序运行");
printf("\n======================================");
printf("\n 请输入您的选择 (1,2,3)");
scanf("%d",&k);
switch(k)
{ case 1:{ printf("\n 进栈 e=?"); scanf("%d",&e);
push( &s1,e); out_s( s1 );
} break;
case 2:{ x= pop( &s1);
printf("\n出栈元素 : %d", x);
out_s( s1 );
} break;
case 3: exit(0);
} /* switch */
printf("\n ----------------");
}while(k>=1 && k<3);
printf("\n 再见!")
printf(“\n 打回车键,返回。“); ch=getch();
} /* main */
/* 初始化空栈 * /
void init_s(SqStack *s)
{ s->top=-1;
} /* init_s */
/* 输出栈的内容 */
void out_s(SqStack s)
{ char ch; int i; /* 千万不能修改栈顶指针top */
if (s->top==-1) printf(“\n Stack is NULL. “);
else{ i=s->top;
while( i!=-1){ printf(“\n data=%d”, s->a[i]);
i--; }
}
printf(“\n 打回车键,继续。“); ch=getch();
} /* out_c */
/* 进栈函数 */
void push(SqStack *s,ElemType e)
{

}/* push */
/* 出栈函数 */
ElemType pop(SqStack *s)
{

} /* pop */
- 运行结果

- 算法分析
1.创建一个空栈,定义栈的大小 size,以及栈顶元素指针 top
- 判断栈是否为空:当堆栈为空时,返回。当堆栈不为空时,再见 。
- 判断栈是否已满:当堆栈已满时返回,当堆栈未满时,返回 Null。
- 入栈:在线性表最后元素后面插入一个新的数据元素。并改变栈顶指针top 的指向位置
- 出栈:在线性表最后元素后面删除最后一个数据元素。并改变栈顶指针top 的指向位置
- 获取栈顶元素:相当于获取线性表中最后一个数据元素。与插入元素、删除元素不同的是,该操并不改变栈顶指针 top 的指向位置
循环队列(即队列的顺序存储结构)实现。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20 /* 数组最大界限 */
typedef int ElemType; /* 数据元素类型 */
typedef struct
{ ElemType a[MAXSIZE]; /* 一维数组子域 */
int front,rear; /* 头、尾指针子域 */
}SqQueue; /* 循环队列的结构体类型 */
SqQueue Q1;
/* 函数声明 */
void init_Q(SqQueue *Q);
void out_Q(SqQueue Q);
void EnQueue(SqQueue *Q,ElemType e);
ElemType DeQueue(SqQueue *Q);
/* 主函数 */
main()
{ int k; ElemType e,x; char ch;
init_Q( &Q1); /* 初始化一个空循环队列 */
do { printf("\n\n\n");
printf("\n\n 1. 数据元素e进队列 ");
printf("\n\n 2. 出队一个元素,返回其值");
printf("\n\n 3. 结束程序运行");
printf("\n======================================");
printf("\n 请输入您的选择 (1,2,3)");
scanf("%d",&k);
switch(k)
{ case 1:{ printf("\n 进队 e=?"); scanf("%d",&e);
EnQueue(SqQueue &Q1,e); out_Q(Q1);
} break;
case 2:{ x= DeQueue(&Q1);
printf("\n出队元素 : %d", x);
out_Q(Q1 );
out_Q(Q1 );
} break;
case 3: exit(0);
} /* switch */
printf("\n ----------------");
}while(k>=1 && k<3);
printf("\n 再见!");
printf(“\n 打回车键,返回。“); ch=getch();
} /* main */
/* 初始化空队列 * /
void init_Q(SqQueue *Q)
{ Q->front=0; Q->rear=0;
} /* init_Q */
/* 输出队列的内容 */
void out_Q(SqQueue Q)
{ char ch; int i;
/* 不能修改队列头、尾指针 */

} /* out_Q */
/ * 进队函数 */
void EnQueue(SqQueue *Q,ElemType e)
{

}/* EnQueue */
/* 出队函数 */
ElemType DeQueue(SqQueue *Q)
{

} /* DeQueue */

- 算法分析
1. 初始化一个空循环队列 。
2. 从队⾸获取元素。如果队列为空,返回,队列满则出错 。
3. 获取队尾元素。如果队列为空,返回,队列满则出错 。
4. 向循环队列插⼊⼀个元素。如果成功插⼊则返回。
5. 从循环队列中删除⼀个元素。如果成功删除则返回。
边栏推荐
猜你喜欢

北京 加速生态建设 迈动互联与摩尔线程完成产品兼容互认证

B2B mall website helps enterprises speed up distribution and build an efficient and intelligent B2B online distribution platform

Lifting method (I) AdaBoost

Leetcode question brushing: SF Technology Smart logistics Campus Technology Challenge

技术分享 | MySQL中一个聚类增量统计 SQL 的需求

技术分享 | kubernetes pod 简介
![[deeply understand tcapulusdb technology] tmonitor background one click installation](/img/f6/d2a287aac4ef3dfa8c75f7130202a4.png)
[deeply understand tcapulusdb technology] tmonitor background one click installation

力扣刷题集结4(mysql版本)

提升方法(下)提升树

HIC Pro | HIC data processing tool
随机推荐
Self made C compiler
GDB调试实战(10)多线程调试
分别利用for、while、do while,循环求1-100的和
TRNA analysis using trnascan se
对“XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们
高项-立项管理
InstaDeep Ltd:Arthur Flajolet | 单机上基于群体的快速强化学习
Luogu p1514 [noip2010 improvement group] water diversion into the city
IQtree|构建进化树的软件
如何卸载用conda命令安装的包
Utilisation de la combinaison d'assertions de l'API Stream et de la mise en cache locale pour les requêtes floues (près de 1000 fois plus efficace que MySQL)
使用StreamAPI 斷言組合,結合本地緩存做模糊查詢(比mysql效率提昇近1000倍)
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
Advanced packaging, the beginning of a big cycle -- a symposium on semiconductor equipment
Common abbreviations and terms of mitochondrial genome
IP-guard打印管控,防止打印渠道信息泄露
Iqtree| software for constructing evolutionary tree
Hiclotter|hic data visualization tool
Using streamapi assertion combination and local cache for fuzzy query (nearly 1000 times more efficient than MySQL)
pi4j gpio针脚上拉电阻,下拉电阻概念