琴无续弦 2015-12-27 10:35 采纳率: 50%
浏览 1623
已采纳

这个simplelist.h是怎么搞的?不太明白?关于c++的

这个simplelist。h是怎么搞的?不太明白?

#if !defined_LIST_H_
#define LIST_H
//单链表定义
#include
#include"simplelist.h"

int main()
{
List l1;
cout<<"以-1作为约定的链表数据输入结束符号"< cout l1.inputFrond(-1);
l1.output();
List l2(l1);
l2.output();
return 0;
}

template //链表节点类的定义

struct LinkNode
{
T date; //数据域
LinkNode*link; //链指针域
LinkNode(LinkNode *ptr =NULL) {link=ptr;}
LinkNode(const T&item,LinkNode *ptr=NULL)
{
data=item;
link=ptr;
}
};

template
class List
{
public:
List(){first=new LinkNode;}
List(const T &x){first= new LinkNode(x);}
List(List &L);
~List(){makeEmpty();}
void makeEmpty();
int Length() const;
LinkNode *getHead()const{return first;}
LinkNode *Search(T x);
LinkNode *Locate(int i);
bool getData(int i,T &x)const;
void setData(int i,T &x);
bool Insert(int i, T &x);
bool Remove(int i, T &x);

    bool IsEmpty()const
    {
        return first->link==NULL?true:false;
    }
    bool IsFull()const{return false ;}

    void Sort()
    void inputFrond (T endTag);
    void output();
    List<T>&operator=(List <T>&L);

protected:
    LinkNode<T> *first;

};

template
List::List(List &L)
{
T value;
LinkNode *srcptr=L.getHead();
LinkNode *desptr=first=new LinkNode;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
desptr->link=new LinkNode(value);
desptr=desptr->link;
srcptr=srcptr->link;
}
desptr->link=NULL;
}

template
int List::makeEmpty()
{
LinkNode *q;
while(first->link!=NULL)
{
q=first->link;
first->link=q->link;
delete q;

}

}

template
int List::Length()const
{
LinkNode *p=first->link;
int count=0;
while(p!=NULL)
{
p=p->link;
count++
}
return count;

}

template
LinkNode*List::Search(T x)
{
LinkNode*current=first->link;
while(current!=NULL)
if(current->data==x)break;
else current =current->link;
return current;
}

template
LinkNode*List::Loate(int i)
{
if(i<0)
{
return NULL;
}

LinkNode<T>*current =first;
int k=0;
while(current!=NULL&&k<i)
{
    current=current->link;
    k++;
}
return current;

}

template
bool List::getData(int i,T &x)const
{
if(i LinkNode *current=Locate(i);
if(current==NULL) return false ;
else
{
x=current->data;
return true;
}
}

template
void List::setData(int i,T &x)
{
if(i<=0) return;
LinkNode *current =Locate(i);
if(current==NULL) return;
else current->data=x;
}

template
bool List::Insert(int i,T &x)
{
LinkNode *current=Locate(i);
if(current==NULL) return false;
LinkNode *newNode=new LinkNode(x);
if(newNode==NULL){return false;}
newNode->link=current->link;
current->link=newNode;
return true;
}

template
bool List::Remove(int i,T &x)
{
LinkNode*current=Locate(i-1);
if(current==NULL||current->link==NULL) return false;
LinkNode *del=current->link;
current->link=del->link;
x=del->data;
delete del;
return true;

}

template
void List::output()
{
LinkNode *current=first->link;
while(current!=NULL)
{
cout<data< current=current->link;
}
}

template
void List::inputFront(T endTag)
{
LinkNode *newNode;
T val;
//MakeEmpty();
cin>>val;
while(val!=endTag)
{
newNode=new LinkNode(val);
if(newNode==NULL)
{
cerr<<"储存分配错误"< }
newNode->link=first->link;//插在表前端
first->link=newNode;
cin>>val;
}
}

template
List &List::operator=(List &L)
{
LinkNode *srcptr =L.getHead();
LinkNode *desptr =first=new LinkNode;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
desptr=desptr->link;
srcptr=srcptr->link;
}
desptr->link=NULL;
}

#endif

  • 写回答

2条回答 默认 最新

  • ysuwood 2015-12-27 10:44
    关注

    挺长的,具体哪里不明白?如果都不明白,说来就话长了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料