1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-02-04 11:24:10 +08:00
Files
CS408/Code/CPP-Code/head/static_sequence_list.h
Didnelpsun 94c62a2082 更新
2021-10-23 23:25:29 +08:00

43 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "sequence_list.h"
// 静态顺序表
class StaticSequenceList : public SequenceList {
public:
// 构造函数
StaticSequenceList();
explicit StaticSequenceList(int max_size);
// 插入函数
bool Insert(int index, element_type elem) override;
};
StaticSequenceList::StaticSequenceList() :SequenceList() {
}
StaticSequenceList::StaticSequenceList(int max_size) : SequenceList(max_size) {
}
bool StaticSequenceList::Insert(int index, element_type elem) {
// 当静态顺序表已经满了就不能插入任何元素
if (this->GetLength() >= this->GetMaxSize()) {
// cout << "Insert:静态顺序表空间不足,插入失败!" << endl;
cout << "Insert:The space size of " << this->GetMaxSize() << " is not enough!" << endl;
return false;
}
// 索引位置从0开始所以可以插入的范围是0到list->length
if (index > this->GetLength() || index < 0) {
// cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
cout << "Insert:Insert index value " << index << " is out of range!" << endl;
return false;
}
// 从最后一个元素开始交换后移list->length是空的
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i - 1));
}
this->SetData(index, elem);
this->SetLength();
return true;
}