1顺序表结构的缺点
在插入或者删除结点时,需要移动大量的数据。
如果表特别大,很难分配足够的连续存储空间。
2链表的结点特点
含有两个部分,数据部分:保存该节点的实际数据。地址部分:保存下一个结点的引用地址。
3链表结构的优劣
优:不需要分配连续的地址空间,可以用new函数分配结点的存储空间,将节点赋值null即可释放其占用的内存空间
劣:浪费存储空间,需要保存额外的引用变量。对链表的访问也只能从头开始逐个查找。
4链表的细分
单链表:节点中只含有一个引用。双向链表:每个节点中包含指向下一个,上一个节点的两个引用。
单循环链表:单链表中,将终端结点的引用null改为头节点即可构成单循环链表。
多重链的循环链表:将表中的结点链在多个环上。
5用java语言表示链表
class Data2{
String key;
String name;
int age;
}
class cltype{
Data2 nodeData=new Data2();
cltype nextNode;
}
追加一个结点
cltype claddend(cltype head,Data2 nodeData){
cltype node,htemp;
if(node=new cltype())==null)
{
system.out.print(申请失败);
return null;
}else
{
node.nodeData=nodeData;
node.nextNode=null;
if(head==null){
head==node;
return head;
}
htemp=head;
while(htemp.nextNode!=null){
htemp=htemp.nextNode;
}
htemp.nextNode=node;
return head;
}
}
插入头节点
cltype claddfirst(cltype head,Data2 nodeData)
{
cltype node;
if(node==newcltype()==null)
{
system.out.print(申请失败);
return null;
}
else
{
node.nodeData=nodeData;
node.nextNode=head;
head=node;
return head;
}
}
查找结点
cltype clfindNode(cltype head,string key){
cltype htemp;
htemp=head;
while(htemp!=null){
if(htemp.nodeData.key.compareto(key)==0)
{
return htemp;
}
htemp=htemp.nextNode;
}
return null;
}
插入节点
cltype clinsertNode(cltype head,string findkey,Data2 nodeData){
cltype node,nodetemp;
if(node=new cltype())=null
{
system.out.print(申请失败);
return null;
}
node.nodeData=nodeData;
nodetemp=clfindnode(head,findkey);
if(nodetemp!=null)
{
node.nextnode=nodetemp.nextndoe;
nodetemp.nextndoe=node;
}
else
{
system.out.print("未找到正确的插入位置);
}
return head;
}
删除结点
int clDeleteNode(cltype head,string key){
cltype node,htemp;
htemp=head;
node=head;
while(htemp!=null)
{
if(htemp.nodeData.key.compareto(key)==0){
node.nextNode=htemp.nextnode;
htemp=null;
return 1;
}
else
{
node=htemp;
htemp=htemp.nextNode;
}
}return 0;
}
计算链表长度
int cllength(cltype head){
cltype htemp;
int len=0;
htemp=head;
while(htemp!=null)
{
len++;
htemp=htemp.nextNode;
}
return len;
}
显示所有结点
void clallnode(cltype head)
{
cltype htemp;
Data nodeData;
htemp=head;
system.out.print()
while(htemp!=null)
{
nodeData=htemp.nodeData;
sys.out.print(node 内容)
htemp=htemp.nextNode;
}
}
end!!!!