mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-18 01:19:14 +08:00
更新代码
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
11
Code/Code/head/link_queue.h
Normal file
11
Code/Code/head/link_queue.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 顺序队列
|
||||
typedef struct {
|
||||
// 数据
|
||||
element_type* data;
|
||||
// 队头指针和队尾指针
|
||||
int front, rear;
|
||||
};
|
||||
29
Code/Code/head/link_stack.h
Normal file
29
Code/Code/head/link_stack.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#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;
|
||||
};
|
||||
@@ -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<class List>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
60
Code/Code/head/sequence_queue.h
Normal file
60
Code/Code/head/sequence_queue.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#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;
|
||||
}
|
||||
|
||||
// 进队
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
|
||||
#### 顺序队列插入
|
||||
|
||||
如果出队,则前面的空间会空闲,但是假如队尾指针会依照插入而不断加1,则我们的队尾指针最后会指向最后一个区域,计算机不知道前面是怎么样,所以就认为空间已经满了,实际上没有。这就是假溢出。
|
||||
如果出队,则前面的空间会空闲,但是假如队尾指针会依照插入而不断加$1$,则我们的队尾指针最后会指向最后一个区域,计算机不知道前面是怎么样,所以就认为空间已经满了,实际上没有。这就是假溢出。
|
||||
|
||||
解决的方法就是使用模运算,将队尾指针不仅仅是加一,而是加一后再取整个静态数组大小MAXSIZE的模,这样如果队列尾指针超过了范围也仍能回到最开始插入数据。这时候物理结构虽然是线性的,而逻辑上已经变成了环型的了。
|
||||
解决的方法就是使用模运算,将队尾指针不仅仅是加一,而是加一后再取整个静态数组大小$MAXSIZE$的模,这样如果队列尾指针超过了范围也仍能回到最开始插入数据。这时候物理结构虽然是线性的,而逻辑上已经变成了环型的了。
|
||||
|
||||
所以与此同时,队列已满的条件也不再是队尾指针$=MAXSIZE$了,而是队尾指针的下一个指针是队头指针,这里最后一个存储单元是不能存储数据的,因为如果存储了数据那么头指针就等于尾指针,这在我们的定义中是空队列的意思,会混淆,所以必须牺牲一个存储单元。
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
|
||||
同理我们可以定义一个$int$类型的$tag$,当进行删除操作就置$tag$为$0$,插入操作时置$tag$为$1$,只有删除才可能队空,只有插入才可能队满,所以就可以根据这个来判断。
|
||||
|
||||
## 循环队列
|
||||
|
||||
## 链队
|
||||
|
||||
## 双端队列
|
||||
|
||||
Reference in New Issue
Block a user