23 顺序队列

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; 

 

posted @ 2020-03-29 21:38  shanlu  阅读(136)  评论(0编辑  收藏  举报