首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

来帮帮忙!

2012-02-21 
高手进来帮帮忙!!//结点类templateclass T class LinListtemplateclass Tclass CirListtemplatecla

高手进来帮帮忙!!
//结点类
template<class T> class LinList;
template<class T>class CirList;
template<class T>
class ListNode
{
friend class LinList<T>;
friend class CirList<T>;
private:
ListNode<T> *next;
public:
T data;
ListNode(ListNode<T> *ptrNext=NULL);
ListNode(const T&item,ListNode<T> *ptrNext=NULL);
~ListNode(){};
};

template<class T>
ListNode<T>::ListNode(ListNode<T>*ptrNext):next(ptrNext)
{}

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

//链表类
#include"ListNode.h"

template<class T>
class LinList
{
private:
ListNode<T>*head;
int size;
ListNode<T>*currPtr;
public:
LinList();
~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;
void Receive();
};

template<class T>
LinList<T>::LinList(void)
{
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越界出错1!"<<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越界出错2!"<<endl;
exit(0);
}

ListNode<T>*p=Index(pos-1);
ListNode<T>*newListNode=new ListNode<T>(item,p->next);
p->next=newListNode;
size++;
}

template<class T>
T LinList<T>::Delete(int pos)
{
if(pos<0||pos>size-1)
{
cout<<"参数pos越界出错3!"<<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<<"参数越界出错4!"<<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<<"参数出错5!"<<endl;
exit(0);
}
if(pos==-1)return head;
if(pos==0)currPtr=head->next;
else
{
currPtr=head->next;
ListNode<T>prevPtr=head;
for(int 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>
void LinList<T>::Receive()
{
cout<<"输入集合,按两次回车键确认:";
//接收集合元素
string Data;
getline(cin,Data,'\n');
int size=Data.length();
const char *ptr;
ptr=Data.data();
fflush(stdin);
fflush(stdout);
for(int i=0;i<size;i++)
Insert(*(ptr+i),i);
//判断重复,不符合要求的元素
for(i=0;i<ListSize();i)
{
if(GetData(i)<'A'||GetData(i)>'Z')
Delete(i);
else i++;
}
for(int k=0;k<ListSize();k++)
{
for(int n=k+1;n<ListSize();n)
{
if(GetData(k)==GetData(n))
Delete(n);
else n++;
}
}
//输出符合条件的元素
cout<<"符合条件的元素:";
fflush(stdin);
fflush(stdout);
if(ListEmpty()==1)
cout<<"没有符合条件的元素!\n"<<endl;
else
{
ListNode<T>*p=Reset(0);
while(!EndOfList())
{
cout<<p->data<<" ";
p=Next();
}
cout<<"\n";
}
}

//主函数
#include "stdafx.h"
#include <iostream>
#include <string>
#include "LinList.h"

using namespace std;

void main()
{
LinList<char>myList1;
cout<<"请输入大写字母'A'~'Z'!"<<endl;
myList1.Receive();
myList1.ClearList();
myList1.Receive();
}


为什么运行后,输入第二次数据的时候,遍历就进入死循环了,请高手指点!!

[解决办法]

C/C++ code
template <class T > void LinList <T >::ClearList(void) {     ListNode <T >*p,*p1;     //head的next没有置空    p=head->next;     head->next = NULL;    while(p!=NULL)     {         p1=p;         p=p->next;         delete p1;             }     size=0; } 

热点排行