package Test2; //链表:LinkList public class LinkList { public static void main(String[] args) { } } class Link { class Node { private int key; //定义链表结点的存储数据 private int value; //定义链表结点的存储数据 private Node next; //指向下一个元素的引用 private Node head; //给链表类声明一个头结点 public Node(int key,int value) { this.key = key; this.value = value; next = null; } public void addNode(int key,int value) { if(head == null) //如果链表为空 { head = new Node(key,value); //把结点直接加入head中 } else { Node current = head; while(current.next != null) //通过循环判断当前的值下个元素是否为空 { current = current.next; //查找链表尾部 } current.next = new Node(key,value); //添加新的结点 } } public Node getNode(int key) //链表的查询检索只能从前向后依次进行,无法使用随机搜索 { Node current = head; if(head.key == key) { return head; } else { current = head.next; while(current.key != key) { current = current.next; } return current; } } } }
浙公网安备 33010602011771号