mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-10-31 20:35:08 +08:00
更新队列
This commit is contained in:
@@ -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