双向链表插入第一个元素时引发了异常: 写入访问权限冲突。 p->llink 是 nullptr。
自己写的一个双向链表的模板类,在插入第一个元素时就出现了问题,希望大神帮忙看看哪里有问题(有可能时插入函数那里)
同时很困惑不知道插入第一个元素时应该怎么写
代码在下面
template<class T>class DoubleList;
template<class T>class Node
{
T element;
Node<T>* llink;
Node<T>* rlink;
friend DoubleList<T>;
};
template<class T>class DoubleList
{
public:
DoubleList();
DoubleList(const int& n);
~DoubleList();
Node<T>* Located(int n);
bool Insert(int n, T x);
bool Remove(int n);
int Length();
void Print();
void Clear();
private:
Node<T>* Head;
};
template<class T>
DoubleList<T>::DoubleList() { Head = new Node<T>; Head->llink = NULL; Head->rlink = NULL; }
template<class T>
DoubleList<T>::DoubleList(const int & n) { Head = new Node<T>(n); Head->llink = NULL; Head->rlink = NULL; }
template<class T>
DoubleList<T>::~DoubleList() { Clear(); }
template<class T>
Node<T>* DoubleList<T>::Located(int n)
{
Node<T>* p = Head;
int i = 0;
if (n < i) { cout << "定位出错" << endl; return NULL; }
while (p->llink != NULL&&i < n) { p = p->rlink; i++; }
return p;
}
template<class T>
bool DoubleList<T>::Insert(int n,T x)
{
Node<T>* p = Located(n);
if (p == NULL) { cout << "插入元素失败,请检查需要插入元素的位置" << endl; return false; }
Node<T>* q = new Node<T>;
if (q == NULL) { cout << "为新元素分配空间时失败" << endl; return false; }
q->element = x;
q->llink = p->llink;
q->rlink = p;
p->llink->rlink = q;
p->llink = q;
return true;
}
template<class T>
bool DoubleList<T>::Remove(int n)
{
Node<T>* p = Located(n);
if (p == NULL) { cout << "删除元素失败,该位置很可能没有元素" << endl; return false; }
p->llink->rlink = p->rlink;
p->rlink->llink = p->llink;
delete p;
return true;
}
template<class T>
int DoubleList<T>::Length()
{
int count = 0;//计数器
Node<T>* p = Head;
while(p->rlink!=NULL)
{
p = p->rlink;
count++;
}
return count;
}
template<class T>
void DoubleList<T>::Print()
{
Node<T>* p = Head;
while(p->rlink!=NULL)
{
p = p->rlink;
cout << p->element <<" "<<endl;
}
}
template<class T>
void DoubleList<T>::Clear()
{
Node<T>* p = NULL;
while(p!=NULL)
{
p = Head->rlink;
Head->rlink = p->rlink;
delete p;
}
}
同时很困惑不知道插入第一个元素时应该怎么写
代码在下面
template<class T>class DoubleList;
template<class T>class Node
{
T element;
Node<T>* llink;
Node<T>* rlink;
friend DoubleList<T>;
};
template<class T>class DoubleList
{
public:
DoubleList();
DoubleList(const int& n);
~DoubleList();
Node<T>* Located(int n);
bool Insert(int n, T x);
bool Remove(int n);
int Length();
void Print();
void Clear();
private:
Node<T>* Head;
};
template<class T>
DoubleList<T>::DoubleList() { Head = new Node<T>; Head->llink = NULL; Head->rlink = NULL; }
template<class T>
DoubleList<T>::DoubleList(const int & n) { Head = new Node<T>(n); Head->llink = NULL; Head->rlink = NULL; }
template<class T>
DoubleList<T>::~DoubleList() { Clear(); }
template<class T>
Node<T>* DoubleList<T>::Located(int n)
{
Node<T>* p = Head;
int i = 0;
if (n < i) { cout << "定位出错" << endl; return NULL; }
while (p->llink != NULL&&i < n) { p = p->rlink; i++; }
return p;
}
template<class T>
bool DoubleList<T>::Insert(int n,T x)
{
Node<T>* p = Located(n);
if (p == NULL) { cout << "插入元素失败,请检查需要插入元素的位置" << endl; return false; }
Node<T>* q = new Node<T>;
if (q == NULL) { cout << "为新元素分配空间时失败" << endl; return false; }
q->element = x;
q->llink = p->llink;
q->rlink = p;
p->llink->rlink = q;
p->llink = q;
return true;
}
template<class T>
bool DoubleList<T>::Remove(int n)
{
Node<T>* p = Located(n);
if (p == NULL) { cout << "删除元素失败,该位置很可能没有元素" << endl; return false; }
p->llink->rlink = p->rlink;
p->rlink->llink = p->llink;
delete p;
return true;
}
template<class T>
int DoubleList<T>::Length()
{
int count = 0;//计数器
Node<T>* p = Head;
while(p->rlink!=NULL)
{
p = p->rlink;
count++;
}
return count;
}
template<class T>
void DoubleList<T>::Print()
{
Node<T>* p = Head;
while(p->rlink!=NULL)
{
p = p->rlink;
cout << p->element <<" "<<endl;
}
}
template<class T>
void DoubleList<T>::Clear()
{
Node<T>* p = NULL;
while(p!=NULL)
{
p = Head->rlink;
Head->rlink = p->rlink;
delete p;
}
}
...全文
相关推荐
各种查找与排序算法-笔试面试必备
各种查找与排序算法,笔试面试过程中经常遇到的题目。。
哈希表设计
对你所在班级中的人名设计一个哈希表,完成响应的建表和查表程序。
指针相关知识的总结
指针的相关知识的总结,对于不理解指针的初学者可以看一看
C++题目汇总终稿 70.pdf
C++面试题目汇总
基于springboot+vue的摄影作品社区(项目源码)
基于springboot+vue的摄影作品社区(项目源码) 系统采用前后端分离的 B/S 架构。后端基于 Spring Boot 2.7.2 与 Java 1.8 构建,以 MyBatis-Plus 完成数据访问、PageHelper 实现分页,用 MySQL 8 存储业务数据;身份认证采用 Sa-Token 轻量级框架并整合 JWT 令牌,配合 Redis 实现图形验证码缓存与会话存储;前端使用 Vue 3 与 Naive UI 构建界面,由 Vite 完成构建,看板统计图表基于 ECharts 渲染。系统面向普通用户与管理员两类角色:用户端提供注册登录、作品上传与归档、多条件检索浏览、作品集管理、分类与标签管理、摄影圈专题、回收站、个人设置与公告查看等功能;管理端提供数据看板、用户管理与公告管理等功能。


