diff --git a/Code/CPP-Code/head/head.h b/Code/CPP-Code/head/head.h index 8cc94c9..2e3e73e 100644 --- a/Code/CPP-Code/head/head.h +++ b/Code/CPP-Code/head/head.h @@ -1,8 +1,8 @@ -// 初始化最大长度 +锘// 鍒濆鍖栨渶澶ч暱搴 #define MAXSIZE 5 -// 定义默认值 +// 瀹氫箟榛樿鍊 #define DEFAULTDATA '0' -// 定义最大值 +// 瀹氫箟鏈澶у //#define INFINITY 32767 -// 定义默认数据类型 -typedef char element_type; +// 瀹氫箟榛樿鏁版嵁绫诲瀷 +typedef char element_type; \ No newline at end of file diff --git a/Code/CPP-Code/head/link_list.h b/Code/CPP-Code/head/link_list.h index 1630310..1c1c3a2 100644 --- a/Code/CPP-Code/head/link_list.h +++ b/Code/CPP-Code/head/link_list.h @@ -1,120 +1,282 @@ -#include +锘#include #include #include "head.h" // 鍗曢摼琛ㄧ粨鐐 class LinkListNode { +private: + // 鏁版嵁 + element_type _data{}; + // 鎸囬拡 + LinkListNode* _next{}; public: - // 鏁版嵁 - element_type data; - // 鎸囬拡 - LinkListNode* next; - // 鏋勯犲嚱鏁 - LinkListNode(); - explicit LinkListNode(element_type data); + // 璁剧疆鏁版嵁 + bool SetData(element_type data); + // 鑾峰彇鏁版嵁 + element_type GetData() const; + // 璁剧疆next + bool SetNext(LinkListNode* next); + // 鑾峰彇next + LinkListNode* GetNext(); + // 鏋勯犲嚱鏁 + LinkListNode(); + explicit LinkListNode(element_type data); + LinkListNode(element_type data, LinkListNode* next); }; -class LinkList{ +class LinkList { +private: + // 鎸囬拡 + LinkListNode* _next{}; + // 閾捐〃闀垮害 + int _length{}; + // 绫诲瀷锛岀湡鏈夊ご鑺傜偣锛屽亣鏃犲ご鑺傜偣 + bool _type{}; +protected: + // 璁剧疆閾捐〃绫诲瀷 + bool SetType(bool type); public: - // 鎸囬拡 - LinkListNode* next; - // 閾捐〃闀垮害 - int length; - // 鏋勯犲嚱鏁 - LinkList(); - // 鍒ょ┖ - bool Empty(); + // 璁剧疆next + bool SetNext(LinkListNode* next); + // 鑾峰彇next + LinkListNode* GetNext(); + // 闀垮害鑷姞1 + bool SetLength(); + // 璁剧疆闀垮害 + bool SetLength(int length); + // 鑾峰彇闀垮害 + int GetLength() const; + // 鑾峰彇閾捐〃绫诲瀷 + bool GetType() const; + // 鏋勯犲嚱鏁 + LinkList(); + // 鎵撳嵃 + virtual bool Print() = 0; + // 鍒ょ┖ + bool Empty() const; + // 鎻掑叆 + virtual bool Insert(int index, element_type data) = 0; + // 寰幆鎻掑叆 + bool LoopInsert(element_type* elem, int start, int length); }; -class LinkListWithHead: public LinkList{ +class LinkListWithHead : public LinkList { public: - // 鏋勯犲嚱鏁 - LinkListWithHead(); + // 鏋勯犲嚱鏁 + LinkListWithHead(); + // 鎵撳嵃 + bool Print() override; + // 鎻掑叆 + bool Insert(int index, element_type data) override; }; -class LinkListWithoutHead: public LinkList{ +class LinkListWithoutHead : public LinkList { +private: + // 鏁版嵁 + element_type _data{}; public: - // 鏁版嵁 - element_type data; - // 鏋勯犲嚱鏁 - LinkListWithoutHead(); + // 璁剧疆鏁版嵁 + bool SetData(element_type data); + // 鑾峰彇鏁版嵁 + element_type GetData() const; + // 鏋勯犲嚱鏁 + LinkListWithoutHead(); + // 鎵撳嵃 + bool Print() override; + // 鎻掑叆 + bool Insert(int index, element_type data) override; }; +bool LinkListNode::SetData(element_type data) { + this->_data = data; + return true; +} + +element_type LinkListNode::GetData() const { + return this->_data; +} + +bool LinkListNode::SetNext(LinkListNode* next) { + this->_next = next; + return true; +} + +LinkListNode* LinkListNode::GetNext() { + return this->_next; +} + LinkListNode::LinkListNode() { - this->data = NULL; - this->next = nullptr; + this->SetData(NULL); + this->SetNext(nullptr); } LinkListNode::LinkListNode(element_type data) { - this->data = data; - this->next = nullptr; + this->SetData(data); + this->SetNext(nullptr); +} + +LinkListNode::LinkListNode(element_type data, LinkListNode* next) { + this->SetData(data); + this->SetNext(next); +} + +bool LinkList::SetNext(LinkListNode* next) { + this->_next = next; + return true; +} + +LinkListNode* LinkList::GetNext() { + return this->_next; +} + +bool LinkList::SetLength() { + this->_length++; + return true; +} + +bool LinkList::SetLength(int length) { + this->_length = length; + return true; +} + +int LinkList::GetLength() const { + return this->_length; +} + +bool LinkList::SetType(bool type) { + this->_type = type; + return true; +} + +bool LinkList::GetType() const { + return this->_type; } LinkList::LinkList() { - this->next= nullptr; - this->length =0; + this->SetNext(nullptr); + this->SetLength(0); } -LinkListWithHead::LinkListWithHead() = default; +LinkListWithHead::LinkListWithHead() { + this->SetType(true); +}; + +bool LinkListWithoutHead::SetData(element_type data) { + this->_data = data; + return true; +} + +element_type LinkListWithoutHead::GetData() const { + return this->_data; +} LinkListWithoutHead::LinkListWithoutHead() { - this->data = NULL; + this->SetType(false); + this->SetData(NULL); } -bool LinkList::Empty() { - if(this->length==0){ - return true; - } else { - return false; - } +bool LinkList::Empty() const { + if (this->GetLength() == 0) { + return true; + } + else { + return false; + } } +bool LinkListWithHead::Print() { + int i = 1; + if (this->GetLength() == 0) { + return true; + } + cout << "绗0涓厓绱犲间负绌" << endl; + // 褰撳墠閬嶅巻鎸囬拡 + LinkListNode* p = this->GetNext(); + if (p != nullptr) { + cout << "绗" << i << "涓厓绱犲间负" << p->GetData() << endl; + i++; + p = p->GetNext(); + } + return true; +} + +bool LinkListWithoutHead::Print() { + int i = 1; + if (this->GetLength() == 0) { + return true; + } + cout << "绗" << i << "涓厓绱犲间负" << this->GetData() << endl; + // 褰撳墠閬嶅巻鎸囬拡 + LinkListNode* p = this->GetNext(); + if (p != nullptr) { + cout << "绗" << i << "涓厓绱犲间负" << p->GetData() << endl; + i++; + p = p->GetNext(); + } + return true; +} + +bool LinkListWithHead::Insert(int index, element_type data) { + if (index < 1) { + cout << "Insert:鎻掑叆绱㈠紩鍊艰繃灏忥紒" << endl; + return false; + } + // 瀹氫箟涓涓粨鐐规寚閽坧鎸囧悜褰撳墠鎵弿鍒扮殑缁撶偣 + LinkListNode* p; + // 瀹氫箟涓涓彉閲廼琛ㄧず褰撳墠鎵弿鍒扮殑缁撶偣鐨勭储寮曞彿 + int i = 1; + // 灏嗛摼琛ㄥご缁撶偣鐨刵ext鎸囧悜p锛屼负绗1涓粨鐐 + p = this->GetNext(); + LinkListNode* s = new LinkListNode(data); + // 濡傛灉璇ラ摼琛ㄤ负绌洪摼琛 + if (p == nullptr) { + this->SetLength(); + this->SetNext(s); + return true; + } + // 寰幆閬嶅巻鍒拌揪鎸囧畾绱㈠紩鍙风殑鍗曢摼琛ㄧ殑缁撶偣 + // 鏉′欢鏄綋鍓嶇粨鐐圭殑涓嬩竴涓笉涓虹┖涓旂储寮曞彿鍒拌揪锛屾墍鍒拌揪鐨勭粨鐐逛竴瀹氫笉鏄┖缁撶偣 + while (p->GetNext() != nullptr && i < index - 1) { + p = p->GetNext(); + i++; + } + // 濡傛灉姝ゆ椂i灏忎簬index-1锛岃〃绀洪亶鍘嗗畬杩樻病鏈夊埌杈惧搴旂殑绱㈠紩 + if (i < index - 1) { + cout << "Insert:鎻掑叆绱㈠紩鍊艰繃澶э紒" << endl; + return false; + } + cout << i << index << endl; + // 姝ゆ椂i==index-1 + // 灏唒鍘熸潵鐨勫悗缁х粰鏂扮殑缁撶偣 + s->SetNext(p->GetNext()); + p->SetNext(s); + cout << p->GetNext() << endl; + this->SetLength(); + return true; +} + +bool LinkListWithoutHead::Insert(int index, element_type data) { + return true; + +} + +bool LinkList::LoopInsert(element_type* elem, int start, int length) { + for (int i = 1; i < length; i++) { + bool result = this->Insert(i, elem[i + start]); + if (!result) { + cout << "LoopInsert:寰幆鎻掑叆澶辫触锛" << endl; + return false; + } + } + return true; +} -//// 鎻掑叆鏈夊ご鑺傜偣鍗曢摼琛ㄥ厓绱 -//// 0鍙疯妭鐐规槸澶磋妭鐐 -//int InsertLinkListWithHead(LinkList list, int index, element_type elem) { -// if (index < 1) { -// printf("InsertLinkListWithHead:鎻掑叆绱㈠紩鍊艰繃灏忥紒\n"); -// return 1; -// } -// // 瀹氫箟涓涓粨鐐规寚閽坧鎸囧悜褰撳墠鎵弿鍒扮殑缁撶偣 -// LinkListNode* p; -// // 瀹氫箟涓涓彉閲廼琛ㄧず褰撳墠鎵弿鍒扮殑缁撶偣鐨勭储寮曞彿 -// 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; -// // 灏唒鍘熸潵鐨勫悗缁х粰鏂扮殑缁撶偣 -// 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; +// cout << "InsertLinkListWithoutHead:鎻掑叆绱㈠紩鍊艰繃灏忥紒" << endl; +// return false; // } // if (index == 0) { // LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode)); @@ -124,11 +286,11 @@ bool LinkList::Empty() { // s->next = list; // // 灏唋ist鎸囬拡璁剧疆涓簊鎸囬拡 // list = s; -// return 0; +// return true; // } // else { -// printf("InsertLinkListWithoutHead:鍒嗛厤鍐呭瓨澶辫触锛乗n"); -// return 1; +// cout << "InsertLinkListWithoutHead:鍒嗛厤鍐呭瓨澶辫触锛" << endl; +// return false; // } // } // // 瀹氫箟涓涓粨鐐规寚閽坧鎸囧悜褰撳墠鎵弿鍒扮殑缁撶偣 @@ -145,8 +307,8 @@ bool LinkList::Empty() { // } // // 濡傛灉姝ゆ椂i灏忎簬index-1锛岃〃绀洪亶鍘嗗畬杩樻病鏈夊埌杈惧搴旂殑绱㈠紩 // if (i < index - 1) { -// printf("InsertLinkListWithoutHead:鎻掑叆绱㈠紩鍊艰繃澶э紒\n"); -// return 1; +// cout << "InsertLinkListWithoutHead:鎻掑叆绱㈠紩鍊艰繃澶э紒" << endl; +// return false; // } // // 姝ゆ椂i==index-1 // LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode)); @@ -155,10 +317,10 @@ bool LinkList::Empty() { // // 灏唒鍘熸潵鐨勫悗缁х粰鏂扮殑缁撶偣 // s->next = p->next; // p->next = s; -// return 0; +// return true; // } // else { -// printf("InsertLinkListWithoutHead:鍒嗛厤绌洪棿澶辫触锛乗n"); -// return 1; +// cout << "InsertLinkListWithoutHead:鍒嗛厤绌洪棿澶辫触锛" << endl; +// return false; // } //} \ No newline at end of file diff --git a/Code/CPP-Code/head/sequence_list.h b/Code/CPP-Code/head/sequence_list.h index 3ccf146..0af8b57 100644 --- a/Code/CPP-Code/head/sequence_list.h +++ b/Code/CPP-Code/head/sequence_list.h @@ -9,29 +9,44 @@ using namespace std; // 椤哄簭琛 class SequenceList { +private: + // 鏁版嵁 + element_type *_data{}; + // 闀垮害 + int _length{}; public: - element_type *data{}; - int length{}; + // 璁剧疆鏁版嵁 + bool SetData(element_type* elem); + bool SetData(int index, element_type elem); + // 鑾峰彇鏁版嵁 + element_type* GetData() const; + element_type GetData(int index) const; + // 闀垮害鑷姞1 + bool SetLength(); + // 璁剧疆闀垮害 + bool SetLength(int length); + // 璁剧疆闀垮害 + int GetLength() const; // 鏋勯犲嚱鏁 SequenceList(); // 鎻掑叆鍑芥暟 - virtual bool Insert(int index, element_type elem); + virtual bool Insert(int index, element_type elem) = 0; // 鎵撳嵃鍑芥暟 - void Printf(); + bool Print() const; // 寰幆鎻掑叆鍑芥暟 - bool LoopInsert(element_type *elem, int start, int end); + bool LoopInsert(element_type *elem, int start, int length); // 鍒犻櫎鍑芥暟 bool Delete(int index, element_type &elem); // 澶氫釜鍒犻櫎鍑芥暟 - bool MultiDelete(int index, int len, element_type *elem); + bool LoopDelete(int index, int len, element_type *elem); // 鎸変綅鑾峰彇鍏冪礌 - element_type GetElem(int index); + element_type GetElem(int index) const; // 鎸夊艰幏鍙栧厓绱 - int Locate(element_type elem); + int Locate(element_type elem) const; // 鍒ょ┖ - bool Empty(); + bool Empty() const; // 閿姣 - bool Destroy(); + bool Destroy() const; }; // 闈欐侀『搴忚〃 @@ -45,9 +60,14 @@ public: // 鍔ㄦ侀『搴忚〃 class DynamicSequenceList: public SequenceList{ -public: +private: // 宸插垎閰嶇殑鏈澶у閲 - int max_size; + int _max_size{}; +public: + // 璁剧疆鏈澶у閲 + bool SetMaxSize(int length); + // 鑾峰彇鏈澶у閲 + int GetMaxSize(); // 鏋勯犲嚱鏁 DynamicSequenceList(); // 鎻掑叆鍑芥暟 @@ -60,32 +80,74 @@ private: bool ReIncrease(int len); }; +bool SequenceList::SetData(element_type* elem) { + this->_data = elem; + return true; +} + +bool SequenceList::SetData(int index, element_type elem) { + this->_data[index] = elem; + return true; +} + +element_type *SequenceList::GetData() const { + return this->_data; +} + +element_type SequenceList::GetData(int index) const { + return this->_data[index]; +} + +bool SequenceList::SetLength() { + this->_length ++; + return true; +} + +bool SequenceList::SetLength(int length) { + this->_length = length; + return true; +} + +int SequenceList::GetLength() const { + return this->_length; +} + SequenceList::SequenceList() { - this->length = 0; + this->SetLength(0); } StaticSequenceList::StaticSequenceList() : SequenceList() { - this->data = (element_type*)malloc(MAXSIZE * sizeof(element_type)); + this->SetData((element_type*)malloc(MAXSIZE * sizeof(element_type))); +} + +bool DynamicSequenceList::SetMaxSize(int length) { + this->_max_size = length; + return true; +} + +int DynamicSequenceList::GetMaxSize() { + return this->_max_size; } DynamicSequenceList::DynamicSequenceList() : SequenceList() { // 鍒濆垵濮嬪寲鍔ㄦ侀『搴忚〃闀垮害涓0 - this->max_size=0; + this->SetMaxSize(0); // 鐢宠涓鐗囪繛缁殑瀛樺偍绌洪棿 auto* space = (element_type*)malloc(MAXSIZE * sizeof(element_type)); if (space) { - this->data = space; - this->max_size = MAXSIZE; + this->SetData(space); + this->SetMaxSize(MAXSIZE); } else { cout << "InitSequenceList:鍒嗛厤绌洪棿澶辫触锛" << endl; } } -void SequenceList::Printf() { - for (int i = 0; i < this->length; i++) { - cout << "绗" << i + 1 << "涓厓绱犲间负" << this->data[i] << endl; +bool SequenceList::Print() const { + for (int i = 0; i < this->GetLength(); i++) { + cout << "绗" << i + 1 << "涓厓绱犲间负" << this->GetData(i) << endl; } + return true; } bool DynamicSequenceList::OtherIncrease(int len) { @@ -94,16 +156,16 @@ bool DynamicSequenceList::OtherIncrease(int len) { return false; } // 鐢宠涓鐗囪繛缁殑瀛樺偍绌洪棿 - int new_length = this->max_size + len; + int new_length = this->GetMaxSize() + len; auto* space = (element_type*)malloc(new_length * sizeof(element_type)); if (space) { // 寤虹珛涓棿鍙橀噺 - this->data = space; - element_type* temp = this->data; - for (int i = 0; i < this->length; i++) { - this->data[i] = temp[i]; + this->SetData(space); + element_type* temp = this->GetData(); + for (int i = 0; i < this->GetLength(); i++) { + this->SetData(i, temp[i]); } - this->max_size = new_length; + this->SetMaxSize(new_length); free(temp); return true; } @@ -113,75 +175,71 @@ bool DynamicSequenceList::OtherIncrease(int len) { } } -bool DynamicSequenceList::ReIncrease(int len) { - if (len <= 0) { +bool DynamicSequenceList::ReIncrease(int length) { + if (length <= 0) { cout << "ReIncrease:鐢宠绌洪棿搴旇澶т簬0锛" << endl; return false; } // 鐢宠涓鐗囪繛缁殑瀛樺偍绌洪棿 - int new_length = this->max_size + len; - auto* space = (element_type*)realloc(this->data, new_length * sizeof(element_type)); + int new_length = this->GetMaxSize() + length; + auto* space = (element_type*)realloc(this->GetData(), new_length * sizeof(element_type)); if (space) { - this->data = space; - this->max_size += len; + this->SetData(space); + this->SetMaxSize(this->GetMaxSize()+length); return true; } else { - this->max_size = 0; - this->length = 0; + this->SetMaxSize(0); + this->SetLength(0); cout << "ReIncrease:鍒嗛厤鍏朵粬鍦板潃绌洪棿澶辫触锛" << endl; return false; } } -bool SequenceList::Insert(int index, element_type elem) { - return false; -} - bool StaticSequenceList::Insert(int index, element_type elem) { // 褰撻潤鎬侀『搴忚〃宸茬粡婊′簡灏变笉鑳芥彃鍏ヤ换浣曞厓绱 - if (this->length >= MAXSIZE) { + if (this->GetLength() >= MAXSIZE) { cout << "Insert:闈欐侀『搴忚〃绌洪棿涓嶈冻锛屾彃鍏ュけ璐ワ紒" << endl; return false; } // 绱㈠紩浣嶇疆浠0寮濮嬶紝鎵浠ュ彲浠ユ彃鍏ョ殑鑼冨洿鏄0鍒發ist->length - if (index > this->length || index < 0) { + if (index > this->GetLength() || index < 0) { cout << "Insert:鎻掑叆绱㈠紩" << index << "瓒呰繃绱㈠紩鑼冨洿锛" << endl; return false; } // 浠庢渶鍚庝竴涓厓绱犲紑濮嬩氦鎹㈠悗绉伙紝list->length鏄┖鐨 - for (int i = this->length; i > index; i--) { - this->data[i] = this->data[i - 1]; + for (int i = this->GetLength(); i > index; i--) { + this->SetData(i, this->GetData(i-1)); } - this->data[index] = elem; - this->length++; + this->SetData(index, elem); + this->SetLength(); return true; } bool DynamicSequenceList::Insert(int index, element_type elem) { - if (index > this->length || index < 0) { + if (index > this->GetLength() || index < 0) { cout << "Insert:鎻掑叆绱㈠紩" << index << "瓒呰繃绱㈠紩鑼冨洿锛" << endl; return false; } // 褰撳姩鎬侀『搴忚〃宸茬粡婊′簡锛岄渶瑕佹柊澧炰竴涓綅缃 // 涓轰簡閬垮厤绱㈠紩鏃犳晥鑰屽澧炲姞涓涓┖闂达紝鎵浠ユ斁鍦ㄦ鏌ョ储寮曞肩殑鍚庨潰 - if (this->length >= MAXSIZE) { + if (this->GetLength() >= MAXSIZE) { bool result = this->ReIncrease(1); if (!result) { cout << "Insert:鐢宠绌洪棿澶辫触锛" << endl; return false; } } - for (int i = this->length; i > index; i--) { - this->data[i] = this->data[i - 1]; + for (int i = this->GetLength(); i > index; i--) { + this->SetData(i, this->GetData(i-1)); } - this->data[index] = elem; - this->length++; + this->SetData(index, elem); + this->SetLength(); return true; } -bool SequenceList::LoopInsert(element_type *elem, int start, int end) { - for (int i = 0; i < end; i++) { +bool SequenceList::LoopInsert(element_type *elem, int start, int length) { + for (int i = 0; i < length; i++) { bool result = this->Insert(i, elem[i + start]); if (!result) { cout << "LoopInsert:寰幆鎻掑叆澶辫触锛" << endl; @@ -192,45 +250,45 @@ bool SequenceList::LoopInsert(element_type *elem, int start, int end) { } bool SequenceList::Delete(int index, element_type &elem) { - if (index >= this->length || index < 0) { + if (index >= this->GetLength() || index < 0) { cout << "Delete:鍒犻櫎绱㈠紩" << index << "瓒呰繃绱㈠紩鑼冨洿锛" << endl; return false; } - elem = this->data[index]; - for (int i = index; i < this->length; i++) { - this->data[i] = this->data[i + 1]; + elem = this->GetData(index); + for (int i = index; i < this->GetLength(); i++) { + this->SetData(i, this->GetData(i+1)); } - this->length--; + this->SetLength(this->GetLength()-1); return true; } -bool SequenceList::MultiDelete(int index, int len, element_type *elem) { - if (index + len >= this->length || index < 0) { - cout << "MultiDelete:鍒犻櫎绱㈠紩" << index << "瓒呰繃绱㈠紩鑼冨洿锛" << endl; +bool SequenceList::LoopDelete(int index, int length, element_type *elem) { + if (index + length > this->GetLength() || index < 0) { + cout << "LoopDelete:鍒犻櫎绱㈠紩" << index + length << "瓒呰繃绱㈠紩鑼冨洿锛" << endl; return false; } - for (int i = index; i < this->length - len; i++) { - if (i < index + len) { - elem[i - index] = this->data[i]; + for (int i = index; i <= this->GetLength() - length; i++) { + if (i < index + length) { + elem[i - index] = this->GetData(i); } - this->data[i] = this->data[i + len]; + this->SetData(i, this->GetData(i + length)); } - this->length -= len; + this->SetLength(this->GetLength()-length); return true; } -element_type SequenceList::GetElem(int index) { - if (index >= this->length || index < 0) { +element_type SequenceList::GetElem(int index) const { + if (index >= this->GetLength() || index < 0) { cout << "GetElem:鏌ユ壘绱㈠紩" << index << "瓒呰繃绱㈠紩鑼冨洿锛" << endl; return DEFAULTDATA; } - return this->data[index]; + return this->GetData(index); } -int SequenceList::Locate(element_type elem) { - for (int i = 0; i < this->length; i++) { - if (this->data[i] == elem) { +int SequenceList::Locate(element_type elem) const { + for (int i = 0; i < this->GetLength(); i++) { + if (this->GetData(i) == elem) { return i; } } @@ -238,8 +296,8 @@ int SequenceList::Locate(element_type elem) { return -1; } -bool SequenceList::Empty() { - if (this->length == 0) { +bool SequenceList::Empty() const { + if (this->GetLength() == 0) { return false; } else { @@ -247,9 +305,9 @@ bool SequenceList::Empty() { } } -bool SequenceList::Destroy() { - if (this->data) { - free(this->data); +bool SequenceList::Destroy() const { + if (this->GetData()) { + free(this->GetData()); } return true; } diff --git a/Code/CPP-Code/source/test.cpp b/Code/CPP-Code/source/test.cpp index d1933eb..146e47d 100644 --- a/Code/CPP-Code/source/test.cpp +++ b/Code/CPP-Code/source/test.cpp @@ -1,40 +1,32 @@ -// 测试文件 +锘// 娴嬭瘯鏂囦欢 #include "../head/sequence_list.h" #include "../head/link_list.h" int SequenceListTest() { DynamicSequenceList list; - 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(); - /*list.Printf(); - printf("\n"); - int len = 2; - element_type elem[2]; - list.MultiDelete(0, len, elem);*/ - /*list.Printf(); - for (int i = 0; i < len; i++) { - printf("%c\n", elem[i]); - }*/ - /*DynamicSequenceList dlist; - InitDynamicSequenceList(&dlist); - OtherIncreaseDynamicSequenceList(&dlist, 15); - printf("%d", dlist.max_size);*/ - int index = list.Locate('5'); - index = list.GetElem(1); - cout << list.Empty() << endl; + list.Print(); + element_type b[4]; + list.LoopDelete(1, 3, b); + list.Print(); + for (int i = 0; i < 3; i++) { + cout << b[i] << endl; + } list.Destroy(); return 0; } int LinkListTest() { - LinkList list; - cout << list.Empty() << endl; + LinkListWithHead list; + //cout << list.Empty() << endl; + //element_type a[6] = { '1','2','3','4','5','6' }; + //list.LoopInsert(a, 1, 6); + list.Insert(1, '3'); + list.Print(); + list.Insert(2, '4'); + list.Print(); return 0; } diff --git a/Computer-Organization/1-data-representation-and-operation-ex.md b/Computer-Organization/1-data-representation-and-operation-ex.md index abeaa11..e1d3cf7 100644 --- a/Computer-Organization/1-data-representation-and-operation-ex.md +++ b/Computer-Organization/1-data-representation-and-operation-ex.md @@ -20,7 +20,15 @@ $0.3\times2=0.6=0+0.6$锛$0.6\times2=1.2=1+0.2$锛$0.2\times2=0.4=0+0.4$锛岃繖 ### 鏁版嵁鏍¢獙 -**渚嬮** +**渚嬮** 璁惧緟鏍¢獙鐨勬暟鎹负$D_8\sim D_1=10101011$锛岃嫢閲囩敤娴锋槑鏍¢獙锛屽叾娴锋槑鐮佷负()锛堣娴锋槑鐮佸叿鏈変竴浣嶇籂閿欒兘鍔涳紝$P_{13}$閲囩敤鍏ㄦ牎楠岋級銆 + +$A.0\,1010\,0\,101\,1\,1\,11$ + +$B.0\,1000\,0\,111\,1\,1\,11$ + +瑙o細$A$銆傞鐩殑閲嶇偣灏辨槸姹傚嚭鏍¢獙浣嶃 + + #### 寰幆鍐椾綑鏍¢獙鐮 @@ -33,3 +41,5 @@ $B.$鍒犻櫎鏁版嵁 $C.$閫氳繃浣欐暟鍊艰嚜琛岀籂姝 $D.$浠ヤ笂鍧囧彲 + +瑙o細$D$銆$CRC$鍙互绾犳涓浣嶆垨澶氫綅閿欒锛堢敱澶氶」寮$G(x)$鍐冲畾锛夛紝鑰屽疄闄呬紶杈撲腑绾犳鏂规硶鍙互鎸夐渶姹傝繘琛岄夋嫨锛屽湪璁$畻鏈虹綉缁滀腑锛岃繖涓夌鏂规硶閮芥槸寰堝父瑙佺殑銆