qq_36204385
Joker_Jay
2017-11-21 11:25
采纳率: 100%
浏览 847

请问这个是什么原因啊,访问冲突问题.

#include
using namespace std;
struct Node{
int data;
Node next;
};
class LinkSqlist{
Node *Head;
public:
LinkSqlist();
~LinkSqlist();
void CreateList(int m);
void InsertList(int m, int n);
void DeleteList(int m);
void ShowList();
};
LinkSqlist::LinkSqlist(){
Head = new Node;
Head->next = NULL;
}
LinkSqlist::~LinkSqlist()
{
Node *p;
while (Head)
{
p = Head;
Head = Head->next;
delete p;
}
Head = NULL;
}
void LinkSqlist::CreateList(int m){
Node *p, *q;
p = Head;
cout << "依次输入" << m << "个元素" << endl;
for (int i = 0; i < m; i++)
{
q = new Node;
cin >> q->data;
q->next = p->next;
p->next = q;
p = q;
}
}
void LinkSqlist::InsertList(int m,int n)
{
Node *p;
int q=0;
p = Head;
while (p&&q < m - 1){
p = p->next;
q++;
}
if (!p&&q>m - 1)
cout << "error!!!please checking !" << endl;
else
{
Node *t;
t = new Node;
t->next = p->next;
p->next = t;
t->data = n;
}
}
void LinkSqlist::DeleteList(int m)
{
int j=0;
int k;
Node *p;
p = Head;
Node *q;
while (p&&j < m - 1)
{
p = p->next;
j++;
}
q = p->next;
k = q->data;
cout << "要删除的结点值为:" << k << endl;
p->next = q->next;
delete q;
}
void LinkSqlist::ShowList()
{
Node*q=Head->next;
while (Head)
{
cout << q->data << endl;
q = q->next;
}
}
int main()
{
LinkSqlist ly;
ly.CreateList(3);
ly.ShowList();
/*ly.InsertList(2, 8);
ly.ShowList();
ly.DeleteList(1);
ly.ShowList();
/
return 0;
}
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • superliu1122
    superliu1122 2017-11-21 16:08
    已采纳
     void LinkSqlist::ShowList()
     {
     Node*q=Head->next;
     while (Head)
     {
     cout << q->data << endl;
     q = q->next;
     }
     }
    

    其他没仔细看,这个while的判断条件写错了,应该用while(q)

    点赞 评论
  • ranyixu11
    doggy_coder 2017-11-21 12:54

    内存访问错误。你看一下你的代码有没有对空指针进行了访问

    点赞 评论

相关推荐