我正在通过我的双向链接列表寻求一般建议和编码帮助。任何带有注释的编码更正将不胜感激。
我不太熟悉指针的使用,确实需要一些帮助。
#pragma once
#ifndef LIST_H
#define LIST_H
#include <cstddef>
template <class Type>
class List
{
public:
//constructor of List class
List()
{
//Head and Tail are assigned Null values when the Object is constructed in Memory
Head = nullptr;
Tail = nullptr;
}
~List()
{
}
{
NODE *current = new NODE();
current->Data = Data;
if (Head == NULL)
{
Head = current;
Tail = current;
}
else
{
Tail->next = current;
current->previous = Tail;
Tail = current;
}
}
void find(Type Value)
{
NODE * current = Head;
while (current != nullptr && current->Data != Value)
{
current = current->next;
}
}
void Delete(Type value)
{
NODE * Delete_Node = find(value);
if (*Head == Delete_Node)
{
Delete_Node->next->previous = nullptr;
Head = Delete_Node->next;
}
else if (*Tail == Delete_Node)
{
Delete_Node->previous->next = nullptr;
Tail = Delete_Node->previous;
}
else if (*Tail == Delete_Node && *Head == Delete_Node)
{
Head = nullptr;
Tail = nullptr;
}
else
{
Delete_Node->next->previous = Delete_Node->previous;
Delete_Node->previous->next = Delete_Node->next;
}
delete Delete_Node;
}
void display() {
NODE* pointer;
pointer = Head;
while (pointer != nullptr)
{
std::cout << pointer->Data << " ";
pointer = pointer->next;
}
}
private:
struct NODE
{
Type Data;
NODE *next;
NODE *previous;
NODE()
{
Data = NULL;
next = nullptr;
previous = nullptr;
}
};
NODE* Head;
NODE* Tail;
};
#endif
主文件。
#include "List.h"
#include <iostream>
int main()
{
List<int> My_List;
My_List.Insert(1);
My_List.Insert(2);
My_List.Insert(3);
My_List.Insert(4);
My_List.display();
My_List.Delete(3);
My_List.display();
My_List.Delete(4);
My_List.display();
My_List.Delete(1);
My_List.display();
My_List.Delete(2);
}
我正在测试插入和删除功能。我不确定编码不正确的地方。这些是我收到的错误。
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'List<int>::NODE' (or there is no acceptable conversion) list.h 66
Error C2440 'initializing': cannot convert from 'void' to 'List<int>::NODE *' list.h 55
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'List<int>::NODE' (or there is no acceptable conversion) list.h 56
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'List<int>::NODE' (or there is no acceptable conversion) list.h 61