链表:依靠引用传递关系实现多个数据保存。
链表的常规实现:
1 public class Node { 2 String data; 3 Node next; 4 public Node(String data){ 5 this.data = data; 6 } 7 }
实现增、删、查、改:
为了对链表实现保护,将链表进行内部私有化实现:
增加数据:
count : 增加数据的标记
foot : 遍历链表的角标
private class Node { private String data;// save data private Node next;// Save the data of the next node public Node(String data) {// Contractor this.data = data; } public void addNode(Node temp) {// add node by recursive if (this.next == null) this.next = temp; else this.next.addNode(temp); } } private Node root; private int count = 0; private int foot;// index of node public void add(String str) { Node node = new Node(str);// crate new node if (this.root == null) this.root = node;// root no node exists else this.root.addNode(node);// root node exists count++; } public void print() { if (count == 0) System.out.println("Current List is null"); Node temp = root; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } }
测试:
我的外部类为ListNode
// for test public static void main(String[] args) { ListNode listNode = new ListNode(); listNode.add("a"); listNode.add("b"); listNode.add("c"); listNode.add("d"); listNode.print(); }
输出为:a b c d
链表的长度:
当前列表的长度和是否为空都可以通过count进行判断(在ListNode内)。
public int getLength() { return count; } public boolean isEmpty() { return count == 0; }
判断数据是否存在:
判断数据是否存在需要到Node类里面去查找。
在ListNode内:
public boolean contains(String str) { if (str == null || count == 0) return false; return root.containsNode(str); }
在Node类里面:
public boolean containsNode(String str) { if (str.equals(this.data))// The data of the current node is the data to be found. return true;// base case if (this.next != null)// Find in subsequent nodes return this.next.containsNode(str); return false; }
取得链表中index位置的数据(重要):
在ListNode内:
public String getData(int index) { if (count < index) return null; foot = 0; return root.getNodeData(index); }
在Node类里面:
public String getNodeData(int index) { if (ListNode.this.foot++ == index)// The current node is the target return this.data;// base case return this.next.getNodeData(index);// Find in subsequent nodes }
测试:
// for test public static void main(String[] args) { ListNode listNode = new ListNode(); listNode.add("a"); listNode.add("b"); listNode.add("c"); listNode.add("d"); System.out.println(listNode.getLength()); System.out.println(listNode.getData(3)); System.out.println(listNode.contains("a")); System.out.println(listNode.isEmpty()); listNode.print(); }
输出:
4 d true false a b c d
变更数据:
变更数据和之前的查询操作类似。
在ListNode内:
public void setData(int index, String data) { if(this.count < index){ System.out.println("Exceeding the length of List!"); return; } this.foot = 0; this.root.setDataNode(index, data); }
在Node内:
public void setDataNode(int index, String data) { if (ListNode.this.foot++ == index)// The current node is the target this.data = data;// base case else// important this.next.setDataNode(index, data); }
删除在index出的数据:
在Node内:
public void removeIndexNode(Node pervious, int index){ if (ListNode.this.foot++ == index){// The current node is the target pervious.next = this.next;// base case } else{ this.next.removeIndexNode(this, index); } }
在ListNode内:
public void removeIndex(int index){ if(this.count < index){ System.out.println("Exceeding the length of List!"); return; } if(index == 0){// remove top node this.root = this.root.next;// Clear current node return; } this.foot = 1; // Judging from the second element this.root.next.removeIndexNode(this.root, index); this.count--; }
注意,起初在这儿的时候卡了,传进去原链表才能对其进行修改。如果是按照数据进行删除数据类似的写就行。
链表转为数组:
在ListNode内:
public String[] toArray(){ String[] strings = new String[count]; if (this.count == 0){ System.out.println("Current List is null"); return null; } Node temp = root; int i = 0; while (temp != null) { strings[i] = temp.data; i++; // System.out.print(temp.data + " "); temp = temp.next; } return strings; }
链表转数组和打印链表类似。
测试:
// for test public static void main(String[] args) { ListNode listNode = new ListNode(); for (int i = 0; i <= 5; i++) listNode.add(i + ""); listNode.print(); System.out.println(); listNode.setData(1, "7"); listNode.print(); System.out.println(); listNode.removeIndex(2); listNode.print(); System.out.println(); String[] strings = listNode.toArray(); System.out.println(Arrays.toString(strings)); }
输出:
0 1 2 3 4 5
0 7 2 3 4 5
0 2 3 4 5
[0, 2, 3, 4, 5]