diff --git a/Code/CPP-Code/head/link_stack.h b/Code/CPP-Code/head/link_stack.h index bc53bb4..840cf21 100644 --- a/Code/CPP-Code/head/link_stack.h +++ b/Code/CPP-Code/head/link_stack.h @@ -16,7 +16,7 @@ public: bool SetData(element_type data); // 取数据 - element_type GetData(); + element_type GetData() const; // 设置指针 bool SetNext(LinkStackNode* next); @@ -40,7 +40,7 @@ bool LinkStackNode::SetData(element_type data) { return true; } -element_type LinkStackNode::GetData() { +element_type LinkStackNode::GetData() const { return this->_data; } diff --git a/Code/Code/head/link_list.h b/Code/Code/head/link_list.h index 98ba927..6fe227d 100644 --- a/Code/Code/head/link_list.h +++ b/Code/Code/head/link_list.h @@ -16,6 +16,12 @@ LinkList InitLinkList() { return list; } +bool InitLinkList(LinkList &list) { + list->data = DEFAULTELEM; + list->next = nullptr; + return true; +} + // п bool EmptyLinkList(LinkList list) { return list->next == nullptr && list->data == DEFAULTELEM; @@ -43,7 +49,8 @@ bool PrintLinkList(LinkList list) { } // ǰָ LinkListNode *node = list; - while (!TypeLinkList(list) && node != nullptr && node->data != DEFAULTELEM || TypeLinkList(list) && node != nullptr) { + while (!TypeLinkList(list) && node != nullptr && node->data != DEFAULTELEM || + TypeLinkList(list) && node != nullptr) { printf("%dԪֵΪ%c\n", i, node->data); node = node->next; i++; @@ -79,7 +86,7 @@ bool InsertLinkListWithHead(LinkList &list, int index, element_type elem) { return false; } // ʱi==index-1 - auto *s = new LinkListNode(); + auto *s = (LinkListNode *) malloc(sizeof(LinkListNode)); s->data = elem; // pԭĺ̸µĽ s->next = p->next; @@ -97,7 +104,7 @@ bool InsertLinkListWithoutHead(LinkList &list, int index, element_type elem) { printf("InsertLinkListWithoutHead:ֵ%dС\n", index); return false; } - auto *s = new LinkListNode(); + auto *s = (LinkListNode *) malloc(sizeof(LinkListNode)); if (index == 0) { s->data = elem; // sĺΪlistָ @@ -305,7 +312,7 @@ element_type GetLinkListElem(LinkList list, int index) { } // ֵ -int LocateLinkList(LinkList list, element_type elem){ +int LocateLinkList(LinkList list, element_type elem) { LinkListNode *node = list; for (int i = 0; i < GetLengthLinkList(list); i++) { if (node->data == elem) { @@ -319,7 +326,7 @@ int LocateLinkList(LinkList list, element_type elem){ // bool DestroyLinkList(LinkList &list) { list->data = DEFAULTELEM; - delete(list); + delete (list); return true; } diff --git a/Code/Code/head/link_queue.h b/Code/Code/head/link_queue.h new file mode 100644 index 0000000..d33df1e --- /dev/null +++ b/Code/Code/head/link_queue.h @@ -0,0 +1,11 @@ +#include +#include +#include "head.h" + +// 顺序队列 +typedef struct { + // 数据 + element_type* data; + // 队头指针和队尾指针 + int front, rear; +}; \ No newline at end of file diff --git a/Code/Code/head/link_stack.h b/Code/Code/head/link_stack.h new file mode 100644 index 0000000..5b40df9 --- /dev/null +++ b/Code/Code/head/link_stack.h @@ -0,0 +1,29 @@ +#include +#include +#include "head.h" + +typedef struct LinkStackNode{ + // 数据 + element_type data; + // 指针 + struct LinkStackNode *next; +} *LinkStack; + +// 初始化 +LinkStack InitLinkStack(){ + auto stack = (LinkStack) malloc(sizeof(LinkStack)); + stack->data = DEFAULTELEM; + stack->next = nullptr; + return stack; +} + +bool InitLinkStack(LinkStack &stack){ + stack->data = DEFAULTELEM; + stack->next = nullptr; + return true; +} + +// 判空 +bool EmptyLinkStack(LinkStack stack){ + return stack->data == DEFAULTELEM; +}; \ No newline at end of file diff --git a/Code/Code/head/sequence_list.h b/Code/Code/head/sequence_list.h index ee0fe58..223a8ef 100644 --- a/Code/Code/head/sequence_list.h +++ b/Code/Code/head/sequence_list.h @@ -4,7 +4,7 @@ // ̬˳ typedef struct { - element_type data[MAXSIZE]; + element_type *data; int length; } StaticSequenceList; @@ -16,17 +16,61 @@ typedef struct { // ʼ bool InitSequenceList(StaticSequenceList &list) { + list.data = (element_type *) malloc(sizeof(element_type) * MAXSIZE); list.length = 0; return true; } +bool InitSequenceList(StaticSequenceList &list, int max_size) { + list.data = (element_type *) malloc(sizeof(element_type) * max_size); + list.length = 0; + return true; +} + +StaticSequenceList InitStaticSequenceList() { + auto *list = (StaticSequenceList *) malloc(sizeof(StaticSequenceList)); + list->data = (element_type *) malloc(sizeof(element_type) * MAXSIZE); + list->length = 0; + return (StaticSequenceList &) list; +} + +StaticSequenceList InitStaticSequenceList(int max_size) { + auto *list = (StaticSequenceList *) malloc(sizeof(StaticSequenceList)); + list->data = (element_type *) malloc(sizeof(element_type) * max_size); + list->length = 0; + return (StaticSequenceList &) list; +} + bool InitSequenceList(DynamicSequenceList &list) { list.length = 0; - list.data = new element_type[MAXSIZE]; + list.data = (element_type *) malloc(sizeof(element_type) * MAXSIZE); list.max_size = MAXSIZE; return true; } +bool InitSequenceList(DynamicSequenceList &list, int max_size) { + list.length = 0; + list.data = (element_type *) malloc(sizeof(element_type) * max_size); + list.max_size = max_size; + return true; +} + +DynamicSequenceList InitDynamicSequenceList() { + auto *list = (DynamicSequenceList *) malloc(sizeof(DynamicSequenceList)); + list->data = (element_type *) malloc(sizeof(element_type) * MAXSIZE); + list->length = 0; + list->max_size = MAXSIZE; + return (DynamicSequenceList &) list; +} + +DynamicSequenceList InitDynamicSequenceList(int max_size) { + auto *list = (DynamicSequenceList *) malloc(sizeof(DynamicSequenceList)); + list->data = (element_type *) malloc(sizeof(element_type) * max_size); + list->length = 0; + list->max_size = MAXSIZE; + return (DynamicSequenceList &) list; +} + // ӡ template bool PrintSequenceList(List list) { @@ -170,7 +214,7 @@ int LocateSequenceList(List list, element_type elem) { // ٶ̬˳ int DestroyDynamicSequenceList(DynamicSequenceList &list) { - delete(list.data); + delete (list.data); return 0; } diff --git a/Code/Code/head/sequence_queue.h b/Code/Code/head/sequence_queue.h new file mode 100644 index 0000000..ec717c7 --- /dev/null +++ b/Code/Code/head/sequence_queue.h @@ -0,0 +1,60 @@ +#include +#include +#include "head.h" + +// 顺序队列 +typedef struct { + // 数据 + element_type *data; + // 队头、队尾 + int front, rear; + // 队列最大容量 + int max_size; +} SequenceQueue; + +// 初始化 +bool InitSequenceQueue(SequenceQueue &queue) { + queue.data = (element_type *) malloc(sizeof(element_type) * MAXSIZE); + queue.front = 0; + queue.rear = 0; + queue.max_size = MAXSIZE; + return true; +} + +bool InitSequenceQueue(SequenceQueue &queue, int max_size) { + queue.data = (element_type *) malloc(sizeof(element_type) * max_size); + queue.front = 0; + queue.rear = 0; + queue.max_size = max_size; + return true; +} + +SequenceQueue InitSequenceQueue() { + auto *queue = (SequenceQueue *) malloc(sizeof(SequenceQueue)); + queue->data = (element_type *) malloc(sizeof(element_type) * MAXSIZE); + queue->front = 0; + queue->rear = 0; + queue->max_size = MAXSIZE; + return (SequenceQueue &) queue; +} + +SequenceQueue InitSequenceQueue(int max_size) { + auto *queue = (SequenceQueue *) malloc(sizeof(SequenceQueue)); + queue->data = (element_type *) malloc(sizeof(element_type) * max_size); + queue->front = 0; + queue->rear = 0; + queue->max_size = max_size; + return (SequenceQueue &) queue; +} + +// 判空 +bool EmptySequenceQueue(SequenceQueue queue){ + return queue.front == queue.rear; +} + +// 判满(存在假溢出) +bool FullSequenceQueue(SequenceQueue queue){ + return queue.rear == queue.max_size; +} + +// 进队 \ No newline at end of file diff --git a/Code/Code/head/sequence_stack.h b/Code/Code/head/sequence_stack.h index 9464e23..2a6ea6d 100644 --- a/Code/Code/head/sequence_stack.h +++ b/Code/Code/head/sequence_stack.h @@ -5,7 +5,7 @@ // ˳ջ typedef struct { // ջԪ - element_type data[MAXSIZE]; + element_type *data; // ջָ int top; // @@ -14,11 +14,35 @@ typedef struct { // ʼ bool InitSequenceStack(SequenceStack &stack) { + stack.data = (element_type *) malloc(sizeof(element_type) * MAXSIZE); stack.top = -1; stack.max_size = MAXSIZE; return true; } +bool InitSequenceStack(SequenceStack &stack, int max_size) { + stack.data = (element_type *) malloc(sizeof(element_type) * max_size); + stack.top = -1; + stack.max_size = max_size; + return true; +} + +SequenceStack InitSequenceStack(){ + auto* stack = (SequenceStack*) malloc(sizeof(SequenceStack)); + stack->data = (element_type *) malloc(sizeof(element_type) * MAXSIZE); + stack->top = -1; + stack->max_size = MAXSIZE; + return (SequenceStack &) stack; +} + +SequenceStack InitSequenceStack(int max_size){ + auto* stack = (SequenceStack*) malloc(sizeof(SequenceStack)); + stack->data = (element_type *) malloc(sizeof(element_type) * max_size); + stack->top = -1; + stack->max_size = max_size; + return (SequenceStack &) stack; +} + // п bool EmptySequenceStack(SequenceStack stack) { return stack.top == -1; diff --git a/Code/Code/source/test.cpp b/Code/Code/source/test.cpp index 540f5d9..28eefae 100644 --- a/Code/Code/source/test.cpp +++ b/Code/Code/source/test.cpp @@ -5,6 +5,9 @@ #include "../head/double_link_list.h" #include "../head/static_link_list.h" #include "../head/sequence_stack.h" +#include "../head/link_stack.h" +#include "../head/sequence_queue.h" +#include "../head/link_queue.h" using namespace std; diff --git a/Data-Structrue/3-queue.md b/Data-Structrue/3-queue.md index 45c819f..63a797a 100644 --- a/Data-Structrue/3-queue.md +++ b/Data-Structrue/3-queue.md @@ -12,9 +12,9 @@ #### 顺序队列插入 -如果出队,则前面的空间会空闲,但是假如队尾指针会依照插入而不断加1,则我们的队尾指针最后会指向最后一个区域,计算机不知道前面是怎么样,所以就认为空间已经满了,实际上没有。这就是假溢出。 +如果出队,则前面的空间会空闲,但是假如队尾指针会依照插入而不断加$1$,则我们的队尾指针最后会指向最后一个区域,计算机不知道前面是怎么样,所以就认为空间已经满了,实际上没有。这就是假溢出。 -解决的方法就是使用模运算,将队尾指针不仅仅是加一,而是加一后再取整个静态数组大小MAXSIZE的模,这样如果队列尾指针超过了范围也仍能回到最开始插入数据。这时候物理结构虽然是线性的,而逻辑上已经变成了环型的了。 +解决的方法就是使用模运算,将队尾指针不仅仅是加一,而是加一后再取整个静态数组大小$MAXSIZE$的模,这样如果队列尾指针超过了范围也仍能回到最开始插入数据。这时候物理结构虽然是线性的,而逻辑上已经变成了环型的了。 所以与此同时,队列已满的条件也不再是队尾指针$=MAXSIZE$了,而是队尾指针的下一个指针是队头指针,这里最后一个存储单元是不能存储数据的,因为如果存储了数据那么头指针就等于尾指针,这在我们的定义中是空队列的意思,会混淆,所以必须牺牲一个存储单元。 @@ -28,6 +28,8 @@ 同理我们可以定义一个$int$类型的$tag$,当进行删除操作就置$tag$为$0$,插入操作时置$tag$为$1$,只有删除才可能队空,只有插入才可能队满,所以就可以根据这个来判断。 +## 循环队列 + ## 链队 ## 双端队列