奇怪的C++错误(求高人。。。)。。。
才学数据结构,编单链表的时候,编一个将两个单链表链接在一起的函数。。。
编完了能通过编译,但是执行的时候。。。奇怪的问题发生了。。。
整个程序能跑完,但是不是正常完成,跑完最后一句就卡死了(像死循环那种,不显示按任意键继续)。。。但是只要把链接两个单链表那句去掉,又能正常跑完(显示按任意键继续)。。。
代码如下(有问题那句就是被注释了那句):
#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;
}