public class Link { /** * 链结点 */ private int iData; private double dData; public Link next; public Link(int iData,double dData) { this.dData = dData; this.iData = iData; } //打印节点 public void displayLink() { System.out.println("{"+iData+","+dData+"}"); } } public class LinkList { //这个类比较简单就有一个指向节点的引用 private Link first; public LinkList() { this.first = null; } //判断链表是不是为空 public boolean isEmpty() { if(first == null) { return true; }else{ return false; } } //链表的插入头插法(面向对象的思考) public void insertFirst(Link link) { link.next = first;//首先让新生成的节点的next指向first first = link; } //删除的头节点的方法 public Link deleteFirst() { //删除节点的原理:让first指向第一个节点的下一个节点 Link temp = first; first = first.next; return temp; } //打印链表的方法 public void displayList() { Link current = first; while(current != null) { current.displayLink(); current = current.next; } System.out.println(" "); } }
时间: 2024-12-28 00:44:40