C++ 在链表c+中的某个位置插入节点+;
我有一个链表,我需要在“迭代器”所在的位置插入一个节点 下面是插入函数(我已经制作了模板):C++ 在链表c+中的某个位置插入节点+;,c++,insert,doubly-linked-list,C++,Insert,Doubly Linked List,我有一个链表,我需要在“迭代器”所在的位置插入一个节点 下面是插入函数(我已经制作了模板): 当我调用insert函数时,我遇到了问题。滚动函数工作正常。迭代器->下一个=迭代器不正确,会创建循环。由于上一行是temp->previous=iterator,iterator->next应该指向temp。更新两个链接后,对于列表中的任何有效节点,都应该有n->next->prev==n template <class T> void List<T>::ins
当我调用insert函数时,我遇到了问题。滚动函数工作正常。
迭代器->下一个=迭代器
不正确,会创建循环。由于上一行是temp->previous=iterator
,iterator->next
应该指向temp。更新两个链接后,对于列表中的任何有效节点,都应该有n->next->prev==n
template <class T>
void List<T>::insert(T x)
{
if (size=0)
{
cout << "adding postion to head because empty list" << endl;
NodeRef newNode = new Node(x);
tail = head = newNode;
}
else
{
NodeRef temp = new Node(x);
temp->previous = iterator;
iterator->next = iterator;
}
}
class List
{
private:
struct Node
{
T data;
Node* next;
Node* previous;
Node() : next(NULL), previous(NULL) {} //define our own default constuctor
Node(T data) : next(NULL), previous(NULL), data(data) {}
};
typedef struct Node* NodeRef;
NodeRef head;
NodeRef tail;
NodeRef iterator; //points to one node at a time
NodeRef current1;//temp
int size;
public:
void insert(T);Inserts a new element into the list in the position after the "iterator"
void scroll() {iterator = iterator->next;}