mirror of
https://github.com/CodePanda66/CSPostgraduate-408.git
synced 2023-05-21 21:49:33 +08:00
✨ Add LinkQueue
This commit is contained in:
@@ -71,10 +71,11 @@ bool GetTop(SqStack S, int &x) {
|
||||
x = S.data[S.top];
|
||||
return true;
|
||||
}
|
||||
|
||||
//读取栈顶元素,初始化1
|
||||
bool GetTop1(SqStack S, int &x) {
|
||||
if (S.top == 0)return false;
|
||||
x = S.data[S.top-1];//注意按初始化1的方式,这里指针减1才是栈顶元素的位置,
|
||||
x = S.data[S.top - 1];//注意按初始化1的方式,这里指针减1才是栈顶元素的位置,
|
||||
// 同时注意不能使用--S.top,因为这里是读取,不可修改原栈,所以不可和出栈一样
|
||||
//但即时这里你错误使用了--S.top,也不会有问题,因为此处的S是值传递,非引用传递,所以你修改的也只是复制之后的S,不会影响原栈S,即时这样也不建议使用--S.top,如果老师较真的话,可以扣分哒
|
||||
return true;
|
||||
@@ -90,23 +91,24 @@ int GetTopOther(SqStack S) {
|
||||
|
||||
int GetTopOther1(SqStack S) {
|
||||
if (S.top == 0)return -1;
|
||||
return S.data[S.top-1];
|
||||
return S.data[S.top - 1];
|
||||
}
|
||||
|
||||
|
||||
//打印整个栈
|
||||
void PrintStack(SqStack S){
|
||||
void PrintStack(SqStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top>=0){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top,S.data[S.top--]);
|
||||
while (S.top >= 0) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", S.top, S.data[S.top--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
//打印整个栈,初始化方式1
|
||||
void PrintStack1(SqStack S){
|
||||
void PrintStack1(SqStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top>0){//注意判空的条件
|
||||
printf("S1[%d]=%d\n",S.top-1,S.data[--S.top]);//初始化方式1得先移动指针再获取元素
|
||||
while (S.top > 0) {//注意判空的条件
|
||||
printf("S1[%d]=%d\n", S.top - 1, S.data[--S.top]);//初始化方式1得先移动指针再获取元素
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
@@ -117,34 +119,34 @@ void testStack() {
|
||||
SqStack S;
|
||||
printf("测试第一种初始化方式\n");
|
||||
InitStack(S);
|
||||
if (Push(S,1)){
|
||||
if (Push(S, 1)) {
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push(S,2)){
|
||||
if (Push(S, 2)) {
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack(S);
|
||||
int x;
|
||||
if (Pop(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (Pop(S, x)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack(S);
|
||||
int x1;
|
||||
if (GetTop(S,x1)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x1);
|
||||
}else{
|
||||
if (GetTop(S, x1)) {
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x1);
|
||||
} else {
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
int x4=GetTopOther(S);
|
||||
if (x4!=-1){
|
||||
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n",x4);
|
||||
} else{
|
||||
int x4 = GetTopOther(S);
|
||||
if (x4 != -1) {
|
||||
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n", x4);
|
||||
} else {
|
||||
printf("第二种读取栈顶元素的方式失败了\n");
|
||||
}
|
||||
|
||||
@@ -152,34 +154,34 @@ void testStack() {
|
||||
printf("测试第二种初始化方式\n");
|
||||
SqStack S1;
|
||||
InitStack1(S1);
|
||||
if (Push1(S1,1)){
|
||||
if (Push1(S1, 1)) {
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push1(S1,2)){
|
||||
if (Push1(S1, 2)) {
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack1(S1);
|
||||
int x2;
|
||||
if (Pop1(S1, x2)){
|
||||
printf("出栈成功,弹出的元素为[%d]\n",x2);
|
||||
} else{
|
||||
if (Pop1(S1, x2)) {
|
||||
printf("出栈成功,弹出的元素为[%d]\n", x2);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack1(S1);
|
||||
int x3;
|
||||
if (GetTop1(S1,x3)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x3);
|
||||
}else{
|
||||
if (GetTop1(S1, x3)) {
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x3);
|
||||
} else {
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
int x5=GetTopOther1(S1);
|
||||
if (x5!=-1){
|
||||
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n",x5);
|
||||
} else{
|
||||
int x5 = GetTopOther1(S1);
|
||||
if (x5 != -1) {
|
||||
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n", x5);
|
||||
} else {
|
||||
printf("第二种读取栈顶元素的方式失败了\n");
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ typedef struct {
|
||||
//初始化
|
||||
void InitStack(ShStack &S) {
|
||||
S.top0 = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
S.top1 =MaxSize;//这里的MaxSize就是所谓的第二个栈的栈底
|
||||
S.top1 = MaxSize;//这里的MaxSize就是所谓的第二个栈的栈底
|
||||
//可以根据顺序栈的第二种初试化方式,思考一下这种共享顺序栈的第二种初始化方式
|
||||
//S.top0=0
|
||||
//S.top1=MaxSize-1
|
||||
@@ -26,19 +26,19 @@ void InitStack(ShStack &S) {
|
||||
|
||||
//入栈0
|
||||
bool Push0(ShStack &S, int t) {
|
||||
if (S.top0 +1== S.top1)return false;//注意共享栈满的条件
|
||||
if (S.top0 + 1 == S.top1)return false;//注意共享栈满的条件
|
||||
S.data[++S.top0] = t;//仔细品味一下这个++S.top
|
||||
return true;
|
||||
}
|
||||
|
||||
//入栈1
|
||||
bool Push1(ShStack &S, int t) {
|
||||
if (S.top0 +1== S.top1)return false;//注意共享栈满的条件
|
||||
if (S.top0 + 1 == S.top1)return false;//注意共享栈满的条件
|
||||
S.data[--S.top1] = t;//仔细品味一下这个--S.top,想想为什么?
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//出栈,并打印出栈顶元素
|
||||
bool Pop0(ShStack &S, int &x) {
|
||||
//判断
|
||||
@@ -49,6 +49,7 @@ bool Pop0(ShStack &S, int &x) {
|
||||
// S.top -=1;//再改指针
|
||||
return true;
|
||||
}
|
||||
|
||||
//出栈1
|
||||
bool Pop1(ShStack &S, int &x) {
|
||||
//判断
|
||||
@@ -66,6 +67,7 @@ bool GetTop0(ShStack S, int &x) {
|
||||
x = S.data[S.top0];
|
||||
return true;
|
||||
}
|
||||
|
||||
//栈1
|
||||
bool GetTop1(ShStack S, int &x) {
|
||||
if (S.top1 == MaxSize)return false;
|
||||
@@ -75,18 +77,19 @@ bool GetTop1(ShStack S, int &x) {
|
||||
|
||||
|
||||
//打印整个栈,栈0
|
||||
void PrintStack0(ShStack S){
|
||||
void PrintStack0(ShStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top0>-1){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top0,S.data[S.top0--]);
|
||||
while (S.top0 > -1) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", S.top0, S.data[S.top0--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
//打印整个栈
|
||||
void PrintStack1(ShStack S){
|
||||
void PrintStack1(ShStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top1<MaxSize){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top1,S.data[S.top1++]);
|
||||
while (S.top1 < MaxSize) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", S.top1, S.data[S.top1++]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
@@ -96,55 +99,55 @@ void testShStack() {
|
||||
ShStack S;
|
||||
InitStack(S);
|
||||
printf("测试第一个栈\n");
|
||||
if (Push0(S,1)){
|
||||
if (Push0(S, 1)) {
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push0(S,2)){
|
||||
if (Push0(S, 2)) {
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
int x;
|
||||
if (Pop0(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (Pop0(S, x)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
int x1;
|
||||
if (GetTop0(S,x1)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x1);
|
||||
}else{
|
||||
if (GetTop0(S, x1)) {
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x1);
|
||||
} else {
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
printf("测试第二个栈\n");
|
||||
if (Push1(S,10)){
|
||||
if (Push1(S, 10)) {
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push1(S,9)){
|
||||
if (Push1(S, 9)) {
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x3;
|
||||
if (Pop1(S, x3)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x3);
|
||||
} else{
|
||||
if (Pop1(S, x3)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x3);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x4;
|
||||
if (GetTop1(S,x4)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x4);
|
||||
}else{
|
||||
if (GetTop1(S, x4)) {
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x4);
|
||||
} else {
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
# define MaxSize 10
|
||||
typedef struct LinkNode{
|
||||
typedef struct LinkNode {
|
||||
int data;
|
||||
struct LinkNode *next;
|
||||
} *LinkStack;
|
||||
@@ -15,22 +15,22 @@ typedef struct LinkNode{
|
||||
|
||||
//初始化
|
||||
bool InitStack(LinkStack &LS) {
|
||||
LS=(LinkNode *)malloc(sizeof(LinkNode));//分配一个头节点
|
||||
if(LS==NULL){
|
||||
return false;
|
||||
}
|
||||
LS->next=NULL;
|
||||
LS = (LinkNode *) malloc(sizeof(LinkNode));//分配一个头节点
|
||||
if (LS == NULL) {
|
||||
return false;
|
||||
}
|
||||
LS->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
//入栈 参考头插法建立单链表
|
||||
bool Push(LinkStack &LS, int t) {
|
||||
//入站不需要检查
|
||||
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
|
||||
if (s==NULL)return false;
|
||||
s->data=t;
|
||||
s->next=LS->next;
|
||||
LS->next=s;
|
||||
//入站不需要检查
|
||||
LinkNode *s = (LinkNode *) malloc(sizeof(LinkNode));
|
||||
if (s == NULL)return false;
|
||||
s->data = t;
|
||||
s->next = LS->next;
|
||||
LS->next = s;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -38,11 +38,11 @@ bool Push(LinkStack &LS, int t) {
|
||||
//出栈,并打印出栈顶元素
|
||||
bool Pop(LinkStack &LS, int &x) {
|
||||
//判断
|
||||
if (LS->next==NULL)return false;//栈空,这里的条件
|
||||
LinkNode *q ;
|
||||
q=LS->next;
|
||||
LS->next=q->next;
|
||||
x=q->data;
|
||||
if (LS->next == NULL)return false;//栈空,这里的条件
|
||||
LinkNode *q;
|
||||
q = LS->next;
|
||||
LS->next = q->next;
|
||||
x = q->data;
|
||||
free(q);
|
||||
return true;
|
||||
}
|
||||
@@ -50,21 +50,21 @@ bool Pop(LinkStack &LS, int &x) {
|
||||
|
||||
//读取栈顶元素,栈
|
||||
bool GetTop(LinkStack LS, int &x) {
|
||||
if (LS==NULL)return false;
|
||||
x=LS->next->data;
|
||||
if (LS == NULL)return false;
|
||||
x = LS->next->data;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//打印整个栈,栈
|
||||
void PrintStack(LinkStack LS){
|
||||
void PrintStack(LinkStack LS) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
int i=0;int x;
|
||||
LinkNode *p=LS->next;
|
||||
while (p!=NULL){//注意判空的条件
|
||||
printf("S[%d]=%d\n",i,p->data);
|
||||
p=p->next;
|
||||
int i = 0;
|
||||
int x;
|
||||
LinkNode *p = LS->next;
|
||||
while (p != NULL) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", i, p->data);
|
||||
p = p->next;
|
||||
i++;
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
@@ -75,40 +75,40 @@ void testLinkStack() {
|
||||
LinkStack S;
|
||||
InitStack(S);
|
||||
printf("测试第一个栈\n");
|
||||
if (Push(S,1)){
|
||||
if (Push(S, 1)) {
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push(S,2)){
|
||||
if (Push(S, 2)) {
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack(S);
|
||||
int x;
|
||||
if (Pop(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (Pop(S, x)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
|
||||
PrintStack(S);
|
||||
int x1;
|
||||
if (GetTop(S,x1)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x1);
|
||||
}else{
|
||||
if (GetTop(S, x1)) {
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x1);
|
||||
} else {
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
if (Pop(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (Pop(S, x)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
if (Pop(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (Pop(S, x)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
@@ -5,96 +5,98 @@
|
||||
//循环顺序队列的第一种实现方式
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define MaxSize 10
|
||||
|
||||
typedef struct {
|
||||
int data[MaxSize];//
|
||||
int front,rear;//对头指针和队尾指针
|
||||
}SqQueue;
|
||||
int front, rear;//对头指针和队尾指针
|
||||
} SqQueue;
|
||||
|
||||
//初始化
|
||||
void InitQueue(SqQueue &Q){
|
||||
Q.rear=Q.front=0;//初始化时,队头队尾都指向0
|
||||
void InitQueue(SqQueue &Q) {
|
||||
Q.rear = Q.front = 0;//初始化时,队头队尾都指向0
|
||||
}
|
||||
|
||||
//判空
|
||||
bool QueueEmpty(SqQueue Q){
|
||||
if(Q.front==Q.rear)
|
||||
bool QueueEmpty(SqQueue Q) {
|
||||
if (Q.front == Q.rear)
|
||||
return true;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
//入队操作
|
||||
bool EnQueue(SqQueue &Q,int t){
|
||||
if((Q.rear+1)%MaxSize==Q.front)return false;//队满,注意这里的判满条件
|
||||
bool EnQueue(SqQueue &Q, int t) {
|
||||
if ((Q.rear + 1) % MaxSize == Q.front)return false;//队满,注意这里的判满条件
|
||||
//这里的判满条件会造成浪费一个存储空间的问题
|
||||
Q.data[Q.rear]=t;
|
||||
Q.rear=(Q.rear+1)%MaxSize;//通过取余操作让整个队列循环起来
|
||||
Q.data[Q.rear] = t;
|
||||
Q.rear = (Q.rear + 1) % MaxSize;//通过取余操作让整个队列循环起来
|
||||
return true;
|
||||
}
|
||||
|
||||
//出队操作
|
||||
bool DeQueue(SqQueue &Q,int &x){
|
||||
if(Q.rear==Q.front)return false;//队空
|
||||
x=Q.data[Q.front];
|
||||
Q.front=(Q.front+1)%MaxSize;
|
||||
bool DeQueue(SqQueue &Q, int &x) {
|
||||
if (Q.rear == Q.front)return false;//队空
|
||||
x = Q.data[Q.front];
|
||||
Q.front = (Q.front + 1) % MaxSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
//获取队头元素,用x返回
|
||||
bool GetHead(SqQueue Q,int &x){
|
||||
if (Q.front==Q.rear)return false;
|
||||
x=Q.data[Q.front];
|
||||
bool GetHead(SqQueue Q, int &x) {
|
||||
if (Q.front == Q.rear)return false;
|
||||
x = Q.data[Q.front];
|
||||
return true;
|
||||
}
|
||||
|
||||
//打印整个队列
|
||||
void PrintQueue(SqQueue Q){
|
||||
void PrintQueue(SqQueue Q) {
|
||||
printf("开始打印队列\n");
|
||||
while(Q.front!=Q.rear){
|
||||
printf("Q[%d]=%d\n",Q.front,Q.data[Q.front]);
|
||||
Q.front=(Q.front+1)%MaxSize;
|
||||
while (Q.front != Q.rear) {
|
||||
printf("Q[%d]=%d\n", Q.front, Q.data[Q.front]);
|
||||
Q.front = (Q.front + 1) % MaxSize;
|
||||
}
|
||||
printf("打印完毕!\n");
|
||||
}
|
||||
|
||||
//测试
|
||||
void TestQueue(){
|
||||
void TestQueue() {
|
||||
printf("开始测试!\n");
|
||||
SqQueue Q;
|
||||
InitQueue(Q);
|
||||
if (EnQueue(Q,1)){
|
||||
if (EnQueue(Q, 1)) {
|
||||
printf("入队成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入队失败了\n");
|
||||
}
|
||||
if (EnQueue(Q,2)){
|
||||
if (EnQueue(Q, 2)) {
|
||||
printf("入队又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入队又失败了\n");
|
||||
}
|
||||
PrintQueue(Q);
|
||||
int x;
|
||||
if (DeQueue(Q, x)){
|
||||
printf("出队成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if(GetHead(Q,x)){
|
||||
printf("获取队头成功!,队头元素为:%d\n",x);
|
||||
} else{
|
||||
if (GetHead(Q, x)) {
|
||||
printf("获取队头成功!,队头元素为:%d\n", x);
|
||||
} else {
|
||||
printf("获取队头元素失败!");
|
||||
}
|
||||
if(QueueEmpty(Q)){
|
||||
if (QueueEmpty(Q)) {
|
||||
printf("队空啦\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("队非空\n");
|
||||
}
|
||||
|
||||
printf("结束测试!\n");
|
||||
}
|
||||
|
||||
int main(){
|
||||
int main() {
|
||||
TestQueue();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -5,97 +5,99 @@
|
||||
//循环顺序队列的第二种实现方式
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define MaxSize 10
|
||||
|
||||
typedef struct {
|
||||
int data[MaxSize];//
|
||||
int front,rear;//对头指针和队尾指针
|
||||
int front, rear;//对头指针和队尾指针
|
||||
int size;//利用size变量记录队列长度,并用作判满的条件!有了size就不会浪费一个存储空间
|
||||
}SqQueue;
|
||||
} SqQueue;
|
||||
|
||||
//初始化
|
||||
void InitQueue(SqQueue &Q){
|
||||
Q.rear=Q.front=0;//初始化时,队头队尾都指向0
|
||||
Q.size=0;//初试长度
|
||||
void InitQueue(SqQueue &Q) {
|
||||
Q.rear = Q.front = 0;//初始化时,队头队尾都指向0
|
||||
Q.size = 0;//初试长度
|
||||
}
|
||||
|
||||
//判空
|
||||
bool QueueEmpty(SqQueue Q){
|
||||
if(Q.size==0)//有了size,条件不一样了
|
||||
bool QueueEmpty(SqQueue Q) {
|
||||
if (Q.size == 0)//有了size,条件不一样了
|
||||
return true;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
//入队操作
|
||||
bool EnQueue(SqQueue &Q,int t){
|
||||
if(Q.size==MaxSize)return false;//队满,注意这里的判满条件
|
||||
Q.data[Q.rear]=t;
|
||||
Q.rear=(Q.rear+1)%MaxSize;//通过取余操作让整个队列循环起来
|
||||
bool EnQueue(SqQueue &Q, int t) {
|
||||
if (Q.size == MaxSize)return false;//队满,注意这里的判满条件
|
||||
Q.data[Q.rear] = t;
|
||||
Q.rear = (Q.rear + 1) % MaxSize;//通过取余操作让整个队列循环起来
|
||||
Q.size++;
|
||||
return true;
|
||||
}
|
||||
|
||||
//出队操作
|
||||
bool DeQueue(SqQueue &Q,int &x){
|
||||
if(Q.size==0)return false;//队空
|
||||
x=Q.data[Q.front];
|
||||
Q.front=(Q.front+1)%MaxSize;
|
||||
bool DeQueue(SqQueue &Q, int &x) {
|
||||
if (Q.size == 0)return false;//队空
|
||||
x = Q.data[Q.front];
|
||||
Q.front = (Q.front + 1) % MaxSize;
|
||||
Q.size--;
|
||||
return true;
|
||||
}
|
||||
|
||||
//获取队头元素,用x返回
|
||||
bool GetHead(SqQueue Q,int &x){
|
||||
if (Q.size==0)return false;
|
||||
x=Q.data[Q.front];
|
||||
bool GetHead(SqQueue Q, int &x) {
|
||||
if (Q.size == 0)return false;
|
||||
x = Q.data[Q.front];
|
||||
return true;
|
||||
}
|
||||
|
||||
//打印整个队列
|
||||
void PrintQueue(SqQueue Q){
|
||||
void PrintQueue(SqQueue Q) {
|
||||
printf("开始打印队列\n");
|
||||
while(Q.front!=Q.rear){
|
||||
printf("Q[%d]=%d\n",Q.front,Q.data[Q.front]);
|
||||
Q.front=(Q.front+1)%MaxSize;
|
||||
while (Q.front != Q.rear) {
|
||||
printf("Q[%d]=%d\n", Q.front, Q.data[Q.front]);
|
||||
Q.front = (Q.front + 1) % MaxSize;
|
||||
}
|
||||
printf("打印完毕!\n");
|
||||
}
|
||||
|
||||
//测试
|
||||
void TestQueue(){
|
||||
void TestQueue() {
|
||||
printf("开始测试!\n");
|
||||
SqQueue Q;
|
||||
InitQueue(Q);
|
||||
if (EnQueue(Q,1)){
|
||||
if (EnQueue(Q, 1)) {
|
||||
printf("入队成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入队失败了\n");
|
||||
}
|
||||
if (EnQueue(Q,2)){
|
||||
if (EnQueue(Q, 2)) {
|
||||
printf("入队又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入队又失败了\n");
|
||||
}
|
||||
PrintQueue(Q);
|
||||
int x;
|
||||
if (DeQueue(Q, x)){
|
||||
printf("出队成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if(GetHead(Q,x)){
|
||||
printf("获取队头成功!,队头元素为:%d\n",x);
|
||||
} else{
|
||||
if (GetHead(Q, x)) {
|
||||
printf("获取队头成功!,队头元素为:%d\n", x);
|
||||
} else {
|
||||
printf("获取队头元素失败!\n");
|
||||
}
|
||||
if (DeQueue(Q, x)){
|
||||
printf("出队成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if(QueueEmpty(Q)){
|
||||
if (QueueEmpty(Q)) {
|
||||
printf("队空啦\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("队非空\n");
|
||||
}
|
||||
|
||||
@@ -103,7 +105,7 @@ void TestQueue(){
|
||||
printf("结束测试!\n");
|
||||
}
|
||||
|
||||
int main(){
|
||||
int main() {
|
||||
TestQueue();
|
||||
return 0;
|
||||
}
|
||||
@@ -66,36 +66,36 @@ void TestQueue(){
|
||||
printf("开始测试!\n");
|
||||
SqQueue Q;
|
||||
InitQueue(Q);
|
||||
if (EnQueue(Q,1)){
|
||||
if (EnQueue(Q, 1)) {
|
||||
printf("入队成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入队失败了\n");
|
||||
}
|
||||
if (EnQueue(Q,2)){
|
||||
if (EnQueue(Q, 2)) {
|
||||
printf("入队又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入队又失败了\n");
|
||||
}
|
||||
PrintQueue(Q);
|
||||
int x;
|
||||
if (DeQueue(Q, x)){
|
||||
printf("出队成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if(GetHead(Q,x)){
|
||||
printf("获取队头成功!,队头元素为:%d\n",x);
|
||||
} else{
|
||||
if (GetHead(Q, x)) {
|
||||
printf("获取队头成功!,队头元素为:%d\n", x);
|
||||
} else {
|
||||
printf("获取队头元素失败!\n");
|
||||
}
|
||||
if (DeQueue(Q, x)){
|
||||
printf("出队成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if(QueueEmpty(Q)){
|
||||
if (QueueEmpty(Q)) {
|
||||
printf("队空啦\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("队非空\n");
|
||||
}
|
||||
|
||||
|
||||
120
DataStructure/DS_2_StackAndQueue/DS_2_6_LiQueue.cpp
Normal file
120
DataStructure/DS_2_StackAndQueue/DS_2_6_LiQueue.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/2.
|
||||
//
|
||||
|
||||
//链式队列(带头节点版本)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct LinkNode {
|
||||
int data;
|
||||
struct LinkNode *next;
|
||||
} LinkNode;
|
||||
|
||||
typedef struct {
|
||||
LinkNode *front, *rear;
|
||||
} LinkQueue;
|
||||
|
||||
void InitQueue(LinkQueue &Q) {
|
||||
Q.front = Q.rear = (LinkNode *) malloc(sizeof(LinkNode));
|
||||
//初始化时,front 、rear 都指向头节点
|
||||
Q.front->next = NULL;
|
||||
}
|
||||
|
||||
//判空
|
||||
bool IseEmpty(LinkQueue Q) {
|
||||
if (Q.front == Q.rear)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
//入队操作
|
||||
bool EnQueue(LinkQueue &Q, int x) {
|
||||
//判满?链式存储一般不需要判满,除非内存不足
|
||||
LinkNode *s = (LinkNode *) malloc(sizeof(LinkNode));
|
||||
if (s == NULL)return false;
|
||||
s->data = x;
|
||||
s->next = NULL;
|
||||
Q.rear->next = s;//新节点插入到rear之后
|
||||
Q.rear = s;//修改表尾指针
|
||||
return true;
|
||||
}
|
||||
|
||||
//出队
|
||||
bool DeQueue(LinkQueue &Q, int &x) {
|
||||
if (Q.front == Q.rear)return false;//队空
|
||||
LinkNode *p = Q.front->next;//用指针p记录队头元素
|
||||
x = p->data;//用x变量返回队头元素
|
||||
Q.front->next = p->next;//修改头节点的next指针
|
||||
if (Q.rear == p)//此次是最后一个节点出队
|
||||
Q.rear = Q.front;//修改rear指针,思考一下为什么?
|
||||
free(p); //释放节点空间
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetHead(LinkQueue Q, int &x) {
|
||||
if (Q.front == Q.rear)return false;//队空
|
||||
x = Q.front->next->data;//用x变量返回队头元素
|
||||
return true;
|
||||
}
|
||||
bool QueueEmpty(LinkQueue Q){
|
||||
return Q.front==Q.rear? true: false;
|
||||
}
|
||||
|
||||
void PrintQueue(LinkQueue Q) {
|
||||
printf("开始打印队列\n");
|
||||
int i = 0;
|
||||
while (Q.front != Q.rear) {
|
||||
Q.front = Q.front->next;
|
||||
printf("Q[%d]=%d\n", i++, Q.front->data);
|
||||
}
|
||||
printf("打印完毕!\n");
|
||||
}
|
||||
|
||||
//测试函数
|
||||
void TestLinkQueue() {
|
||||
printf("开始测试!\n");
|
||||
LinkQueue Q;
|
||||
InitQueue(Q);
|
||||
if (EnQueue(Q, 1)) {
|
||||
printf("入队成功啦!\n");
|
||||
} else {
|
||||
printf("入队失败了\n");
|
||||
}
|
||||
if (EnQueue(Q, 2)) {
|
||||
printf("入队又成功啦!\n");
|
||||
} else {
|
||||
printf("入队又失败了\n");
|
||||
}
|
||||
PrintQueue(Q);
|
||||
int x;
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if (GetHead(Q, x)) {
|
||||
printf("获取队头成功!,队头元素为:%d\n", x);
|
||||
} else {
|
||||
printf("获取队头元素失败!\n");
|
||||
}
|
||||
if (DeQueue(Q, x)) {
|
||||
printf("出队成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出队失败了,再检出一下吧!\n");
|
||||
}
|
||||
if (QueueEmpty(Q)) {
|
||||
printf("队空啦\n");
|
||||
} else {
|
||||
printf("队非空\n");
|
||||
}
|
||||
|
||||
printf("测试结束!\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
TestLinkQueue();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user