程序调试的问题
编了个程序,代码如下:
/*
s_linklist.h
项目名称:顺序链表
创建时间:2007.10.18
在单链表的基础上,修改了插入函数使其有排序的功能
*/
#ifndef S_LINKLIST_H
#define S_LINKLIST_H
#include<iostream.h>
#include<stdlib.h>
template<class T>class s_linklist;
template<class T>class node
{
friend class s_linklist<T>;
private:
node<T>*next;
public:
T data;
node(node<T>*pnext=NULL);
node(const T&item,node<T>*pnext=NULL);
};
template<class T>class s_linklist
{
private:
node<T>*head;
node<T>*pc;
public:
s_linklist();
~s_linklist();
T GetCurrent();
int EndOfList();
node<T>* Locate(const T&item);
void display();
void Insert(const T&item);
void Delete(const T&item);
};
template<class T>node<T>::node(node<T>*pnext)
{
next=pnext;
}
template<class T>node<T>::node(const T&item,node<T>*pnext)
{
data=item;
next=pnext;
}
template<class T>s_linklist<T>::s_linklist()
{
head=pc=new node<T>();
head->next=NULL;
}
template<class T>s_linklist<T>::~s_linklist()
{
node<T>*p,*q;
p=head->next;
while(p!=NULL)
{
q=p;
p=p->next;
delete q;
}
delete head;
}
template<class T>T s_linklist<T>::GetCurrent()
{
if(pc==head||pc==NULL)
{
cerr<<"未取到数据"<<endl;
exit(1);
}
return pc->data;
}
template<class T>int s_linklist<T>::EndOfList()
{
if(pc==NULL)
return 1;
else
return 0;
}
template<class T> node<T>* s_linklist<T>::Locate(const T&item)
{
pc=head->next;
while(pc!=NULL)
{
if(pc->data==item)
break;
else
pc=pc->next;
}
return pc;
}
template<class T>void s_linklist<T>::display()
{
node<T>* p=head->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
template<class T> void s_linklist<T>::Insert(const T&item)
{
if(head->next==NULL)
{
node<T>*newnode=new node<T>(item,NULL);
head->next=pc=newnode;
}
else
{
pc=head->next;
while(pc->data<item)
{
if(pc->next==NULL)
{
node<T>*newnode=new node<T>(item,NULL);
pc->next=newnode;
return;
}
pc=pc->next;
}
node<T>* newnode=new node<T>(item,pc);
node<T>*p=head;
while(p->next!=pc)
p=p->next;
p->next=newnode;
pc=newnode;
}
}
template<class T>void s_linklist<T>::Delete(const T&item)
{
if(pc==head||pc==NULL)
{
cerr<<"不能删除"<<endl;
exit(1);
}
node<T>*p,*q=head;
p=Locate(item);
while(q->next!=p)
q=q->next;
q->next=p->next;
delete p;
pc=q->next;
}
#endif
//main.cpp
#include"s_linklist.h"
void com_two(s_linklist<int>,s_linklist<int>);//用于两链表的合并
void main()
{
s_linklist<int>mylist1,mylist2;
int temp,n=5;//n用于存储输入链表的个数,temp用于存储输入的数
cout<<"输入第一组整数:";
for(int i=0;i<n;i++)
{
cin>>temp;
mylist1.Insert(temp);
}
cout<<"输出的排序单链表应为:";
mylist1.display();
cout<<endl;
cout<<"输入第二组数据:";
for(i=0;i<n;i++)
{
cin>>temp;
mylist2.Insert(temp);
}
cout<<"输出的排序单链表应为:";
mylist2.display();
com_two(mylist1,mylist2);
cout<<endl;
cout<<"合并两个单链表,输出排好序的结果应为:";
mylist2.display();
}
void com_two(s_linklist<int>a,s_linklist<int>b)
{
do
{
int c=a.GetCurrent();
b.Insert(c);
}while(!a.EndOfList());
}
如果在main函数中不加com_two函数(用来和并两链表的)程序没什么问题.但加了,程序运行到一半就死在那了.望高手指导,谢谢
[解决办法]
a.GetCurrent();
之后,a里的指针pc有没有往下移动?
[解决办法]
void com_two(s_linklist <int >a,s_linklist <int >b)
{
do
{
int c=a.GetCurrent();
b.Insert(c);
}while(!a.EndOfList()); //你这个是不是有问题啊,C得到的永远是一个值
}
GetCurrent() 函数先保存当前的值,再让指针指向下一个元素,最后返回保存的数据
[解决办法]
输入数据 :3 6 9 8 7 4 1 2 5
只输出: 3 6 7 8 9
说明你的Insert函数是错的.