吃什么?作业复习LinkedList==DEBUG

#include<iostream>

#include<string>

#include<memory>

#include<stdexcept>

using namespace std;

template<typename T>

class LinkedList{

typedef struct Node {

T data;

shared_ptr<Node> next;

Node() :data(T{}),next(nullptr) {} //default //? //data不初始化是有问题的

Node(T e, shared_ptr<Node> link = nullptr) :data(e),next(link){} //why written like this//类型传成sharedptr会不会直接stop?//所以我直接写在一起//Node只是结构体,不是模板,不能写成Node<T>

};//

private: //what***?

shared_ptr<Node>head; //typedef struct Node { } Node; 定义的是普通结构体 Node,不是 Node<T

shared_ptr<Node>tail;

int length; //i-dle的歌好听!

public:

LinkedList() :length(0),tail(nullptr),head(nullptr){

}//用来初始化head

//shared_ptr 默认构造是 nullptr,但是我的带参数构造函数假设 head 已可用。//?

LinkedList(const T v\[\],int n ) :length(0),tail(nullptr),head(nullptr){

for (int i = 0; i < n; i++) {

insert_at_tail(vi);

}

}//第一要复用函数,第二head是nullptr

virtual ~LinkedList() {} //为什么书上写的是virtual ~ ?//对啊吃什么?//virtual是通常用于一个场景是吧

int GetLength() const {

int x; auto it = head;

for (x = 0;it!=nullptr; x++) {

//head = head->next;//const 函数里不能修改head而且本来也不能

it = it->next;

}

return x;

}

void set_tail() {

if (!head) { tail = nullptr; return; }

auto temp = head;

while (temp->next) temp = temp->next;

tail = temp;

}

bool isEmpty() {

if (!head) return 1;

return 0;

}

void Clear() {

head = nullptr;

tail = nullptr;

length = 0;

}

void insert_at_head(const T&e) {

//if (!head) { auto it2 = make_shared<Node>(e, nullptr); head = it2; }//内存泄露

head = make_shared<Node>(e,head);

if (!tail)tail = head;//set_tail(); //效率低下

length++;

}

void insert_at_tail(const T& e) {

auto it =make_shared<Node>(e, nullptr);

if (!head) { //head = it=tail;//关于赋值数据顺序

head = tail = it;

}

//set_tail();//效率低下

else {

tail->next = it; //不能让tail为空

tail = it;

}

length++;

}

int find_position_return(const T& e) {

auto it = head; int k = 1;

while (it) { //检查眼前就是检查所有

if (it->data == e) return k;

it = it->next; k++;

}

return -1;

}

void remove_with_pos(int pos) {

if (pos<1 || pos>length) throw out_of_range("The position is error.");

if (pos == 1) {

head = head->next; if (!head)tail = nullptr;

}

else {

auto it = head;

for (int i = 1; i < pos-1; i++) {

it = it->next;

}

auto it2 = it->next;

it->next = it2->next;

if (pos == length)tail = it;

}

length--;

}

void show() {

auto it = head;

while (it) {

cout << it->data << " ";

it = it->next;

}

cout << endl;

}

};

int main() {

int a5 = { 1,2,3,4,5 };

LinkedList<int> list(a,5);//LinkedList 是模板类,你需要指定类型参数//fixed?

cout << "Length: " << list.GetLength() << endl; // 应该输出 5

list.show(); // 应该输出 1 2 3 4 5

cout << endl;

list.insert_at_head(0);

list.show(); // 应该输出 0 1 2 3 4 5

list.remove_with_pos(2);

cout << "After removing position of 2,the list shows:" << endl;

list.show();

cout<<"The position of 3 is "<<list.find_position_return(3)<<endl;

return 0;

}

确实看得出来上课确实什么都没讲,我缺乏的点在于构造的参数关系,以及内存空间的权限,以及正反逻辑,而且还有暂时对算法时间复杂度忽略的问题

当然作业还有SeqList,现在我打算做这个作业。

SeqList(const SeqList&) = delete;

SeqList& operator=(const SeqList&) = delete;

这是禁用拷贝操作,暂时没看懂。

#include<iostream>

#include<memory>

#include<string>

#include<stdexcept>

using namespace std;

template<typename T>

class SeqList {

private:

int maxSize;

int length;

T* data;

public:

SeqList() :data(), length(0), maxSize(0) {}

SeqList(const T v\[\], int n) :length(0),maxSize(0),data(nullptr){

for (int i = 0; i < n; i++)

push_back(vi);

}//error

virtual ~SeqList() { delete\[\] data; }//unknown before

int getLength() { return length; }

//void expand() { if (length >= maxSize)maxSize = (maxSize==0)?1:2*maxSize; auto it = new TmaxSize; for (int i = 0; i < length; i++) { iti = datai; }auto temp = data; data = it; delete\[\] temp; return; }//error//写法很危险------maxSize 在申请内存前就改了。如果 new 抛出异常,maxSize 已经是新值但内存没申请成功,对象处于不一致状态。

void expand() {

if (length >= maxSize)newSize = (maxSize == 0) ? 1 : 2 * maxSize; auto it = new TnewSize;

for (int i = 0; i < length; i++) {

iti = datai;

}

delete\[\] data; data = it; maxSize = newSize; return;

}

void push_front(const T&e) {

expand();

for (int i = length; i > 0; i--) {

datai = datai-1;

}

length++;

data0 = e;

}

void push_back(const T& e) {

expand();

datalength = e;//error

length++;

}

int find_return_index(const T& e) {

for (int i = 0; i < length; i++) {

if (datai == e) return i;

}

return -1;

}

void remove(int pos) {

if (pos<1 || pos>length) throw out_of_range("The position is out of range.");

for (int i = pos-1; i <length-1 ; i++) {

datai = datai + 1;

}

length--;

}

void show() {

for (int i = 0; i < length;i++)cout << datai << " ";

cout << endl;

}

};

int main() {

int a5 = { 2,3,4,5,6 };

SeqList<int> list(a, 5);

cout << "Length: " << list.getLength() << endl;

list.show();

cout << endl;

list.push_front(0);

cout << "Length: " << list.getLength() << endl;

list.show();

list.remove(2);

cout << "After removing position of 2,the list shows:" << endl;

list.show();

cout << "The position of 5 is " << list.find_return_index(5) << endl;

return 0;

}

相关推荐
H.莓飛1 小时前
【数据结构】排序总结与规模实测
linux·服务器·开发语言·数据结构·visual studio·1024程序员节
keyipatience1 小时前
Git学习(4)Git分布式优势与实战配置全解析
git·学习
码少女2 小时前
数据结构——B+树基础
数据结构·b树
青少儿编程课堂3 小时前
蓝桥杯青少组 + 全国青少年信息素养大赛 + 电子学会图形化考级|校园推箱子大挑战解析
c++·python·算法·bfs·信息学竞赛
多弗朗皮卡丘3 小时前
C++STL之空间配置器
开发语言·c++·内存池·空间配置器
牵猫散步的鱼儿3 小时前
Demosaic 和 Remosaic
c++
CodeYp20194 小时前
第19讲:封装红黑树实现 mymap 和 myset
开发语言·c++·红黑树
comyccccc4 小时前
STM32F407VET6核心板的部分电路绘制学习笔记
笔记·stm32·学习
叼烟扛炮4 小时前
Linux 学习笔记(三十六):并发进阶——读写锁 vs 自旋锁,到底怎么选?
linux·运维·服务器·笔记·学习
是隼人4 小时前
buuctf-pwn [极客大挑战 2019]Not Bad(orw+jmp)题解(学习过程持续更新)
学习·安全·pwn入门·ctf入门