mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-16 15:07:38 +08:00
更新串
This commit is contained in:
237
Code/CPP-Code/head/link_string.h
Normal file
237
Code/CPP-Code/head/link_string.h
Normal file
@@ -0,0 +1,237 @@
|
||||
#include "head.h"
|
||||
|
||||
// 块链串结点
|
||||
class LinkStringNode {
|
||||
private:
|
||||
// 数据
|
||||
char *_data{};
|
||||
// 指针
|
||||
LinkStringNode *_next{};
|
||||
public:
|
||||
// 初始化数据
|
||||
bool SetData();
|
||||
|
||||
bool SetData(int data_size);
|
||||
|
||||
// 设置数据
|
||||
bool SetData(char *character);
|
||||
|
||||
bool SetData(int index, char character);
|
||||
|
||||
// 获取数据
|
||||
char *GetData();
|
||||
|
||||
char GetData(int index);
|
||||
|
||||
// 设置指针
|
||||
bool SetNext();
|
||||
|
||||
bool SetNext(LinkStringNode *next);
|
||||
|
||||
// 获取指针
|
||||
LinkStringNode *GetNext();
|
||||
|
||||
// 初始化
|
||||
LinkStringNode();
|
||||
|
||||
explicit LinkStringNode(int max_size);
|
||||
|
||||
LinkStringNode(int max_size, LinkStringNode *next);
|
||||
|
||||
LinkStringNode(int max_size, char *character, LinkStringNode *next);
|
||||
|
||||
LinkStringNode(int max_size, char *character);
|
||||
};
|
||||
|
||||
bool LinkStringNode::SetData() {
|
||||
this->_data = new char[1];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkStringNode::SetData(int data_size) {
|
||||
this->_data = new char[data_size];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkStringNode::SetData(char *character) {
|
||||
this->_data = character;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkStringNode::SetData(int index, char character) {
|
||||
this->_data[index] = character;
|
||||
return true;
|
||||
}
|
||||
|
||||
char *LinkStringNode::GetData() {
|
||||
return this->_data;
|
||||
}
|
||||
|
||||
char LinkStringNode::GetData(int index) {
|
||||
return this->_data[index];
|
||||
}
|
||||
|
||||
bool LinkStringNode::SetNext() {
|
||||
this->_next = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkStringNode::SetNext(LinkStringNode *next) {
|
||||
this->_next = next;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkStringNode *LinkStringNode::GetNext() {
|
||||
return this->_next;
|
||||
}
|
||||
|
||||
LinkStringNode::LinkStringNode() {
|
||||
this->SetData();
|
||||
this->SetNext();
|
||||
}
|
||||
|
||||
LinkStringNode::LinkStringNode(int max_size) {
|
||||
this->SetData(max_size);
|
||||
this->SetNext();
|
||||
}
|
||||
|
||||
LinkStringNode::LinkStringNode(int max_size, LinkStringNode *next) {
|
||||
this->SetData(max_size);
|
||||
this->SetNext(next);
|
||||
}
|
||||
|
||||
LinkStringNode::LinkStringNode(int max_size, char *character, LinkStringNode *next) {
|
||||
this->SetData(max_size);
|
||||
for (int i = 0; i < max_size; i++) {
|
||||
if (character[i] != '\0') {
|
||||
this->SetData(i, character[i]);
|
||||
}
|
||||
}
|
||||
this->SetNext(next);
|
||||
}
|
||||
|
||||
LinkStringNode::LinkStringNode(int max_size, char *character) {
|
||||
this->SetData(max_size);
|
||||
for (int i = 0; i < max_size; i++) {
|
||||
if (character[i] != '\0') {
|
||||
this->SetData(i, character[i]);
|
||||
}
|
||||
}
|
||||
this->SetNext();
|
||||
}
|
||||
|
||||
class LinkString {
|
||||
private:
|
||||
// 头尾指针
|
||||
LinkStringNode *_front{}, *_rear{};
|
||||
// 块链长度
|
||||
int _data_size{};
|
||||
// 长度
|
||||
int _length{};
|
||||
public:
|
||||
// 设置头指针
|
||||
bool SetFront();
|
||||
bool SetFront(LinkStringNode *front);
|
||||
|
||||
// 获取头指针
|
||||
LinkStringNode *GetFront();
|
||||
|
||||
// 设置尾指针
|
||||
bool SetRear();
|
||||
bool SetRear(LinkStringNode *rear);
|
||||
|
||||
// 获取尾指针
|
||||
LinkStringNode *GetRear();
|
||||
|
||||
// 设置总体块链单元长度
|
||||
bool SetDataSize();
|
||||
|
||||
bool SetDataSize(int data_size);
|
||||
|
||||
// 获取数据长度
|
||||
int GetDataSize() const;
|
||||
|
||||
// 数据长度自加
|
||||
bool SetLength();
|
||||
|
||||
// 设置数据长度
|
||||
bool SetLength(int length);
|
||||
|
||||
// 获取数据长度
|
||||
int GetLength() const;
|
||||
|
||||
// 初始化
|
||||
LinkString();
|
||||
|
||||
explicit LinkString(int data_size);
|
||||
};
|
||||
|
||||
bool LinkString::SetFront() {
|
||||
this->_front = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkString::SetFront(LinkStringNode *front) {
|
||||
this->_front = front;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkStringNode *LinkString::GetFront() {
|
||||
return this->_front;
|
||||
}
|
||||
|
||||
bool LinkString::SetRear() {
|
||||
this->_rear = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkString::SetRear(LinkStringNode *rear) {
|
||||
this->_rear = rear;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkStringNode *LinkString::GetRear() {
|
||||
return this->_rear;
|
||||
}
|
||||
|
||||
bool LinkString::SetDataSize() {
|
||||
this->_data_size = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkString::SetDataSize(int data_size) {
|
||||
this->_data_size = data_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
int LinkString::GetDataSize() const {
|
||||
return this->_data_size;
|
||||
}
|
||||
|
||||
bool LinkString::SetLength() {
|
||||
this->_length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkString::SetLength(int length) {
|
||||
this->_length = length;
|
||||
return true;
|
||||
}
|
||||
|
||||
int LinkString::GetLength() const {
|
||||
return this->_length;
|
||||
}
|
||||
|
||||
LinkString::LinkString() {
|
||||
this->SetFront();
|
||||
this->SetRear();
|
||||
this->SetLength(0);
|
||||
this->SetDataSize(1);
|
||||
}
|
||||
|
||||
LinkString::LinkString(int data_size) {
|
||||
this->SetFront();
|
||||
this->SetRear();
|
||||
this->SetLength(0);
|
||||
this->SetDataSize(data_size);
|
||||
}
|
||||
118
Code/CPP-Code/head/sequence_string.h
Normal file
118
Code/CPP-Code/head/sequence_string.h
Normal file
@@ -0,0 +1,118 @@
|
||||
#include "head.h"
|
||||
|
||||
// 顺序串
|
||||
class SequenceString{
|
||||
private:
|
||||
// 数据
|
||||
char* _data{};
|
||||
// 长度
|
||||
int _length{};
|
||||
// 最大容量
|
||||
int _max_size{};
|
||||
public:
|
||||
// 设置数据
|
||||
bool SetData();
|
||||
|
||||
bool SetData(int max_size);
|
||||
|
||||
bool SetData(char *character);
|
||||
|
||||
bool SetData(int index, char character);
|
||||
|
||||
// 获取数据
|
||||
char* GetData();
|
||||
|
||||
char GetData(int index);
|
||||
|
||||
// 长度自加
|
||||
bool SetLength();
|
||||
|
||||
// 设置长度
|
||||
bool SetLength(int length);
|
||||
|
||||
// 获取长度
|
||||
int GetLength() const;
|
||||
|
||||
// 设置最大容量
|
||||
bool SetMaxSize();
|
||||
|
||||
bool SetMaxSize(int max_size);
|
||||
|
||||
// 获取最大容量
|
||||
int GetMaxSize() const;
|
||||
|
||||
// 构造函数
|
||||
SequenceString();
|
||||
|
||||
explicit SequenceString(int max_size);
|
||||
};
|
||||
|
||||
bool SequenceString::SetData() {
|
||||
this->_data = new char[MAXSIZE];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceString::SetData(int max_size) {
|
||||
this->_data = new char[max_size];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceString::SetData(char * character) {
|
||||
this->_data = character;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceString::SetData(int index, char character) {
|
||||
this->_data[index] = character;
|
||||
return true;
|
||||
}
|
||||
|
||||
char *SequenceString::GetData() {
|
||||
return this->_data;
|
||||
}
|
||||
|
||||
char SequenceString::GetData(int index) {
|
||||
return this->_data[index];
|
||||
}
|
||||
|
||||
bool SequenceString::SetLength() {
|
||||
this->_length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceString::SetLength(int length) {
|
||||
this->_length = length;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SequenceString::GetLength() const {
|
||||
return this->_length;
|
||||
}
|
||||
|
||||
bool SequenceString::SetMaxSize() {
|
||||
this->_max_size = MAXSIZE;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceString::SetMaxSize(int max_size) {
|
||||
this->_max_size = max_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SequenceString::GetMaxSize() const {
|
||||
return this->_max_size;
|
||||
}
|
||||
|
||||
SequenceString::SequenceString() {
|
||||
this->SetData();
|
||||
this->SetMaxSize();
|
||||
this->SetLength(0);
|
||||
}
|
||||
|
||||
SequenceString::SequenceString(int max_size) {
|
||||
this->SetData(max_size);
|
||||
this->SetMaxSize();
|
||||
this->SetLength(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include "../head/link_stack.h"
|
||||
#include "../head/sequence_queue.h"
|
||||
#include "../head/link_queue.h"
|
||||
#include "../head/sequence_string.h"
|
||||
#include "../head/link_string.h"
|
||||
|
||||
bool SequenceListTest() {
|
||||
DynamicSequenceList list;
|
||||
|
||||
@@ -2,5 +2,9 @@
|
||||
#define MAXSIZE 5
|
||||
// 定义默认值
|
||||
#define DEFAULTELEM '\0'
|
||||
// 定义默认串值
|
||||
#define DEFAULTCHAR '\0'
|
||||
// 定义串块链数据长度
|
||||
#define DATASIZE 4
|
||||
// 定义默认数据类型
|
||||
typedef char element_type;
|
||||
@@ -7,6 +7,12 @@ typedef struct LinkListNode {
|
||||
} LinkListNode, *LinkList;
|
||||
|
||||
// 初始化
|
||||
bool InitLinkList(LinkList &list) {
|
||||
list->data = DEFAULTELEM;
|
||||
list->next = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkList InitLinkList() {
|
||||
auto list = (LinkList) malloc(sizeof(LinkList));
|
||||
list->data = DEFAULTELEM;
|
||||
@@ -14,12 +20,6 @@ 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;
|
||||
|
||||
@@ -4,8 +4,8 @@ typedef struct LinkStackNode{
|
||||
// 数据
|
||||
element_type data;
|
||||
// 指针
|
||||
struct LinkStackNode *next;
|
||||
} *LinkStack;
|
||||
LinkStackNode *next;
|
||||
} LinkStackNode, *LinkStack;
|
||||
|
||||
// 初始化
|
||||
LinkStack InitLinkStack(){
|
||||
|
||||
25
Code/Code/head/link_string.h
Normal file
25
Code/Code/head/link_string.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "head.h"
|
||||
|
||||
// 块链串结点
|
||||
typedef struct LinkStringNode {
|
||||
// 数据
|
||||
char *data;
|
||||
// 指针
|
||||
LinkStringNode *next;
|
||||
} LinkStringNode, *LinkString;
|
||||
|
||||
bool InitLinkString(LinkString &string) {
|
||||
string->data = (char *) malloc(sizeof(char) * DATASIZE);
|
||||
string->next = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
LinkString InitLinkString() {
|
||||
auto* string = (LinkString) malloc(sizeof(LinkString));
|
||||
string->data = (char *) malloc(sizeof(char) * DATASIZE);
|
||||
string->next = nullptr;
|
||||
return (LinkString &)string;
|
||||
}
|
||||
|
||||
|
||||
|
||||
41
Code/Code/head/sequence_string.h
Normal file
41
Code/Code/head/sequence_string.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "head.h"
|
||||
|
||||
// 顺序串
|
||||
typedef struct {
|
||||
// 数据
|
||||
char *data;
|
||||
// 长度
|
||||
unsigned int length;
|
||||
// 最大容量
|
||||
unsigned int max_size;
|
||||
} SequenceString;
|
||||
|
||||
bool InitSequenceString(SequenceString &string) {
|
||||
string.data = (char *) malloc(sizeof(char) * MAXSIZE);
|
||||
string.max_size = MAXSIZE;
|
||||
string.length = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InitSequenceString(SequenceString &string, int max_size) {
|
||||
string.data = (char *) malloc(sizeof(char) * max_size);
|
||||
string.max_size = max_size;
|
||||
string.length = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
SequenceString InitSequenceString() {
|
||||
auto *string = (SequenceString *) malloc(sizeof(SequenceString));
|
||||
string->data = (char *) malloc(sizeof(char) * MAXSIZE);
|
||||
string->max_size = MAXSIZE;
|
||||
string->length = 0;
|
||||
return (SequenceString &) string;
|
||||
}
|
||||
|
||||
SequenceString InitSequenceString(int max_size) {
|
||||
auto *string = (SequenceString *) malloc(sizeof(SequenceString));
|
||||
string->data = (char *) malloc(sizeof(char) * max_size);
|
||||
string->max_size = max_size;
|
||||
string->length = 0;
|
||||
return (SequenceString &) string;
|
||||
}
|
||||
@@ -9,6 +9,8 @@
|
||||
#include "../head/share_stack.h"
|
||||
#include "../head/sequence_queue.h"
|
||||
#include "../head/link_queue.h"
|
||||
#include "../head/sequence_string.h"
|
||||
#include "../head/link_string.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user