数据结构中的一个问题。实现基于链表的线性表中数据的倒置输出。
[code=C/C++][/code]
#include<iostream>
using namespace std;
template <class Elem>
class Link{
public:
Elem element;
Link*next;
Link(const Elem& elemval,Link* nextval=NULL)
{
element=elemval;
next=nextval;
}
Link(Link* nextval=NULL)
{
next=nextval;
}
};
template <class Elem>
class LList:public Link<Elem>{
private:
Link<Elem>* head;
Link<Elem>* tail;
Link<Elem>* fence;
int leftcnt;
int rightcnt;
void init(){
fence=head=tail=new Link<Elem>;
leftcnt=rightcnt=0;
}
void removeall(){
while(head!=NULL){
fence=head;
head=head->next;
delete fence;
}
}
public:
LList(int DefaultListSize){
init();
}
~LList(){
removeall();
}
void clear(){
removeall();
init();
}
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart(){
fence=head;
rightcnt+=leftcnt;
leftcnt=0;
}
void setEnd(){
fence=tail;
leftcnt+=rightcnt;
rightcnt=0;
}
void prev();
void next(){
if(fence!=tail){
fence=fence->next;
leftcnt++;
righgtcnt--;
}
}
int leftLength() const{
return leftcnt;
}
int rightLength() const{
return rightcnt;
}
bool setPos(int pos);
bool getValue(Elem& it) const{
if(rightcnt==0) return false;
it=fence->next->element;
return true;
}
void print(Link<Elem>* temp=head) const;
//新增的成员函数:实现倒置线性表元素的顺序的功能
bool reset(Link<Elem> * &newhead);
};
template<class Elem>
bool LList<Elem>::insert(const Elem& item){
fence->next=new Link<Elem>(item,fence->next);
if(fence==tail) tail=fence->next;
rightcnt++;
return true;
}
template<class Elem>
bool LList<Elem>::append(const Elem& item){
tail=tail->next=new Link<Elem>(item);
rightcnt++;
return true;
}
template<class Elem>
bool LList<Elem>::remove(Elem& item){
if(fence->next==NULL) return false;
item=fence->next->element;
Link<Elem>* Itemp=fence->next;
fence->next=Itemp->next;
if(tail==Itemp) tail=fence;
delete Itemp;
rightcnt--;
return true;
}
template<class Elem>
void LList<Elem>::prev(){
Link<Elem>* Temp=head;
if(fence==head) return;
while(Temp->next!=fence)
Temp=Temp->next;
fence=temp;
leftcnt--;
rightcnt++;
}
template<class Elem>
bool LList<Elem>::setPos(int pos){
if(pos<0||pos>rightcnt+leftcnt) return false;
fence=head;
for(int i=0;i<pos;i++)
fence=fence->next;
rightcnt=rightcnt+leftcnt-pos;
leftcnt=pos;
return true;
}
template<class Elem>
void LList<Elem>::print(Link<Elem>* temp) const{
cout<<"<";
while(temp!=fence){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<"|";
while(temp->next!=NULL){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<">\n";
}
template<class Elem>
bool LList<Elem>::reset(Link<Elem>* & newhead){
Link<Elem>* ptr=head;
Link<Elem>* sp;
while(ptr!=NULL){
if(newhead==NULL)
newhead=head;
else{
sp=new Link<Elem>();
sp=ptr;
sp->next=newhead;
newhead=sp;
}
ptr=ptr->next;
delete sp;
}
return true;
}
int main()
{
double num;
LList<double>* project=NULL;
cout<<"任意输入10个double型数据可以实现倒置输出:\n";
for(int i=0;i<10;i++)
{
cin>>num;
if(project->append(num)){}
else {cin>>num;i--;}
}
cout<<"倒置输出结果: ";
LList<double>* newproject=NULL;
Link<double>* newhead;
newproject->reset(newhead);
newproject->print(newhead);
cout<<endl;
}
ps:单步调试,在append(num)时就出现指针指向错误。~就是说,连生成一条线性表都生成不了。
个人觉得,问题应该是出现在init()和append()函数对tail的处理是矛盾的。书上说,基于链表的线性表的头指针应该是指向NULL的(考虑到fence的定义),头指针中不放任何数据,可是,在init()中,tail也指向头指针,而同时要保证头指针为空的话,只能在追加函数下(append)添加数据生成线性表,那不就出现tail=NULL的前提下,有要求tail->next=new Link<double>(10),tail->next=tail这种操作.感觉很难理解,认为这么做是错的。
问题一:麻烦高手指点我上述想法的对错。并给出您的个人理解作为参考。
问题二:上述代码要怎么改才能正确生成一条线性表,至于倒置方面的功能,我自己再揣摩思考。
谢谢!
注:以上代码,除了main函数和新增加的reset函数实现倒置外,其他都是教材上的原代码。刚学数据结构,书上的例子尽是模板,都没有实现例子参考。
[解决办法]
你的代碼格式好難看
tail=tail->next=new Link<Elem>(item);// i=0
tail=tail->next=new Link<Elem>(item);// i=1
指向i=0分配的內存的指針指向另一個內存, 你怎樣delete這內存
[解决办法]
能把代码重新帖一遍吗?把代码放到[code=C/C++][/code]中间