list求助
本帖最后由 青云风力 于 2016-6-17 14:20 编辑我自己写了一个链表,可是始终有一个bug,找不出来。
有bug,找不出来。
本帖最后由 青云风力 于 2016-6-17 14:20 编辑#pragma once
template <typename T>
class list_node {
typedef list_node<T>* link_type;
typedef list_node<T> node;
public:
link_type next = nullptr;
link_type prev = nullptr;
T data;
};
template<typename T>
class list {
typedef list_node<T>* link_type;
typedef list_node<T> node;
private:
link_type head = nullptr;
link_type tail = nullptr;
public:
list() {}
list(const T& value) {
link_type temp = (link_type)malloc(sizeof(node));
temp->data = value;
head = tail = temp;
}
list operator=(const list L){}
~list(){}
void push_front(const T& value) {
link_type temp = (link_type)malloc(sizeof(node));
temp->data = value;
temp->next = head;
head->prev = temp;
head = temp;
}
void push_back(const T& value) {
link_type temp = (link_type) malloc(sizeof(node));
temp->data = value;
tail->next = temp;
temp->prev = tail;
tail = temp;
}
void pop_front() {
link_type temp = head;
head = head->next;
delete *temp;
}
void pop_back() {
link_type temp = tail;
tail = tail->prev;
delete *temp;
}
bool replace(T& x, const T& value) {
link_type temp = head;
while (temp->data != x && temp->next != nullptr) {
temp = temp->next;
}
if (temp->data == x) {
temp->data = value;
return true;
}
if (temp->next == nullptr)
return false;
}
void erase(T& value) {
for (link_type temp = head; temp != tail; temp = temp->next) {
if (temp->data == value){
link_type x = temp;
temp->prev = (temp->next)->prev;
temp->next = (temp->prev)->next;
delete *x;
}
}
}
void clean() {
while (head != tail) {
link_type temp = head;
head = head->next;
delete *temp;
}
if (head == tail) {
delete *head;
}
}
void PRINT() {
for (link_type temp = head; temp != tail; temp = temp->next)
std::cout << temp->data << std::endl;
}
}; 我来看看 求问list怎么转为矩阵格式
页:
[1]