mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-16 06:56:54 +08:00
更新进程管理与操作数
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
// 初始化最大长度
|
||||
#define MAXSIZE 5
|
||||
// 定义默认值
|
||||
#define DEFAULTELEM '0'
|
||||
#define DEFAULTDATA '0'
|
||||
// 定义最大值
|
||||
#define INFINITY 32767
|
||||
//#define INFINITY 32767
|
||||
// 定义默认数据类型
|
||||
typedef char element_type;
|
||||
|
||||
@@ -3,159 +3,162 @@
|
||||
#include "head.h"
|
||||
|
||||
// 单链表结点
|
||||
typedef struct LinkListNode {
|
||||
class LinkListNode {
|
||||
public:
|
||||
// 数据
|
||||
element_type data;
|
||||
struct LinkListNode* next;
|
||||
} LinkListNode, *LinkList;
|
||||
// 指针
|
||||
LinkListNode* next;
|
||||
// 构造函数
|
||||
LinkListNode();
|
||||
explicit LinkListNode(element_type data);
|
||||
};
|
||||
|
||||
// 由于C语言无法参数赋值,所以必须借助一个中间变量完成
|
||||
// 初始化有头节点单链表
|
||||
int InitLinkListWithHead(LinkList &list) {
|
||||
list = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (list) {
|
||||
list->data = NULL;
|
||||
list->next = nullptr;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InitLinkListWithHead:分配空间失败!");
|
||||
return 1;
|
||||
}
|
||||
class LinkList{
|
||||
public:
|
||||
// 指针
|
||||
LinkListNode* next;
|
||||
// 链表长度
|
||||
int length;
|
||||
// 构造函数
|
||||
LinkList();
|
||||
// 判空
|
||||
bool Empty();
|
||||
};
|
||||
|
||||
class LinkListWithHead: public LinkList{
|
||||
public:
|
||||
// 构造函数
|
||||
LinkListWithHead();
|
||||
};
|
||||
|
||||
class LinkListWithoutHead: public LinkList{
|
||||
public:
|
||||
// 数据
|
||||
element_type data;
|
||||
// 构造函数
|
||||
LinkListWithoutHead();
|
||||
};
|
||||
|
||||
LinkListNode::LinkListNode() {
|
||||
this->data = NULL;
|
||||
this->next = nullptr;
|
||||
}
|
||||
|
||||
// 初始化无头节点单链表
|
||||
int InitLinkListWithoutHead(LinkList &list) {
|
||||
list = nullptr;
|
||||
return 0;
|
||||
LinkListNode::LinkListNode(element_type data) {
|
||||
this->data = data;
|
||||
this->next = nullptr;
|
||||
}
|
||||
|
||||
// 创建有头节点单链表
|
||||
LinkList CreateLinkListWithHead() {
|
||||
auto list = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (list) {
|
||||
list->data = NULL;
|
||||
list->next = nullptr;
|
||||
}
|
||||
else {
|
||||
printf("CreateLinkListWithHead:分配空间失败!");
|
||||
}
|
||||
return list;
|
||||
LinkList::LinkList() {
|
||||
this->next= nullptr;
|
||||
this->length =0;
|
||||
}
|
||||
|
||||
// 创建无头节点单链表
|
||||
LinkList CreateLinkListWithoutHead() {
|
||||
return nullptr;
|
||||
LinkListWithHead::LinkListWithHead() = default;
|
||||
|
||||
LinkListWithoutHead::LinkListWithoutHead() {
|
||||
this->data = NULL;
|
||||
}
|
||||
|
||||
bool LinkList::Empty() {
|
||||
if(this->length==0){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 判断有头节点单链表是否为空
|
||||
int EmptyLinkListWithHead(LinkList list) {
|
||||
if (list->next) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断无头节点单链表是否为空
|
||||
int EmptyLinkListWithoutHead(LinkList list) {
|
||||
if (list) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 插入有头节点单链表元素
|
||||
// 0号节点是头节点
|
||||
int InsertLinkListWithHead(LinkList list, int index, element_type elem) {
|
||||
if (index < 1) {
|
||||
printf("InsertLinkListWithHead:插入索引值过小!\n");
|
||||
return 1;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkListNode* p;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 0;
|
||||
// 将链表头结点指向p,为第0个结点
|
||||
p = list;
|
||||
// 循环遍历到达指定索引号的单链表的结点
|
||||
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
while (p->next != NULL && i < index - 1) {
|
||||
p = p->next;
|
||||
i++;
|
||||
}
|
||||
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
if (i < index - 1) {
|
||||
printf("InsertLinkListWithHead:插入索引值过大!\n");
|
||||
return 1;
|
||||
}
|
||||
// 此时i==index-1
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (s) {
|
||||
s->data = elem;
|
||||
// 将p原来的后继给新的结点
|
||||
s->next = p->next;
|
||||
p->next = s;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InsertLinkListWithHead:分配内存失败!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 插入无头节点单链表元素
|
||||
// C语言也无法调用这个函数
|
||||
int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) {
|
||||
if (index < 0) {
|
||||
printf("InsertLinkListWithoutHead:插入索引值过小!\n");
|
||||
return 1;
|
||||
}
|
||||
if (index == 0) {
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (s) {
|
||||
s->data = elem;
|
||||
// 将s的后继设为list指针
|
||||
s->next = list;
|
||||
// 将list指针设置为s指针
|
||||
list = s;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InsertLinkListWithoutHead:分配内存失败!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkListNode* p;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 0;
|
||||
// 将链表头结点指向p,为第0个结点
|
||||
p = list;
|
||||
// 循环遍历到达指定索引号的单链表的结点
|
||||
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
while (p->next != NULL && i < index - 1) {
|
||||
p = p->next;
|
||||
i++;
|
||||
}
|
||||
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
if (i < index - 1) {
|
||||
printf("InsertLinkListWithoutHead:插入索引值过大!\n");
|
||||
return 1;
|
||||
}
|
||||
// 此时i==index-1
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (s) {
|
||||
s->data = elem;
|
||||
// 将p原来的后继给新的结点
|
||||
s->next = p->next;
|
||||
p->next = s;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InsertLinkListWithoutHead:分配空间失败!\n");
|
||||
}
|
||||
}
|
||||
//// 插入有头节点单链表元素
|
||||
//// 0号节点是头节点
|
||||
//int InsertLinkListWithHead(LinkList list, int index, element_type elem) {
|
||||
// if (index < 1) {
|
||||
// printf("InsertLinkListWithHead:插入索引值过小!\n");
|
||||
// return 1;
|
||||
// }
|
||||
// // 定义一个结点指针p指向当前扫描到的结点
|
||||
// LinkListNode* p;
|
||||
// // 定义一个变量i表示当前扫描到的结点的索引号
|
||||
// int i = 0;
|
||||
// // 将链表头结点指向p,为第0个结点
|
||||
// p = list;
|
||||
// // 循环遍历到达指定索引号的单链表的结点
|
||||
// // 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
// while (p->next != NULL && i < index - 1) {
|
||||
// p = p->next;
|
||||
// i++;
|
||||
// }
|
||||
// // 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
// if (i < index - 1) {
|
||||
// printf("InsertLinkListWithHead:插入索引值过大!\n");
|
||||
// return 1;
|
||||
// }
|
||||
// // 此时i==index-1
|
||||
// LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
// if (s) {
|
||||
// s->data = elem;
|
||||
// // 将p原来的后继给新的结点
|
||||
// s->next = p->next;
|
||||
// p->next = s;
|
||||
// return 0;
|
||||
// }
|
||||
// else {
|
||||
// printf("InsertLinkListWithHead:分配内存失败!\n");
|
||||
// return 1;
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//// 插入无头节点单链表元素
|
||||
//// C语言也无法调用这个函数
|
||||
//int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) {
|
||||
// if (index < 0) {
|
||||
// printf("InsertLinkListWithoutHead:插入索引值过小!\n");
|
||||
// return 1;
|
||||
// }
|
||||
// if (index == 0) {
|
||||
// LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
// if (s) {
|
||||
// s->data = elem;
|
||||
// // 将s的后继设为list指针
|
||||
// s->next = list;
|
||||
// // 将list指针设置为s指针
|
||||
// list = s;
|
||||
// return 0;
|
||||
// }
|
||||
// else {
|
||||
// printf("InsertLinkListWithoutHead:分配内存失败!\n");
|
||||
// return 1;
|
||||
// }
|
||||
// }
|
||||
// // 定义一个结点指针p指向当前扫描到的结点
|
||||
// LinkListNode* p;
|
||||
// // 定义一个变量i表示当前扫描到的结点的索引号
|
||||
// int i = 0;
|
||||
// // 将链表头结点指向p,为第0个结点
|
||||
// p = list;
|
||||
// // 循环遍历到达指定索引号的单链表的结点
|
||||
// // 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
// while (p->next != NULL && i < index - 1) {
|
||||
// p = p->next;
|
||||
// i++;
|
||||
// }
|
||||
// // 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
// if (i < index - 1) {
|
||||
// printf("InsertLinkListWithoutHead:插入索引值过大!\n");
|
||||
// return 1;
|
||||
// }
|
||||
// // 此时i==index-1
|
||||
// LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
// if (s) {
|
||||
// s->data = elem;
|
||||
// // 将p原来的后继给新的结点
|
||||
// s->next = p->next;
|
||||
// p->next = s;
|
||||
// return 0;
|
||||
// }
|
||||
// else {
|
||||
// printf("InsertLinkListWithoutHead:分配空间失败!\n");
|
||||
// return 1;
|
||||
// }
|
||||
//}
|
||||
@@ -1,15 +1,19 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "head.h"
|
||||
|
||||
#pragma warning(disable:6385)
|
||||
#pragma warning(disable:6386)
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 顺序表
|
||||
class SequenceList {
|
||||
public:
|
||||
element_type *data{};
|
||||
int length{};
|
||||
// 构造函数
|
||||
SequenceList();
|
||||
// 插入函数
|
||||
virtual bool Insert(int index, element_type elem);
|
||||
// 打印函数
|
||||
@@ -33,8 +37,6 @@ public:
|
||||
// 静态顺序表
|
||||
class StaticSequenceList: public SequenceList{
|
||||
public:
|
||||
element_type data[MAXSIZE]{};
|
||||
int length;
|
||||
// 构造函数
|
||||
StaticSequenceList();
|
||||
// 插入函数
|
||||
@@ -44,9 +46,6 @@ public:
|
||||
// 动态顺序表
|
||||
class DynamicSequenceList: public SequenceList{
|
||||
public:
|
||||
// 给一个指针来分配动态数组
|
||||
element_type *data;
|
||||
int length;
|
||||
// 已分配的最大容量
|
||||
int max_size;
|
||||
// 构造函数
|
||||
@@ -61,14 +60,17 @@ private:
|
||||
bool ReIncrease(int len);
|
||||
};
|
||||
|
||||
SequenceList::SequenceList() {
|
||||
this->length = 0;
|
||||
}
|
||||
|
||||
StaticSequenceList::StaticSequenceList() : SequenceList() {
|
||||
this->length=0;
|
||||
this->data = (element_type*)malloc(MAXSIZE * sizeof(element_type));
|
||||
}
|
||||
|
||||
DynamicSequenceList::DynamicSequenceList() : SequenceList() {
|
||||
// 初初始化动态顺序表长度为0
|
||||
this->max_size=0;
|
||||
this->length = 0;
|
||||
// 申请一片连续的存储空间
|
||||
auto* space = (element_type*)malloc(MAXSIZE * sizeof(element_type));
|
||||
if (space) {
|
||||
@@ -76,19 +78,19 @@ DynamicSequenceList::DynamicSequenceList() : SequenceList() {
|
||||
this->max_size = MAXSIZE;
|
||||
}
|
||||
else {
|
||||
printf("InitSequenceList:分配空间失败!\n");
|
||||
cout << "InitSequenceList:分配空间失败!" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void SequenceList::Printf() {
|
||||
for (int i = 0; i < this->length; i++) {
|
||||
printf("第%d个元素值为%c\n", i + 1, this->data[i]);
|
||||
cout << "第" << i + 1 << "个元素值为" << this->data[i] << endl;
|
||||
}
|
||||
}
|
||||
|
||||
bool DynamicSequenceList::OtherIncrease(int len) {
|
||||
if (len <= 0) {
|
||||
printf("OtherIncrease:申请空间应该大于0!\n");
|
||||
cout << "OtherIncrease:申请空间应该大于0!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 申请一片连续的存储空间
|
||||
@@ -106,14 +108,14 @@ bool DynamicSequenceList::OtherIncrease(int len) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
printf("OtherIncrease:重新分配空间失败!\n");
|
||||
cout << "OtherIncrease:重新分配空间失败!" << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool DynamicSequenceList::ReIncrease(int len) {
|
||||
if (len <= 0) {
|
||||
printf("ReIncrease:申请空间应该大于0!\n");
|
||||
cout << "ReIncrease:申请空间应该大于0!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 申请一片连续的存储空间
|
||||
@@ -127,7 +129,7 @@ bool DynamicSequenceList::ReIncrease(int len) {
|
||||
else {
|
||||
this->max_size = 0;
|
||||
this->length = 0;
|
||||
printf("ReIncrease:分配其他地址空间失败!\n");
|
||||
cout << "ReIncrease:分配其他地址空间失败!" << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -139,12 +141,12 @@ bool SequenceList::Insert(int index, element_type elem) {
|
||||
bool StaticSequenceList::Insert(int index, element_type elem) {
|
||||
// 当静态顺序表已经满了就不能插入任何元素
|
||||
if (this->length >= MAXSIZE) {
|
||||
printf("Insert:静态顺序表空间不足,插入失败!\n");
|
||||
cout << "Insert:静态顺序表空间不足,插入失败!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 索引位置从0开始,所以可以插入的范围是0到list->length
|
||||
if (index > this->length || index < 0) {
|
||||
printf("Insert:插入索引%d超过索引范围!\n", index);
|
||||
cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 从最后一个元素开始交换后移,list->length是空的
|
||||
@@ -158,7 +160,7 @@ bool StaticSequenceList::Insert(int index, element_type elem) {
|
||||
|
||||
bool DynamicSequenceList::Insert(int index, element_type elem) {
|
||||
if (index > this->length || index < 0) {
|
||||
printf("Insert:插入索引%d超过索引范围!\n", index);
|
||||
cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 当动态顺序表已经满了,需要新增一个位置
|
||||
@@ -166,7 +168,7 @@ bool DynamicSequenceList::Insert(int index, element_type elem) {
|
||||
if (this->length >= MAXSIZE) {
|
||||
bool result = this->ReIncrease(1);
|
||||
if (!result) {
|
||||
printf("Insert:申请空间失败!\n");
|
||||
cout << "Insert:申请空间失败!" << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -182,7 +184,7 @@ bool SequenceList::LoopInsert(element_type *elem, int start, int end) {
|
||||
for (int i = 0; i < end; i++) {
|
||||
bool result = this->Insert(i, elem[i + start]);
|
||||
if (!result) {
|
||||
printf("LoopInsert:循环插入失败!\n");
|
||||
cout << "LoopInsert:循环插入失败!" << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -191,7 +193,7 @@ bool SequenceList::LoopInsert(element_type *elem, int start, int end) {
|
||||
|
||||
bool SequenceList::Delete(int index, element_type &elem) {
|
||||
if (index >= this->length || index < 0) {
|
||||
printf("Delete:删除索引超过索引范围!\n");
|
||||
cout << "Delete:删除索引" << index << "超过索引范围!" << endl;
|
||||
return false;
|
||||
}
|
||||
elem = this->data[index];
|
||||
@@ -204,7 +206,7 @@ bool SequenceList::Delete(int index, element_type &elem) {
|
||||
|
||||
bool SequenceList::MultiDelete(int index, int len, element_type *elem) {
|
||||
if (index + len >= this->length || index < 0) {
|
||||
printf("MultiDelete:删除索引超过索引范围!\n");
|
||||
cout << "MultiDelete:删除索引" << index << "超过索引范围!" << endl;
|
||||
return false;
|
||||
}
|
||||
for (int i = index; i < this->length - len; i++) {
|
||||
@@ -219,8 +221,8 @@ bool SequenceList::MultiDelete(int index, int len, element_type *elem) {
|
||||
|
||||
element_type SequenceList::GetElem(int index) {
|
||||
if (index >= this->length || index < 0) {
|
||||
printf("GetElem:查找索引超过索引范围!\n");
|
||||
return DEFAULTELEM;
|
||||
cout << "GetElem:查找索引" << index << "超过索引范围!" << endl;
|
||||
return DEFAULTDATA;
|
||||
}
|
||||
return this->data[index];
|
||||
}
|
||||
@@ -232,7 +234,7 @@ int SequenceList::Locate(element_type elem) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
printf("Locate:未能定位到对应值的元素!\n");
|
||||
cout << "Locate:未能定位到对应值的元素!" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -249,7 +251,6 @@ bool SequenceList::Destroy() {
|
||||
if (this->data) {
|
||||
free(this->data);
|
||||
}
|
||||
delete this;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
SequenceListTest();
|
||||
// LinkListTest();
|
||||
//SequenceListTest();
|
||||
LinkListTest();
|
||||
return 0;
|
||||
}
|
||||
@@ -8,11 +8,10 @@ int SequenceListTest() {
|
||||
list.Insert(0, 'a');
|
||||
element_type a[6] = {'1','2','3','4','5','6'};
|
||||
list.LoopInsert(a, 0, 6);
|
||||
printf("%c", list.data[2]);
|
||||
/*element_type b[3] = { '9', 'a', 'e' };
|
||||
list.LoopInsert(b, 1, 2);*/
|
||||
list.Printf();
|
||||
//printf("%c", list.data[2]);
|
||||
element_type b[3] = { '9', 'a', 'e' };
|
||||
list.LoopInsert(b, 1, 2);
|
||||
list.Printf();
|
||||
/*list.Printf();
|
||||
printf("\n");
|
||||
int len = 2;
|
||||
@@ -26,21 +25,16 @@ int SequenceListTest() {
|
||||
InitDynamicSequenceList(&dlist);
|
||||
OtherIncreaseDynamicSequenceList(&dlist, 15);
|
||||
printf("%d", dlist.max_size);*/
|
||||
//int index = list.Locate('5');
|
||||
//printf("%d", index);
|
||||
//list.Destroy();
|
||||
int index = list.Locate('5');
|
||||
index = list.GetElem(1);
|
||||
cout << list.Empty() << endl;
|
||||
list.Destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LinkListTest() {
|
||||
LinkList list = nullptr;
|
||||
InitLinkListWithoutHead(list);
|
||||
// InitLinkListWithHead(list);
|
||||
//LinkList list = CreateLinkListWithHead();
|
||||
// int empty = EmptyLinkListWithHead(list);
|
||||
// LinkList list = CreateLinkListWithoutHead();
|
||||
int empty = EmptyLinkListWithoutHead(list);
|
||||
printf("%d", empty);
|
||||
LinkList list;
|
||||
cout << list.Empty() << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user