单链表push_front不会重新分配头部
Singly linked list push_front won't reassign head
我无法使push_front
正常工作。看来new_head->next = head
不会正确地将new_head和头部联系起来。我的节点类是通常的节点类,其中next
是一个节点指针。我的push_back
功能工作正常。我不明白为什么head
不会链接到new_head
.
template <class T> class mylist {
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef mynode<T>* iterator;
typedef size_t size_type;
mylist() { create(); }
mylist(size_type n, const T& val = T()) { create(n, val); }
~mylist() { uncreate(); }
iterator begin() { return head; }
iterator end() { return head + size(); }
size_type size();
void push_back(const T&);
void push_front(const T&);
void pop_back();
void pop_front();
private:
std::allocator<mynode<T>> alloc;
mynode<T>* head;
mynode<T>* tail;
void create() { head = tail = 0; }
void create(size_type, const T&);
void uncreate();
};
template <class T> void mylist<T>::push_front(const T& t)
{
iterator new_head = alloc.allocate(1);
new_head->data = t;
new_head->next = head;
head = new_head;
if(tail == 0)
tail = head;
}
template <class T> void mylist<T>::push_back(const T& t)
{
tail->next = alloc.allocate(1);
tail = tail->next;
tail->data = t;
tail->next = 0;
}
MAIN
#include "mylist.h"
int main()
{
mylist<int> lst(5, 2);
lst.push_back(22);
mylist<int>::iterator temp = lst.begin();
while(temp != lst.end()) {
std::cout << **temp << " ";
temp++;
}
std::cout << std::endl;
std::cout << "list size: " << lst.size() << std::endl;
lst.push_front(80);
lst.push_back(7);
lst.push_back(3);
mylist<int>::iterator inc = lst.begin();
while(inc != lst.end()) {
std::cout << **inc << " ";
inc++;
}
std::cout << std::endl;
std::cout << "list size: " << lst.size() << std::endl;
}
OUTPUT
2 2 2 2 2 22
list size: 6
80 7 3
list size: 3
EDIT
template <class T> void mylist<T>::create(size_type n, const T& t)
{
head = alloc.allocate(1);
head->data = t;
head->next = alloc.allocate(1);
mynode<T>* next = head->next;
int i = 0;
while(i != n-2) {
next->data = t;
next->next = alloc.allocate(1);
next = next->next;
++i;
}
tail = next;
tail->data = t;
tail->next = 0;
}
class mynode
template <class T> class mynode {
public:
mynode() : next(0) {}
mynode(T a, mynode* n = 0) : data(a), next(n) {}
mynode* next;
mynode* operator++() { return this->next; }
mynode* operator+(size_t n) {
size_t cnt = 0;
mynode* count = this;
while(cnt != n) {
++cnt;
count = count->next;
}
return count;
}
T& operator*() { return this->data; }
T data;
};
Example using operator+
#include "mylist.h"
int main()
{
mylist<int> test(5,5);
mylist<int>::iterator n = test.begin();
while(n != test.end()) {
std::cout << **n << " ";
n = n + 1;
}
std::cout << std::endl;
mylist<int> test2(6,6);
mylist<int>::iterator m = test2.begin();
while(m != test2.end() - 1) {
std::cout << **m << " ";
m = m->next;
if(m == test2.end()-1)
std::cout << **m;
}
std::cout << std::endl;
}
Output from example
5 5 5 5 5
6 6 6 6 6 6
似乎push_back工作不正确。按以下方式更改它
template <class T> void mylist<T>::push_back(const T& t)
{
mynode<T>* new_tail = alloc.allocate(1);
new_tail->data = t;
new_tail->next = 0;
if ( tail == 0 )
{
head = new_tail;
}
else
{
tail->next = new_tail;
}
tail = new_tail;
}
相关文章:
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 为模板参数类型中的新对象分配内存
- 新运算符分配的大小大于声明的大小.为什么
- 在C++中分配分配
- 为什么支撑初始化分配分配填充垃圾变量
- C 新操作员分配新内存
- 新不分配内存?
- 如果我的C++“新”内存分配失败,如何找出返回值
- 我可以用新的分配内存块吗?
- COM / DCOM:服务器存根不会为现有接口中的新方法分配内存
- 在线程在 C++ 中完成后将新任务分配给线程
- 只能使用CUDA中的新运算符分配有限的内存
- 与将新内容分配给向量的指针斗争
- 确定C++中新运算符分配的内存大小
- 按新内存分配
- 是否有必要使用 'new' 将新指针分配给与指向同一结构的先前存在的指针相同的结构?如果是这样,为什么?
- 用于新运算符分配的公共内存
- boost::io_service post方法是否引起新的分配?
- 如何在C++中检索由新运算符分配的对象的地址