1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-06-18 09:27:23 +08:00

更新队列

This commit is contained in:
Didnelpsun
2021-09-17 23:27:48 +08:00
parent 5ef6337176
commit e19b4bd5ae
22 changed files with 580 additions and 90 deletions

View File

@@ -1,5 +1,3 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 双链表结点

View File

@@ -1,5 +1,3 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 单链表结点

View File

@@ -1,11 +1,15 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 顺序队列
typedef struct {
// 链队结点
typedef struct LinkQueueNode {
// 数据
element_type* data;
// 指针
struct LinkQueueNode *next;
} LinkQueueNode;
// 链队
typedef struct {
// 队头指针和队尾指针
int front, rear;
};
LinkQueueNode *front, *rear;
} LinkQueue;

View File

@@ -1,5 +1,3 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
typedef struct LinkStackNode{

View File

@@ -1,5 +1,3 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 静态顺序表

View File

@@ -1,5 +1,3 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 顺序队列
@@ -48,13 +46,76 @@ SequenceQueue InitSequenceQueue(int max_size) {
}
// 判空
bool EmptySequenceQueue(SequenceQueue queue){
bool EmptySequenceQueue(SequenceQueue queue) {
return queue.front == queue.rear;
}
// 判满(存在假溢出)
bool FullSequenceQueue(SequenceQueue queue){
bool FullSequenceQueue(SequenceQueue queue) {
return queue.rear == queue.max_size;
}
// 进队
// 判循环队列满
bool FullCircularSequenceQueue(SequenceQueue queue) {
return (queue.rear + 1) % queue.max_size == queue.front;
}
// 进队
bool EnterSequenceQueue(SequenceQueue &queue, element_type elem) {
// 判断队满
if (FullSequenceQueue(queue)) {
printf("EnterSequenceQueue:队满无法进队!\n");
return false;
}
queue.data[queue.rear++] = elem;
return true;
}
// 进循环队
bool EnterCircularSequenceQueue(SequenceQueue &queue, element_type elem) {
// 判循环队满
if (FullCircularSequenceQueue(queue)) {
printf("EnterCircularSequenceQueue:队满无法进队!\n");
return false;
}
queue.data[queue.rear] = elem;
queue.rear = (queue.rear + 1) % queue.max_size;
return true;
}
// 出队
element_type DepartSequenceQueue(SequenceQueue &queue) {
// 判断队空
if (EmptySequenceQueue(queue)) {
printf("DepartSequenceQueue:队空无法出队!\n");
return DEFAULTELEM;
}
return queue.data[queue.front++];
}
// 出循环队
element_type DepartCircularDepartSequence(SequenceQueue &queue) {
// 判断队空
if (EmptySequenceQueue(queue)) {
printf("DepartCircularDepartSequence:队空无法出队!\n");
return DEFAULTELEM;
}
element_type temp = queue.data[queue.front];
queue.front = (queue.front + 1) % queue.max_size;
return temp;
}
// 获取队长
int LengthSequenceQueue(SequenceQueue queue) {
return (queue.rear - queue.front + queue.max_size) % queue.max_size;
}
// 读队头
element_type HeadSequenceQueue(SequenceQueue &queue) {
// 判断队空
if (EmptySequenceQueue(queue)) {
printf("HeadSequenceQueue:队空无法读队头!\n");
return DEFAULTELEM;
}
return queue.data[queue.front];
}

View File

@@ -1,5 +1,3 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 顺序栈

View File

@@ -0,0 +1,130 @@
#include "head.h"
typedef struct {
// 数据
element_type *data;
// 栈顶指针
int top_left, top_right;
// 最大容量
int max_size;
} ShareStack;
// 初始化
bool InitShareStack(ShareStack &stack) {
stack.data = (element_type *) malloc(sizeof(element_type) * MAXSIZE);
stack.top_left = -1;
stack.top_right = MAXSIZE;
stack.max_size = MAXSIZE;
return true;
}
bool InitShareStack(ShareStack &stack, int max_size) {
stack.data = (element_type *) malloc(sizeof(element_type) * max_size);
stack.top_left = -1;
stack.top_right = max_size;
stack.max_size = max_size;
return true;
}
ShareStack InitShareStack() {
auto *stack = (ShareStack *) malloc(sizeof(ShareStack));
stack->data = (element_type *) malloc(sizeof(element_type) * MAXSIZE);
stack->top_left = -1;
stack->top_right = MAXSIZE;
stack->max_size = MAXSIZE;
return (ShareStack &) stack;
}
ShareStack InitShareStack(int max_size) {
auto *stack = (ShareStack *) malloc(sizeof(ShareStack));
stack->data = (element_type *) malloc(sizeof(element_type) * max_size);
stack->top_left = -1;
stack->top_right = max_size;
stack->max_size = max_size;
return (ShareStack &) stack;
}
// 判左空
bool EmptyLeftShareStack(ShareStack stack){
return stack.top_left == -1;
}
// 判右空
bool EmptyRightShareStack(ShareStack stack){
return stack.top_right == stack.max_size;
}
// 判满
bool FullShareStack(ShareStack stack){
return stack.top_right - stack.top_left == 1;
}
// 左栈长
int LengthLeftShareStack(ShareStack stack){
return stack.top_left + 1;
}
// 右栈长
int LengthRightShareStack(ShareStack stack){
return stack.max_size - stack.top_right;
}
// 左进栈
bool PushLeftShareStack(ShareStack &stack, element_type elem){
if(FullShareStack(stack)){
printf("PushLeftShareStack:栈满无法进栈!\n");
return false;
}
stack.data[++stack.top_left] = elem;
return true;
}
// 右进栈
bool PushRightShareStack(ShareStack &stack, element_type elem){
if(FullShareStack(stack)){
printf("PushRightShareStack:栈满无法进栈!\n");
return false;
}
stack.data[--stack.top_right] = elem;
return true;
}
// 左出栈
element_type PopLeftShareStack(ShareStack &stack){
if(EmptyLeftShareStack(stack)){
printf("PopLeftShareStack:栈空无法出栈!\n");
return DEFAULTELEM;
}
element_type temp = stack.data[stack.top_left];
stack.top_left--;
return temp;
}
// 右出栈
element_type PopRightShareStack(ShareStack &stack){
if(EmptyLeftShareStack(stack)){
printf("PopRightShareStack:栈空无法出栈!\n");
return DEFAULTELEM;
}
element_type temp = stack.data[stack.top_right];
stack.top_left++;
return temp;
}
// 读取左首部
element_type TopLeftShareStack(ShareStack stack){
if(EmptyLeftShareStack(stack)){
printf("PopLeftShareStack:栈空无法出栈!\n");
return DEFAULTELEM;
}
return stack.data[stack.top_left];
}
// 读取右首部
element_type TopRightShareStack(ShareStack stack){
if(EmptyLeftShareStack(stack)){
printf("TopRightShareStack:栈空无法出栈!\n");
return DEFAULTELEM;
}
return stack.data[stack.top_right];
}

View File

@@ -1,5 +1,3 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 静态链表

View File

@@ -6,6 +6,7 @@
#include "../head/static_link_list.h"
#include "../head/sequence_stack.h"
#include "../head/link_stack.h"
#include "../head/share_stack.h"
#include "../head/sequence_queue.h"
#include "../head/link_queue.h"