J海阔天空 2016-05-17 08:04 采纳率: 0%
浏览 1444

请问我的list为什么popback会不好使,请麻烦各位帮帮忙,谢谢

#include
using namespace std;
template
struct list_node
{
list_node* next; // 指向下一个节点的指针
list_node* prev; // 指向前一个节点的指针
T data; //list 数据

};

template
class list;

template
class list_iterator
{

public:
friend class list;

private:
typedef list_node* link_type;
list_node* node;//数据成员 ,为一个节点的指针。

//比较iterator是否相等

public:
list_iterator()
{

}

list_iterator(list_node<T>* p)
{
     node = p ;
}

int operator==(const list_iterator<T>& x) const
 { 
     return node == x.node;
 }
int operator!=(const list_iterator<T>& x) const 
 {
     return node != x.node; 
 }

//*操作符,使得访问iterator就像访问一个指针
T& operator*() const
{
return (*node).data;
}
//自增操作符. ++
list_iterator& operator++()
{
node = (link_type)((*node).next); //使iterator包含的是下一个节点
return *this;
}
//自减操作符 --
list_iterator& operator--()
{
node = (link_type)((*node).prev);
return *this;
}
};

template
class list
{

public:
typedef list_iterator iterator;
typedef list_node* link_type;
private:
link_type node;
link_type node_head;

link_type node_end;
link_type node_tend;
size_t length; //list的长度。
public:

//默认构造函数,创建一个空的list
list()
{
length = 0;
node_end = node_head = NULL;
node = node_end;

}

~list()
{
    this->clear();
}

public:
iterator begin()
{
list_iterator p_ite;
p_ite = node_head;
return p_ite;

}
//返回list的末节点。注意,末节点是最后一个元素的下一个节点。末节点不包含元素的值
iterator end()
{
list_iterator p_ite;
p_ite = node_end->next;
return p_ite;
}
//判断是否为空
bool empty() const
{
return length == 0;
}
// 返回list的长度
size_t size() const
{
return length;
}
//返回list的头部 值引用
T& front()
{
return begin();
}
//返回list的末尾 值的引用
T& back()
{
return *(--end());
}
//在指定iterator之前插入新结点
void insert(list_iterator position,int n, const T& x)//insert(插入的位置,插入的个数,插入的数值)
{
for(int i=0;i {
list_node
temp = new list_node;
temp->data = x;
temp->next = position.node;
position.node->prev->next = temp;
temp->prev = position.node->prev;
position.node->prev = temp;
position.node = temp;
}
}
// 在头部插入一个元素
void push_front(const T& x)
{
insert(begin(),1,x);
}
//在尾部插入一个元素
void push_back(const T& x)
{
list_node* temp = new list_node;
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
//如果链表为空
if(node_head == NULL)
{
node_head = node_end = temp;
}
//若果有多个节点
else
{
node_end->next = temp;
temp->prev = node_end;
}
node_end = temp;
length++;
}
// 删除一个结点
iterator erase(list_iterator position)
{
link_type next_node = (link_type)position.node->next;
link_type prev_node = (link_type)position.node->prev;
prev_node->next = next_node;
next_node->prev = prev_node;
delete(position.node);
--length; //长度减1
return iterator(next_node);
}
// 清空一个list的所有元素
void clear()
{
list_node* temp = node_head;
list_node* deltemp = NULL;
while(temp != NULL)
{
if(temp->next == NULL)
{
delete temp;
temp = NULL;
break ;
}
deltemp = temp;
temp = temp->next;
delete deltemp;
deltemp = NULL;

}
length = 0;
node_end = node_head = NULL;
node = node_end;
}
//删除头部元素
void pop_front()
{
erase(begin());
}
//删除尾部元素
void pop_back()
{
iterator temp = end();
erase(temp);
}
list(int a)
{
length = 0;
node_end = node_head = NULL;
node = node_end;
for(int i=0;i<a;i++)
{
push_back(0);
}
}
list(int a,int b)
{
length = 0;
node_end = node_head = NULL;
node = node_end;
for(int i=0;i<a;i++)
{
push_back(b);
}
}
};

  • 写回答

1条回答 默认 最新

  • 小灸舞 2016-05-17 08:30
    关注

    崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。

    代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
    提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
    单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器