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

顺序表缺点:
  • 插入删除结点的时候,往往需要移动大量的数据
  • 表比较大时,比较难分配足够的连续存储空间
链表每个结点包括:
  • 数据部分
  • 地址部分
链表优势:
  • 节点之间不要求连续存放。
  • 随时释放不用的存储空间。
劣势:
  • 浪费存储空间
/**
 * @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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值