学习java数据结构基础知识之链表
public class Link { public int iData; public double dData; public Link next; public Link(int iData, double dData) { super(); this.iData = iData; this.dData = dData; } public void displayLink(){ System.out.print("{"+iData+","+dData +"}"); } }
public class LinkList { private Link first; public void LinkList() { first =null; } //链表是否为空 public boolean isEmpty(){ return (first ==null); } //插入first头元素 public void insertFirst(int id,double dd) { Link newLink =new Link(id, dd); newLink.next =first; first =newLink; } //删除链表头元素 public Link deleteFirst(){ Link tempLink =first; first =first.next; return tempLink; } //找到特定节点 public Link find(int key){ Link current =first; while (current.iData !=key) { if(current.next ==null){ return null; }else { current =current.next; } } return current; } //删除节点 public Link delete(int key){ Link currentLink =first; Link previous=first; while (currentLink.iData !=key) { if (currentLink.next ==null) { return null; }else { previous =currentLink; currentLink =currentLink.next; } } if (currentLink ==first) { first =first.next; }else{ previous.next =currentLink.next; } return currentLink; } //插入特定节点 public Boolean insertLink(int key ,int iData, double dData){ int i=0; Link currentLink =first; Link previous =first; Link nextLink; Link newLink =new Link(iData, dData); //插入头元素 if ((i==key)&&(currentLink!=null)) { newLink.next =currentLink; first=newLink; return true; }else if (currentLink!=null){ // i++; while (i!= (key-1)) { if (currentLink.next ==null) { currentLink.next=newLink; return true; }else { previous =currentLink; currentLink =currentLink.next; i++; } } if (currentLink.next !=null) { nextLink =currentLink.next; currentLink.next=newLink; newLink.next=nextLink; return true; }else { currentLink.next =newLink; newLink.next =null; return true; } }else if (currentLink ==null) { currentLink =newLink; first=newLink; first.next=null; return true; } return false; } //显示链表所有数据 public void displayList(){ System.out.println("LinkList.displayList() List(first-->last):"); Link currentLink =first; while (currentLink!=null) { currentLink.displayLink(); currentLink =currentLink.next; } System.out.println(""); System.out.print("LinkList.displayList() finish"); } }
public class LinkListApp { public static void main(String[] args){ LinkList theLinkList =new LinkList(); theLinkList.insertLink(9, 999, 999.999); theLinkList.displayList(); theLinkList.insertFirst(22, 22.22); theLinkList.insertFirst(33, 33.33); theLinkList.insertFirst(44, 44.44); theLinkList.insertFirst(55, 55.55); theLinkList.displayList(); theLinkList.insertLink(2, 333, 333.333); theLinkList.displayList(); theLinkList.insertLink(7, 777, 777.777); theLinkList.displayList(); System.out.println(""); while (!theLinkList.isEmpty()) { Link aLink =theLinkList.deleteFirst(); System.out.print("Deleted "); aLink.displayLink(); System.out.println(""); } theLinkList.displayList(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。