首页 > 其他 > 详细

23 顺序队列

时间:2020-03-29 22:15:16      阅读:56      评论:0      收藏:0      [点我收藏+]

1,定义队列,并初始化队列(赋予一个队列10个元素,元素赋值为 -1 )

//顺序队列,定义队列并初始化队列,将队列中的元素初始化为-1

#include<stdio.h>
#include<stdlib.h>
#define Capacity 10

typedef struct Queue {
	int data[Capacity];
	int front; //队头指针
	int rear; //队尾指针
}queue;


queue initQueue(queue Q) {
	for (int i = 0; i < Capacity; i++) {
		Q.data[i] = -1;
	}
	return Q;
}

void showQueue(queue Q) {
	for (int i = Q.front; i < Capacity; i++) {
		printf("%d  ", Q.data[i]);
	}
	printf("\n");
}


void main() {
	struct Queue myqueue;
	myqueue.front = myqueue.rear = 0;
	myqueue = initQueue(myqueue);

	printf("初始化队列是:\n");
	showQueue(myqueue);
	printf("队头指针是:%d\n", myqueue.front);
	printf("队尾指针是:%d\n", myqueue.rear);
}

技术分享图片

2,入队列,从队尾进入一个元素

//入队列,从队尾进入一个元素

#include<stdio.h>
#include<stdlib.h>
#define Capacity 10

typedef struct Queue {
	int data[Capacity];
	int front; //队头指针
	int rear; //队尾指针
}queue;


queue initQueue(queue Q) {
	for (int i = 0; i < Capacity; i++) {
		Q.data[i] = -1;
	}
	return Q;
}

//入队列
queue push(queue Q, int elem) {
	Q.data[Q.rear] = elem;
	Q.rear++;
	return Q;
}

void showQueue(queue Q) {
	for (int i = Q.front; i < Capacity; i++) {
		printf("%d  ", Q.data[i]);
	}
	printf("\n");
}


void main() {
	struct Queue myqueue;
	myqueue.front = myqueue.rear = 0;
	myqueue = initQueue(myqueue);

	printf("初始化队列是:\n");
	showQueue(myqueue);
	printf("队头指针是:%d\n", myqueue.front);
	printf("队尾指针是:%d\n", myqueue.rear);

	printf("将10入队列后:\n");
	myqueue = push(myqueue, 10);
	showQueue(myqueue);
	printf("队头指针是:%d\n", myqueue.front);
	printf("队尾指针是:%d\n", myqueue.rear);

}

技术分享图片

 

 3,出队列

//出队列
#include<stdio.h>
#include<stdlib.h>
#define Capacity 10

typedef struct Queue {
	int data[Capacity];
	int front; //队头指针
	int rear; //队尾指针
}queue;


queue initQueue(queue Q) {
	for (int i = 0; i < Capacity; i++) {
		Q.data[i] = -1;
	}
	return Q;
}

//入队列
queue push(queue Q,int elem) {
	Q.data[Q.rear] = elem;
	Q.rear++;
	return Q;
}

//出队列
queue pop(queue Q) {
	while (Q.front != Q.rear) {
		printf("出队列元素是:%d\n", Q.data[Q.front]);
		Q.front++;
	}
	return Q;
}

void showQueue(queue Q) {
	for (int i = Q.front; i < Capacity; i++) {
		printf("%d  ", Q.data[i]);
	}
	printf("\n");
}


void main() {
	struct Queue myqueue;
	myqueue.front = myqueue.rear = 0;
	myqueue = initQueue(myqueue);

	printf("初始化队列是:\n");
	showQueue(myqueue);
	printf("队头指针是:%d\n", myqueue.front);
	printf("队尾指针是:%d\n", myqueue.rear);

	printf("将10,20,30,40,50入队列后:\n");
	myqueue = push(myqueue, 10);
	myqueue = push(myqueue, 20);
	myqueue = push(myqueue, 30);
	myqueue = push(myqueue, 40);
	myqueue = push(myqueue, 50);
	showQueue(myqueue);
	printf("队头指针是:%d\n", myqueue.front);
	printf("队尾指针是:%d\n", myqueue.rear);

	printf("全部出队列后:\n");
	myqueue=pop(myqueue);
	showQueue(myqueue);

}

技术分享图片

4,假溢出

//假溢出
#include<stdio.h>
#include<stdlib.h>
#define Capacity 5

typedef struct Queue {
	int data[Capacity];
	int front; //队头指针
	int rear; //队尾指针
}queue;


queue initQueue(queue Q) {
	for (int i = 0; i < Capacity; i++) {
		Q.data[i] = -1;
	}
	return Q;
}

//入队列
queue push(queue Q, int elem) {
	if (Q.rear == Capacity) { //顺序队列判 满 的条件
		printf("队列满了\n");
		return;
	}
	Q.data[Q.rear] = elem;
	Q.rear++;
	return Q;
}

//出队列
queue pop(queue Q) {
	printf("出队列元素是:%d\n", Q.data[Q.front]);
	Q.front++;
	return Q;
}

void showQueue(queue Q) {
	for (int i = Q.front; i < Capacity; i++) {
		printf("%d  ", Q.data[i]);
	}
	printf("\n");
}


void main() {
	struct Queue myqueue;
	myqueue.front = myqueue.rear = 0;
	myqueue = initQueue(myqueue);

	printf("初始化队列是:\n");
	showQueue(myqueue);
	printf("队头指针是:%d\n", myqueue.front);
	printf("队尾指针是:%d\n", myqueue.rear);

	printf("将10,20,30,40,50入队列后:\n");
	myqueue = push(myqueue, 10);
	myqueue = push(myqueue, 20);
	myqueue = push(myqueue, 30);
	myqueue = push(myqueue, 40);
	myqueue = push(myqueue, 50);
	showQueue(myqueue);
	printf("队头指针是:%d\n", myqueue.front);
	printf("队尾指针是:%d\n", myqueue.rear);

	printf("队头元素出队列后:\n");
	myqueue = pop(myqueue);
	showQueue(myqueue);
	printf("队头指针是:%d\n", myqueue.front);
	printf("队尾指针是:%d\n", myqueue.rear);

	printf("将100入队列:\n");
	myqueue = push(myqueue, 100);
	showQueue(myqueue);
}

 

技术分享图片

假溢出:Q.front ≠ 0 , Q.rear == Capacity ; 

顺序队列 判空: Q.front == Q.rear = 0 ; 

顺序队列 判满: Q.front == Q.rear = Capacity; 

 

23 顺序队列

原文:https://www.cnblogs.com/shanlu0000/p/12595106.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!