mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-18 01:19:14 +08:00
更新队列
This commit is contained in:
@@ -1,4 +1,166 @@
|
||||
#include <iostream>
|
||||
#include "head.h"
|
||||
|
||||
// 链队结点
|
||||
class LinkQueueNode {
|
||||
private:
|
||||
// 数据
|
||||
element_type _data;
|
||||
// 指针
|
||||
LinkQueueNode *_next;
|
||||
public:
|
||||
// 设置数据
|
||||
bool SetData(element_type elem);
|
||||
|
||||
// 获取数据
|
||||
element_type GetData() const;
|
||||
|
||||
// 设置指针
|
||||
bool SetNext(LinkQueueNode *next);
|
||||
|
||||
// 获取指针
|
||||
LinkQueueNode *GetNext();
|
||||
|
||||
// 构造函数
|
||||
LinkQueueNode() {
|
||||
this->SetData(DEFAULTELEM);
|
||||
this->SetNext(nullptr);
|
||||
}
|
||||
|
||||
LinkQueueNode(element_type elem) {
|
||||
this->SetData(elem);
|
||||
this->SetNext(nullptr);
|
||||
}
|
||||
|
||||
LinkQueueNode(element_type elem, LinkQueueNode *next) {
|
||||
this->SetData(elem);
|
||||
this->SetNext(next);
|
||||
}
|
||||
};
|
||||
|
||||
bool LinkQueueNode::SetData(element_type elem) {
|
||||
this->_data = elem;
|
||||
return true;
|
||||
}
|
||||
|
||||
element_type LinkQueueNode::GetData() const {
|
||||
return this->_data;
|
||||
}
|
||||
|
||||
bool LinkQueueNode::SetNext(LinkQueueNode *next) {
|
||||
this->_next = next;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkQueueNode *LinkQueueNode::GetNext() {
|
||||
return this->_next;
|
||||
}
|
||||
|
||||
// 链队
|
||||
class LinkQueue {
|
||||
private:
|
||||
// 队头指针和队尾指针
|
||||
LinkQueueNode *_front{}, *_rear{};
|
||||
// 长度
|
||||
int _length{};
|
||||
public:
|
||||
// 设置队首指针
|
||||
bool SetFront(LinkQueueNode *front);
|
||||
|
||||
// 获取对首指针
|
||||
LinkQueueNode *GetFront();
|
||||
|
||||
// 设置队尾指针
|
||||
bool SetRear(LinkQueueNode *rear);
|
||||
|
||||
// 获取队尾指针
|
||||
LinkQueueNode *GetRear();
|
||||
|
||||
// 队长自加
|
||||
bool SetLength();
|
||||
|
||||
// 设置队长
|
||||
bool SetLength(int length);
|
||||
|
||||
// 获取队长
|
||||
int GetLength() const;
|
||||
|
||||
// 构造函数
|
||||
LinkQueue();
|
||||
|
||||
// 判空
|
||||
bool Empty() const;
|
||||
|
||||
// 入队
|
||||
bool Enter(element_type elem);
|
||||
|
||||
// 出队
|
||||
element_type Depart();
|
||||
};
|
||||
|
||||
bool LinkQueue::SetFront(LinkQueueNode *front) {
|
||||
this->_front = front;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkQueueNode *LinkQueue::GetFront() {
|
||||
return this->_front;
|
||||
}
|
||||
|
||||
bool LinkQueue::SetRear(LinkQueueNode *rear) {
|
||||
this->_rear = rear;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkQueueNode *LinkQueue::GetRear() {
|
||||
return this->_rear;
|
||||
}
|
||||
|
||||
bool LinkQueue::SetLength() {
|
||||
this->_length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkQueue::SetLength(int length) {
|
||||
this->_length = length;
|
||||
return true;
|
||||
}
|
||||
|
||||
int LinkQueue::GetLength() const {
|
||||
return this->_length;
|
||||
}
|
||||
|
||||
LinkQueue::LinkQueue() {
|
||||
auto *node = new LinkQueueNode();
|
||||
this->SetFront(node);
|
||||
this->SetRear(node);
|
||||
this->SetLength(0);
|
||||
}
|
||||
|
||||
bool LinkQueue::Empty() const {
|
||||
return this->GetLength() == 0;
|
||||
}
|
||||
|
||||
bool LinkQueue::Enter(element_type elem) {
|
||||
// 创建新结点
|
||||
auto *node = new LinkQueueNode(elem);
|
||||
// 把最后一个元素的next连接到node
|
||||
this->GetRear()->SetNext(node);
|
||||
// 移动尾指针
|
||||
this->SetRear(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
element_type LinkQueue::Depart() {
|
||||
if(this->Empty()){
|
||||
cout << "Depart:The queue is empty!" << endl;
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
// 获取对首元素下一个元素的数据
|
||||
element_type elem = this->GetFront()->GetNext()->GetData();
|
||||
// 后移移位
|
||||
this->GetFront()->SetNext(this->GetFront()->GetNext()->GetNext());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
using namespace std;
|
||||
|
||||
// 链栈
|
||||
class LinkStackNode{
|
||||
class LinkStackNode {
|
||||
private:
|
||||
// 数据
|
||||
element_type _data{};
|
||||
// 指针
|
||||
LinkStackNode* _next{};
|
||||
public:
|
||||
LinkStackNode *_next{};
|
||||
|
||||
// 设置数据
|
||||
bool SetData(element_type data);
|
||||
|
||||
@@ -17,17 +17,19 @@ public:
|
||||
element_type GetData() const;
|
||||
|
||||
// 设置指针
|
||||
bool SetNext(LinkStackNode* next);
|
||||
bool SetNext(LinkStackNode *next);
|
||||
|
||||
// 获取指针
|
||||
LinkStackNode* GetNext();
|
||||
LinkStackNode *GetNext();
|
||||
|
||||
public:
|
||||
|
||||
// 构造函数
|
||||
LinkStackNode();
|
||||
|
||||
explicit LinkStackNode(element_type data);
|
||||
|
||||
LinkStackNode(element_type data, LinkStackNode* next);
|
||||
LinkStackNode(element_type data, LinkStackNode *next);
|
||||
|
||||
// 销毁
|
||||
bool Destroy();
|
||||
@@ -68,7 +70,7 @@ LinkStackNode::LinkStackNode(element_type data, LinkStackNode *next) {
|
||||
|
||||
bool LinkStackNode::Destroy() {
|
||||
this->SetData(DEFAULTELEM);
|
||||
delete(this->GetNext());
|
||||
delete (this->GetNext());
|
||||
this->SetNext(nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ private:
|
||||
int _top{};
|
||||
// 最大容量
|
||||
int _max_size{};
|
||||
public:
|
||||
|
||||
// 设置数据
|
||||
bool SetData(element_type *data);
|
||||
|
||||
@@ -27,6 +27,8 @@ public:
|
||||
// 设置最大容量
|
||||
bool SetMaxSize(int max_size);
|
||||
|
||||
public:
|
||||
|
||||
// 获取最大容量
|
||||
int GetMaxSize() const;
|
||||
|
||||
|
||||
@@ -183,10 +183,10 @@ bool PriorInsertLinkList(LinkList &list, element_type *elem, int start, int leng
|
||||
|
||||
// 删除
|
||||
element_type *DeleteLinkListWithHead(LinkList &list, int index, int length) {
|
||||
auto *data = (element_type *) malloc(length * sizeof(element_type));
|
||||
auto *elem = (element_type *) malloc(length * sizeof(element_type));
|
||||
if (index < 1) {
|
||||
printf("DeleteLinkListWithHead:删除索引值%d过小!\n", index);
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
if (length < 1) {
|
||||
printf("DeleteLinkListWithHead:删除长度%d过小!\n", length);
|
||||
@@ -202,7 +202,7 @@ element_type *DeleteLinkListWithHead(LinkList &list, int index, int length) {
|
||||
// 如果链表没有任何数据
|
||||
if (start == nullptr) {
|
||||
printf("DeleteLinkListWithHead:链表为空!\n");
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
// 循环遍历到达指定索引号的单链表的结点
|
||||
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
@@ -213,16 +213,16 @@ element_type *DeleteLinkListWithHead(LinkList &list, int index, int length) {
|
||||
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
if (i < index - 1) {
|
||||
printf("DeleteLinkListWithHead:删除索引值%d过大!\n", index);
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
// 此时i==index-1,start到达,求end
|
||||
end = start;
|
||||
for (int i = 0; i < length; i++) {
|
||||
data[i] = end->data;
|
||||
elem[i] = end->data;
|
||||
end = end->next;
|
||||
if (end == nullptr) {
|
||||
printf("DeleteLinkListWithHead:删除索引最大值%d大于链表最大索引%d!\n", index + length - 1, length - 1);
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
}
|
||||
if (index == 1) {
|
||||
@@ -230,14 +230,14 @@ element_type *DeleteLinkListWithHead(LinkList &list, int index, int length) {
|
||||
} else {
|
||||
start->next = end->next;
|
||||
}
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
|
||||
element_type *DeleteLinkListWithoutHead(LinkList &list, int index, int length) {
|
||||
auto *data = (element_type *) malloc(length * sizeof(element_type));
|
||||
auto *elem = (element_type *) malloc(length * sizeof(element_type));
|
||||
if (index < 0) {
|
||||
printf("DeleteLinkListWithoutHead:删除索引值过小!\n");
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
if (length < 1) {
|
||||
printf("DeleteLinkListWithoutHead:删除长度%d过小!\n", length);
|
||||
@@ -253,7 +253,7 @@ element_type *DeleteLinkListWithoutHead(LinkList &list, int index, int length) {
|
||||
// 如果链表没有任何数据
|
||||
if (EmptyLinkList(list)) {
|
||||
printf("DeleteLinkListWithoutHead:链表为空!\n");
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
// 循环遍历到达指定索引号的单链表的结点
|
||||
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
@@ -264,16 +264,16 @@ element_type *DeleteLinkListWithoutHead(LinkList &list, int index, int length) {
|
||||
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
if (i < index - 1) {
|
||||
printf("DeleteLinkListWithoutHead:删除索引值%d过大!\n", index);
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
// 到达位置
|
||||
end = start;
|
||||
for (int i = 0; i < length; i++) {
|
||||
end = end->next;
|
||||
data[i] = end->data;
|
||||
elem[i] = end->data;
|
||||
if (end->next == nullptr) {
|
||||
printf("DeleteLinkListWithoutHead:删除索引最大值%d大于链表最大索引%d!\n", index + length - 1, length - 1);
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
}
|
||||
// 如果删除第一个第0号结点
|
||||
@@ -282,7 +282,7 @@ element_type *DeleteLinkListWithoutHead(LinkList &list, int index, int length) {
|
||||
start->next = end->next->next;
|
||||
}
|
||||
start->next = end->next;
|
||||
return data;
|
||||
return elem;
|
||||
}
|
||||
|
||||
// 求表长
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// 链队结点
|
||||
typedef struct LinkQueueNode {
|
||||
// 数据
|
||||
element_type* data;
|
||||
element_type data;
|
||||
// 指针
|
||||
struct LinkQueueNode *next;
|
||||
} LinkQueueNode;
|
||||
@@ -12,4 +12,55 @@ typedef struct LinkQueueNode {
|
||||
typedef struct {
|
||||
// 队头指针和队尾指针
|
||||
LinkQueueNode *front, *rear;
|
||||
} LinkQueue;
|
||||
} LinkQueue;
|
||||
|
||||
// 初始化
|
||||
bool InitLinkQueue(LinkQueue &queue){
|
||||
// 建立头节点
|
||||
queue.front = queue.rear = (LinkQueueNode*) malloc(sizeof(LinkQueueNode));
|
||||
// 初始为空
|
||||
queue.front->next = nullptr;
|
||||
queue.front->data = DEFAULTELEM;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkQueue InitLinkQueue(){
|
||||
auto* queue = (LinkQueue*) malloc(sizeof(LinkQueue));
|
||||
queue->front = queue->rear = (LinkQueueNode*) malloc(sizeof(LinkQueueNode));
|
||||
// 初始为空
|
||||
queue->front->next = nullptr;
|
||||
queue->front->data = DEFAULTELEM;
|
||||
return (LinkQueue &) queue;
|
||||
}
|
||||
|
||||
// 判空
|
||||
bool EmptyLinkQueue(LinkQueue queue){
|
||||
return queue.front==queue.rear;
|
||||
}
|
||||
|
||||
// 入队
|
||||
bool EnterLinkQueue(LinkQueue &queue, element_type elem){
|
||||
// 创建新结点
|
||||
auto *node = (LinkQueueNode *) malloc(sizeof(LinkQueueNode));
|
||||
node->data = elem;
|
||||
node->next = nullptr;
|
||||
// 把最后一个元素的next连接到node
|
||||
queue.rear->next = node;
|
||||
// 移动尾指针
|
||||
queue.rear = node;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 出队
|
||||
element_type DepartLinkQueue(LinkQueue &queue){
|
||||
if(EmptyLinkQueue(queue)){
|
||||
printf("DepartLinkQueue:The queue is empty!");
|
||||
return false;
|
||||
}
|
||||
// 获取对首元素下一个元素的数据
|
||||
element_type elem = queue.front->next->data;
|
||||
// 后移移位
|
||||
queue.front->next=queue.front->next->next;
|
||||
// 若队列空
|
||||
return true;
|
||||
}
|
||||
@@ -158,26 +158,27 @@ bool LoopInsertSequenceList(List &list, element_type *elem, int start, int end)
|
||||
|
||||
// 删除
|
||||
template<class List>
|
||||
bool DeleteSequenceList(List &list, int index, element_type &elem) {
|
||||
element_type DeleteSequenceList(List &list, int index) {
|
||||
if (index >= list.length || index < 0) {
|
||||
printf("DeleteStaticSequenceList:删除索引超过索引范围!\n");
|
||||
return false;
|
||||
}
|
||||
elem = list.data[index];
|
||||
element_type elem = list.data[index];
|
||||
for (int i = index; i < list.length; i++) {
|
||||
list.data[i] = list.data[i + 1];
|
||||
}
|
||||
list.length--;
|
||||
return true;
|
||||
return elem;
|
||||
}
|
||||
|
||||
// 删除多个元素
|
||||
template<class List>
|
||||
int MultiDeleteSequenceList(List &list, int index, int length, element_type *elem) {
|
||||
element_type* MultiDeleteSequenceList(List &list, int index, int length) {
|
||||
if (index + length >= list.length || index < 0) {
|
||||
printf("MultiDeleteSequenceList:删除索引超过索引范围!\n");
|
||||
return 1;
|
||||
return nullptr;
|
||||
}
|
||||
auto elem = new element_type[length];
|
||||
for (int i = index; i < list.length - length; i++) {
|
||||
if (i < index + length) {
|
||||
elem[i - index] = list.data[i];
|
||||
@@ -185,7 +186,7 @@ int MultiDeleteSequenceList(List &list, int index, int length, element_type *ele
|
||||
list.data[i] = list.data[i + length];
|
||||
}
|
||||
list.length -= length;
|
||||
return 0;
|
||||
return elem;
|
||||
}
|
||||
|
||||
// 按位查找顺序表元素
|
||||
|
||||
@@ -100,9 +100,9 @@ element_type DepartCircularDepartSequence(SequenceQueue &queue) {
|
||||
printf("DepartCircularDepartSequence:队空无法出队!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
element_type temp = queue.data[queue.front];
|
||||
element_type elem = queue.data[queue.front];
|
||||
queue.front = (queue.front + 1) % queue.max_size;
|
||||
return temp;
|
||||
return elem;
|
||||
}
|
||||
|
||||
// 获取队长
|
||||
|
||||
@@ -95,9 +95,9 @@ element_type PopLeftShareStack(ShareStack &stack){
|
||||
printf("PopLeftShareStack:栈空无法出栈!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
element_type temp = stack.data[stack.top_left];
|
||||
element_type elem = stack.data[stack.top_left];
|
||||
stack.top_left--;
|
||||
return temp;
|
||||
return elem;
|
||||
}
|
||||
|
||||
// 右出栈
|
||||
@@ -106,9 +106,9 @@ element_type PopRightShareStack(ShareStack &stack){
|
||||
printf("PopRightShareStack:栈空无法出栈!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
element_type temp = stack.data[stack.top_right];
|
||||
element_type elem = stack.data[stack.top_right];
|
||||
stack.top_left++;
|
||||
return temp;
|
||||
return elem;
|
||||
}
|
||||
|
||||
// 读取左首部
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// 测试文件
|
||||
// ???????
|
||||
#include <iostream>
|
||||
#include "../head/sequence_list.h"
|
||||
#include "../head/link_list.h"
|
||||
@@ -21,8 +21,7 @@ bool SequenceListTest() {
|
||||
PrintSequenceList(list);
|
||||
printf("\n");
|
||||
int len = 2;
|
||||
element_type elem[2];
|
||||
MultiDeleteSequenceList(list, 0, len, elem);
|
||||
element_type* elem = MultiDeleteSequenceList(list, 0, len);
|
||||
PrintSequenceList(list);
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf("%c\n", elem[i]);
|
||||
@@ -45,9 +44,9 @@ bool LinkListTest() {
|
||||
element_type* data = DeleteLinkListWithoutHead(list, 2, length);
|
||||
PrintLinkList(list);
|
||||
for (int i = 0; i < length; i++) {
|
||||
printf("第%d为%c\n", i, data[i]);
|
||||
printf("??%d?%c\n", i, data[i]);
|
||||
}
|
||||
printf("长度为%d", GetLengthLinkList(list));
|
||||
printf("%c地址%d", '3', LocateLinkList(list, '3'));
|
||||
printf("?????%d", GetLengthLinkList(list));
|
||||
printf("%c???%d", '3', LocateLinkList(list, '3'));
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user