奇怪的C++错误(求高人。。。)。。。

helloxxxxx111 2009-08-15 09:22:03
才学数据结构,编单链表的时候,编一个将两个单链表链接在一起的函数。。。

编完了能通过编译,但是执行的时候。。。奇怪的问题发生了。。。

整个程序能跑完,但是不是正常完成,跑完最后一句就卡死了(像死循环那种,不显示按任意键继续)。。。但是只要把链接两个单链表那句去掉,又能正常跑完(显示按任意键继续)。。。


代码如下(有问题那句就是被注释了那句):

#define NULL 0
#include <iostream>
using namespace std;

template <class T> class LinList;
template <class T> class ListNode;
template <class T>
void Concatenate(LinList<T>& a, LinList<T>& b);


template <class T>
class ListNode
{
friend class LinList<T>;
friend void Concatenate<T>(LinList<T>& a, LinList<T>& b);
public:
T data;

ListNode(ListNode<T> * ptrNext = NULL);
ListNode(const T& item,ListNode<T> * ptrNext = NULL);
~ListNode(){}

private:
ListNode<T> * next;

};

template<class T>
ListNode<T>::ListNode(ListNode<T> * ptrNext /* = NULL */):next(ptrNext)
{

}

template<class T>
ListNode<T>::ListNode(const T& item,ListNode<T> * ptrNext /* = NULL */)
{
data = item;
next = ptrNext;
}


template <class T>
class LinList
{
friend void Concatenate<T>(LinList<T>& a, LinList<T>& b);
public:
LinList(void);
~LinList(void);


int ListSize(void) const;
ListNode<T> * Index(int pos);
void Insert(const T&item ,int pos);
T Delete(int pos);
T GetData(int pos);
int ListEmpty(void) const;
void ClearList(void);


ListNode<T> * Reset(int pos = 0);
ListNode<T> * Next(void);
int EndOfList(void) const;
ListNode<T> * CurrentP(void);
private:
ListNode<T> * head;
ListNode<T> * currPtr;
int size;
};

template <class T>
LinList<T>::LinList()
{
head = new ListNode<T>();
size = 0;
}
template <class T>
LinList<T>::~LinList(void)
{
ClearList();
delete head;
}


template <class T>
int LinList<T>::ListSize(void) const
{
return size;
}
template <class T>
int LinList<T>::ListEmpty(void) const
{
if (size <= 0)
{
return 1;
}
else return 0;
}
template <class T>
ListNode<T> * LinList<T>::Index(int pos)
{
if ( pos < -1 || pos >size )
{
cout<<"pos越界"<<endl;
exit(0);
}
if ( pos == -1)
{
return head;
}
ListNode<T> * p = head->next;
int i = 0;
while(p != NULL && i<pos)
{
p = p->next;
i++;
}
return p;
}
template <class T>
void LinList<T>::Insert(const T &item, int pos)
{
if (pos < 0 || pos > size)
{
cout<<"pos越界"<<endl;
exit(0);
}

ListNode<T> * p = Index(pos - 1);

ListNode<T> * newNode =new ListNode<T>(item ,p->next);
p->next = newNode;
size++;
}

template <class T>
T LinList<T>::Delete(int pos)
{
if (pos < 0 || pos > size-1 )
{
cout<<"pos越界"<<endl;
exit(0);
}

ListNode<T> * q, * p =Index(pos - 1);
q = p->next;
p->next=p->next->next;
T data = q->data;
delete q;
size--;
return data;
}

template <class T>
T LinList<T>::GetData(int pos)
{
if (pos < 0 || pos > size-1 )
{
cout<<"pos越界"<<endl;
exit(0);
}

ListNode<T> * p = Index(pos);
return p->data;
}

template <class T>
void LinList<T>::ClearList(void)
{
ListNode<T> * p, * p1;

p = head->next;
while(p != NULL)
{
p1 = p;
p = p->next;
delete p1;
}
size = 0;
}

template <class T>
ListNode<T> * LinList<T>::Reset(int pos)
{
if (head == NULL)
{
return NULL;
}

if (pos < -1 || pos >= size)
{
cout<<"参数出错"<<endl;
exit(0);
}
if (pos == -1) return head;
if (pos == 0) currPtr = head->next;
else
{
currPtr = head->next;
ListNode<T> prevPtr = head;
int i;
for (i = 0;i < pos;i++)
{
prevPtr = currPtr;
currPtr = currPtr->next;
}
}
return currPtr;
}

template <class T>
ListNode<T> * LinList<T>::Next(void)
{
if(currPtr != NULL) currPtr = currPtr->next;
return currPtr;
}

template <class T>
int LinList<T>::EndOfList(void) const
{
if (currPtr == NULL)
{
return 1;
}
else return 0;
}


template <class T>
ListNode<T> * LinList<T>::CurrentP(void)
{
return currPtr;
}
template <class T>
void Concatenate(LinList<T>& a, LinList<T>& b)
{
ListNode<T> * a_tailer = a.Index(a.ListSize() - 1);
a_tailer->next = b.Index(0);
a.size = a.size+b.size;
}

int main()
{
LinList<int> a;
LinList<int> b;
int n,m;
cin>>n;
int i;
int temp;
for(i = 0;i < n ;i++)
{
cin>>temp;
a.Insert(temp,i);
}
cin>>m;
for (i = 0;i < m;i++)
{
cin>>temp;
b.Insert(temp,i);
}
//Concatenate(a,b);
cout<<"fuck"<<endl;
return 0;
}
...全文
154 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
helloxxxxx111 2009-08-16
  • 打赏
  • 举报
回复
楼上高人谢了!
lsm164 2009-08-16
  • 打赏
  • 举报
回复

int main()
{
LinList <int> a;
LinList <int> b;
int n,m;
cin>>n;
int i;
int temp;
for(i = 0;i < n ;i++)
{
cin>>temp;
a.Insert(temp,i);
}
cin>>m;
for (i = 0;i < m;i++)
{
cin>>temp;
b.Insert(temp,i);
}
//连接后,a有m+n个元素,b有m个元素,
//在程序退出前,a的析构函数会释放掉m+n个元素,
//这其中已经把原来b中的m个元素释放掉了。
//接着b的析构函数会再次释放那m个元素,这时候就会出现重复释放的问题。
//不知道这样解释是否清楚了。。。
Concatenate(a,b);
cout < <"fuck" < <endl;
return 0;
}

helloxxxxx111 2009-08-16
  • 打赏
  • 举报
回复
楼上我不太懂你的话。。。

连接的时候我让a链表的最后一个结点的next指向b的头结点的下一个结点的地址。。。没做其他处理。。。

执行的时候链接是成功的(能从a的头结点访问到链接后a的所有元素)。

异常也不是出现啥子内存不能读写,就是像死循环那种停那里了。。。

谢谢大家哈

为了AC这道题后来我是让a用insert方法直接插入b的元素,可以过,但是为啥子直接连接就有问题呢。
lsm164 2009-08-15
  • 打赏
  • 举报
回复
应该是重复释放内存的原因吧,连接的时候,没有使用new方法创建节点,导致a和b都释放b中的元素,然后就异常了。
helloxxxxx111 2009-08-15
  • 打赏
  • 举报
回复
提问的艺术我看过。。。这已经是最简了。。。


问题我尽量重新描述一遍描述清楚:

写一个单链表,一个函数把两个单链表连在一起,那个函数貌似有点小问题。能让两个单链表连在一起,但是执行的时候,只要代码中执行了这个有问题的函数,程序就无法正常结束(但是能跑完所有代码,只是无法结束,就是不显示出按任意键继续,只有按CTRL+C结束)。

有问题的函数就是Concatenate(a,b)这个。。。
niimp2 2009-08-15
  • 打赏
  • 举报
回复
提个建议:
你提问的方式没有在重点 ,代码太冗长 ,很少有人耐着性子能看完的
最好提问的简练一些。 哈,这也是别人告诉我的。建议修改下.
yang_e_2009 2009-08-15
  • 打赏
  • 举报
回复
断点调试试试吧
helloxxxxx111 2009-08-15
  • 打赏
  • 举报
回复
奇怪就在于能过编译

而且他能跑完疑似有错代码的下一句。。。神吧。。。
helloxxxxx111 2009-08-15
  • 打赏
  • 举报
回复
有问题的代码即最后导数第四排被注释

不好意思代码没有注释段,但愿大家能看懂,谢谢

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧