将链表更改为双向链表
changing linked list into a doubly linked list
嗨,你能帮我把这个链表改成双链表吗?我将非常感谢:)
#include <iostream>
using namespace std;
template <class T>
struct node
{
T data;
node<T> *next;
node<T> *prev;
};
template <class T>
class Container
{
public:
//constructs a new empty Kontener
Container()
{
head = new node<T>;
head->next = head;
head->prev = head;
};
构造一个新jp_list,它是现有列表的副本
Container(const Container<T>& rt_side)
{
head = new node<T>;
head->next = head;
head->prev = head;
node<T> *crt_ptr = rt_side.head->next;
while(crt_ptr != rt_side.head)
{
push_back(crt_ptr->data);
crt_ptr = crt_ptr->next;
}
};
//adds a data node to the front of the list
void push_front(T nw_data)
{
node<T> *temp = new node<T>;
temp->data = nw_data;
temp->next = head->next;
head->next->prev = temp;
temp->prev = head;
head->next = temp;
};
//adds a data node to the end of the list
void push_back(T nw_data)
{
node<T> *temp = new node<T>;
temp->data = nw_data;
head->prev->next = temp;
temp->prev = head->prev;
temp->next = head;
head->prev = temp;
};
//removes the first node and returns the data
T pop_front()
{
node<T> *temp = head->next;
T temp_data = head->next->data;
head->next = temp->next;
temp->next->prev = head;
delete temp;
return temp_data;
};
//removes the last node and returns the data
T pop_back()
{
node<T> *temp = head->prev;
T temp_data = head->prev->data;
head->prev = temp->prev;
temp->prev->next = head;
delete temp;
return temp_data;
};
//resturns the size of the list
int size()
{
int size = 0;
node<T> *crt_ptr; //pointer to current node
crt_ptr = head->next;
while(crt_ptr != head)
{
size += 1;
crt_ptr = crt_ptr->next; //advance to the next node then loop
}
return size;
};
//prints out all the data in the list
void display_all()
{
node<T> *crt_ptr = head->next;
for(int i = 0; crt_ptr != head; i++)
{
cout << "Node " << (i+1) << ": " << crt_ptr->data << endl;
crt_ptr = crt_ptr->next;
}
};
Container& operator= (const Container& rt_side)
{
if(this == &rt_side)
return *this;
node<T> *crt_ptr = head->next;
//empty this list so the rt_side can be coppied in
while(crt_ptr != head)
{
crt_ptr = crt_ptr->next;
pop_front();
}
crt_ptr = rt_side.head->next;
while(crt_ptr != rt_side.head)
{
push_back(crt_ptr->data);
crt_ptr = crt_ptr->next;
}
return *this;
};
virtual ~Container()
{
int list_size = size();
for(int i = 0; i < list_size; i++)
{
pop_front();
}
delete head;
};
private:
node<T> *head;
};
#endif
我只是一个初学者,所以请帮助我:)
尾部将始终指向插入到列表中的最后一项。
但是,我不认为拥有尾部指针就一定使它成为双向链表。单向链表也可以有一个尾指针(无论它可能多么无用)。我相信您要求创建一个双端双向链表。
您已经有下一个和上一个指针来启用双链接。您所要做的就是,当您将某些内容推入列表时,您需要使尾部指针指向正在添加的节点。同样,在删除节点时,在删除最后一个节点之前,需要尾部指针指向尾部的前一个 U。
*更新*下面是一些代码。我假设一个具有两端的双端双向链表。
void push_front(T nw_data)
{
node<T> *temp = new node<T>;
temp->data = nw_data;
if(head == nullptr)
{
head = temp;
tail = temp;
}
else if(head == tail)
{
head->next = temp;
temp->prev = head;
tail = temp;
}
else
{
temp->next = head->next;
head->next->prev = temp;
temp->prev = head;
head->next = temp;
}
};
//adds a data node to the end of the list
void push_back(T nw_data)
{
node<T> *temp = new node<T>;
temp->data = nw_data;
if(head == nullptr)
{
head = temp;
tail = temp;
}
else if(head == tail)
{
head->next = temp;
temp->prev = head;
tail = temp;
}
else
{
temp->prev = tail;
tail->next = temp;
tail = temp;
}
};
T pop_back()
{
node<T> *temp = tail;
T temp_data = tail->data;
tail = tail->prev;
tail->next = null;
delete temp;
return temp_data;
};
* 更新 * 复制构造函数在你的复制构造函数中,如果push_back设置了尾部,那么你需要做的就是像你一样push_back节点。head->next = head,head->prev = head 使链表具有周期性。
Container(const Container<T>& rt_side)
{
this->head = rt_side.head;
node<T> * crt_ptr = rt_side.head->next;
while (crt_ptr != null)
{
push_back(crt_ptr->data);
crt_ptr = crt_ptr->next;
}
};
相关文章:
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 如何实现容纳整数和无效指针的双向链表?
- 为什么我的双向链表删除函数会删除多个节点?
- 我对数据结构、双向链表有一些问题
- C++ 双向链表:使用矩阵类成员创建和填充列表
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 在 c++ 中具有向量的双向链表构造函数
- 如何在双向链表上实现复制赋值?
- 在C++中删除双向链表的头节点后出现访问冲突异常
- 在双向链表中实现 Popback
- 在C++中反转双向链表
- 双向链表 - 无法删除第一个节点
- 我当前实现的双向链表类是否需要重构迭代器 end() 功能?
- 双向链表 std::unique_ptr 类在节点删除时无法按预期工作
- C++ 双向链表 - 插入同时保持递增顺序
- 如何找到双向链表的最大元素?
- 如何实现双向链表的迭代器?
- 双向链表问题 - 包含项目但不显示
- 双向链表未正确打印值
- 哈希表 - 链表数组 - C++