今天從數據結構開始看起,首先了解的是鏈表。我先從鏈表的創建開始,本人習慣使用java,開始介紹也使用java實現
鏈表
鏈表的創建
1.1連表的插:
package link; /** * linkCreate是創建鏈表,主要有鏈表的創建, * 鏈表建立,鏈表的測試,打印鏈表的長度 * @author Administrator * 在這裏使用的是 * */ public class LinkCreate<Item> { private Node first; private int length; public LinkCreate(){ first=null; length=0; } /** * 在這裏是開始創建鏈表,使用的是頭插入的方法 */ public void add(Item item){ Node oldfirst=first; first=new Node(); first.item=item; first.next=oldfirst; length=length+1; } /** * 一個鏈表要判斷是否是空,的長度是多少 * @param node * @return */ public int length(Node node){ int length=0; if(node==null) return length; while(node!=null){ length++; node=node.next; } return length; } public void print(Node node){ while(node!=null){ System.out.print(node.item); node=node.next; System.out.println(); } } public static void main(String[] args) { LinkCreate<String> lc =new LinkCreate<String>(); for(int i=0;i<8;i++) { lc.add(i+"a"); System.out.println(lc.length); } System.out.println(lc.length(lc.first)); lc.print(lc.first); } /** * 在這的Node表示的是鏈表中的節點,主要是使用的是的Item的值 * 以及連接下一個的鏈表 * @author Administrator * */ class Node<Item>{ private Item item; private Node next; } }