您的位置:首页 > 理论基础 > 数据结构算法

Java实现数据结构与算法-链表

2015-08-25 20:49 423 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/zhxhash/article/details/47983235 顺序表缺点:
  • 插入删除结点的时候,往往需要移动大量的数据
  • 表比较大时,比较难分配足够的连续存储空间
链表每个结点包括:
  • 数据部分
  • 地址部分
链表优势:
  • 节点之间不要求连续存放。
  • 随时释放不用的存储空间。
劣势:
  • 浪费存储空间
/**
* @author Hash.Zhang
*
*/
public class LinkedList {
Data NodeData=new Data();
LinkedList NextNode;
/**
* LinkedListAddEnd: Add a new node with data at the end of the linked list
* @return true for successfully inserting; false for not successfully inserting
*/
public boolean LinkedListAddEnd(LinkedList head,Data data)
{
LinkedList node,temp=head;
if((node=new LinkedList())==null)
{
System.out.println("Fail to allocate memory!");
return false;
}
node.NodeData=data;
node.NextNode=null;
while(temp.NextNode!=null)
temp=temp.NextNode;
temp.NextNode=node;
return true;
}
/**
* LinkedListAddFirst: Add a new node with data at the first of the linked list
* @return true for successfully inserting; false for not successfully inserting
*/
public boolean LinkedListAddFirst(LinkedList head,Data data)
{
LinkedList node,temp=head;
if((node=new LinkedList())==null)
{
System.out.println("Fail to allocate memory!");
return false;
}
node.NodeData=data;
node.NextNode=temp.NextNode;
temp.NextNode=node;
return true;
}
/**
* LinkedListFindNode: Find the node with sequence number n
* @return the node with aimed sequence number
*/
public LinkedList LinkedListFindNode(LinkedList head,int n)
{
LinkedList temp=head.NextNode;
while(temp!=null&&temp.NodeData.SequenceNo!=n)
temp=temp.NextNode;
if(temp==null)
{
System.out.println("The sequence number does not exist within the linked list!");
}
return temp;
}
/**
* LinkedListFindNode: Find the node with ID same as key
* @return the node's sequence number with aimed ID same as key, -1 for not found
*/
public int LinkedListFindNode(LinkedList head,String key)
{
LinkedList temp=head.NextNode;
while(temp!=null&&!temp.NodeData.ID.equals(key))
temp=temp.NextNode;
if(temp==null)
{
System.out.println("The key does not exist within the linked list!");
return -1;
}
return temp.NodeData.SequenceNo;
}
/**
* LinkedListInsertNode: Insert the node with data after the node with same ID as key
* @return true for successfully inserting; false for not successfully inserting
*/
public boolean LinkedListInsertNode(LinkedList head,String FindKey,Data data)
{
LinkedList node,temp;
if((node=new LinkedList())==null)
{
System.out.println("Fail to allocate memory!");
return false;
}
temp=LinkedListFindNode(head,LinkedListFindNode(head,FindKey));
if(temp==null)
{
return false;
}
node.NodeData=data;
node.NextNode=temp.NextNode;
temp.NextNode=node;
return true;
}
/**
* LinkedListDeleteNode: Delete the node with same ID as key
* @return true for successfully deleting; false for not successfully deleting
*/
public boolean LinkedListDeleteNode(LinkedList head,String FindKey)
{
LinkedList node=head,temp=head.NextNode;
while(temp!=null)
{
if(temp.NodeData.ID.equals(FindKey))
{
node.NextNode=temp.NextNode;
temp=null;
return true;
}
temp=temp.NextNode;
node=node.NextNode;
}
return false;
}
/**
* Print the content of the whole linked list
*/
public void printList(LinkedList head)
{
int i=0;
LinkedList temp=head;
while((temp=temp.NextNode)!=null)
{
System.out.printf("(ID:%s,Sequence No.%d) ",temp.NodeData.ID,temp.NodeData.SequenceNo);
i++;
}
System.out.printf("\nNet nodes: %d\n",i);
}
/**
* main method to test LinkedList
*/
public static void main(String[] args) {
LinkedList ll=new LinkedList();
ll.LinkedListAddFirst(ll,new Data(100,"Hash"+100));
ll.printList(ll);
for(int i=10;i<20;i++)
ll.LinkedListAddEnd(ll,new Data(i,"Hash"+i));
ll.printList(ll);
for(int i=10;i>0;i--)
ll.LinkedListAddFirst(ll,new Data(i,"Hash"+i));
ll.printList(ll);
ll.LinkedListInsertNode(ll, "Hash1",new Data(99,"Hash"+99));
ll.printList(ll);
ll.LinkedListDeleteNode(ll, "Hash19");
ll.printList(ll);
}

}
public class Data {
<span style="white-space:pre">	</span>int SequenceNo;
<span style="white-space:pre">	</span>String ID;
<span style="white-space:pre">	</span>public Data()
<span style="white-space:pre">	</span>{}
<span style="white-space:pre">	</span>public Data(int a)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>SequenceNo=a;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public Data(String ch)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>ID=ch;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public Data(int a,String ch)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>SequenceNo=a;
<span style="white-space:pre">		</span>ID=ch;
<span style="white-space:pre">	</span>}
}
输出结果: (ID:Hash100,Sequence No.100) 
Net nodes: 1
(ID:Hash100,Sequence No.100) (ID:Hash10,Sequence No.10) (ID:Hash11,Sequence No.11) (ID:Hash12,Sequence No.12) (ID:Hash13,Sequence No.13) (ID:Hash14,Sequence No.14) (ID:Hash15,Sequence No.15) (ID:Hash16,Sequence No.16) (ID:Hash17,Sequence No.17) (ID:Hash18,Sequence No.18) (ID:Hash19,Sequence No.19) 
Net nodes: 11
(ID:Hash1,Sequence No.1) (ID:Hash2,Sequence No.2) (ID:Hash3,Sequence No.3) (ID:Hash4,Sequence No.4) (ID:Hash5,Sequence No.5) (ID:Hash6,Sequence No.6) (ID:Hash7,Sequence No.7) (ID:Hash8,Sequence No.8) (ID:Hash9,Sequence No.9) (ID:Hash10,Sequence No.10) (ID:Hash100,Sequence No.100) (ID:Hash10,Sequence No.10) (ID:Hash11,Sequence No.11) (ID:Hash12,Sequence No.12) (ID:Hash13,Sequence No.13) (ID:Hash14,Sequence No.14) (ID:Hash15,Sequence No.15) (ID:Hash16,Sequence No.16) (ID:Hash17,Sequence No.17) (ID:Hash18,Sequence No.18) (ID:Hash19,Sequence No.19) 
Net nodes: 21
(ID:Hash1,Sequence No.1) (ID:Hash99,Sequence No.99) (ID:Hash2,Sequence No.2) (ID:Hash3,Sequence No.3) (ID:Hash4,Sequence No.4) (ID:Hash5,Sequence No.5) (ID:Hash6,Sequence No.6) (ID:Hash7,Sequence No.7) (ID:Hash8,Sequence No.8) (ID:Hash9,Sequence No.9) (ID:Hash10,Sequence No.10) (ID:Hash100,Sequence No.100) (ID:Hash10,Sequence No.10) (ID:Hash11,Sequence No.11) (ID:Hash12,Sequence No.12) (ID:Hash13,Sequence No.13) (ID:Hash14,Sequence No.14) (ID:Hash15,Sequence No.15) (ID:Hash16,Sequence No.16) (ID:Hash17,Sequence No.17) (ID:Hash18,Sequence No.18) (ID:Hash19,Sequence No.19) 
Net nodes: 22
(ID:Hash1,Sequence No.1) (ID:Hash99,Sequence No.99) (ID:Hash2,Sequence No.2) (ID:Hash3,Sequence No.3) (ID:Hash4,Sequence No.4) (ID:Hash5,Sequence No.5) (ID:Hash6,Sequence No.6) (ID:Hash7,Sequence No.7) (ID:Hash8,Sequence No.8) (ID:Hash9,Sequence No.9) (ID:Hash10,Sequence No.10) (ID:Hash100,Sequence No.100) (ID:Hash10,Sequence No.10) (ID:Hash11,Sequence No.11) (ID:Hash12,Sequence No.12) (ID:Hash13,Sequence No.13) (ID:Hash14,Sequence No.14) (ID:Hash15,Sequence No.15) (ID:Hash16,Sequence No.16) (ID:Hash17,Sequence No.17) (ID:Hash18,Sequence No.18) 
Net nodes: 21

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: