【算法数据结构Java实现】Java实现单链表
生活随笔
收集整理的這篇文章主要介紹了
【算法数据结构Java实现】Java实现单链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.背景
? ? ? ? ? 單鏈表是最基本的數據結構,仔細看了很久終于搞明白了,差不每個部分,每個鏈都是node的一個對象。需要兩個參數定位:一個是index,表示對象的方位。另一個是node的對象。
2.代碼
node類 public class Node {protected Node next;protected int data;public Node(int data){this.data=data;}public void display(){System.out.print(data+"");} }
arraylist類 public class myArrayList {public Node first;//定義頭結點private int pos=0;//節點位置public myArrayList(){// this.first=null;}//插入一個頭結點public void addFirstNode(int data){Node node=new Node(data);node.next=first;first=node;}//刪除頭結點public Node deleteFirstNode(){Node tempNode=first;first=tempNode.next;return tempNode;}// 在任意位置插入節點 在index的后面插入 public void add(int index, int data) { Node node = new Node(data); Node current = first; Node previous = first; while ( pos != index) { previous = current; current = current. next; pos++; } node. next = current; previous. next = node; pos = 0; } // 刪除任意位置的節點 public Node deleteByPos( int index) { Node current = first; Node previous = first; while ( pos != index) { pos++; previous = current; current = current. next; } if(current == first) { first = first. next; } else { pos = 0; previous. next = current. next; } return current; } public void displayAllNodes() { Node current = first; while (current != null) { current.display();System.out.println();current = current. next; } } }
實現的main函數: public class Main {public static void main(String args[]){myArrayList ls=new myArrayList(); ls.addFirstNode(15); ls.addFirstNode(16);ls.add(1, 144);ls.add(2, 44);ls.deleteByPos(1);ls.displayAllNodes(); } }
實現結果:
16
44
15
package LinkedList; /** * <p><strong>我的Java單鏈表練習</strong></p> * <p>單鏈表提供了在列表頭的高效插入和刪除操作,不過在單鏈表的末尾的插入操作效率很低.</p> * <p>單鏈表指針域保存著下一節點的引用,尾結點的指針域等于null</p> * @author baby69yy2000 */ public class SingleLinkedList<T> { /** * 結點類 */ private static class Node<T> { T nodeValue; // 數據域 Node<T> next; // 指針域保存著下一節點的引用 Node(T nodeValue, Node<T> next) { this.nodeValue = nodeValue; this.next = next; } Node(T nodeValue) { this(nodeValue, null); } } // 下面是SingleLinkedList類的數據成員和方法 private Node<T> head, tail; public SingleLinkedList() { head = tail = null; } /** * 判斷鏈表是否為空 */ public boolean isEmpty() { return head == null; } /** * 創建頭指針,該方法只用一次! */ public void addToHead(T item) { head = new Node<T>(item); if(tail == null) tail = head; } /** * 添加尾指針,該方法使用多次 */ public void addToTail(T item) { if (!isEmpty()) { // 若鏈表非空那么將尾指針的next初使化為一個新的元素 tail.next = new Node<T>(item); // 然后將尾指針指向現在它自己的下一個元素 tail = tail.next; } else { // 如果為空則創建一個新的!并將頭尾同時指向它 head = tail = new Node<T>(item); } } /** * 打印列表 */ public void printList() { if (isEmpty()) { System.out.println("null"); } else { for(Node<T> p = head; p != null; p = p.next) System.out.println(p.nodeValue); } } /** * 在表頭插入結點,效率非常高 */ public void addFirst(T item) { Node<T> newNode = new Node<T>(item); newNode.next = head; head = newNode; } /** * 在表尾插入結點,效率很低 */ public void addLast(T item) { Node<T> newNode = new Node<T>(item); Node<T> p = head; while (p.next != null) p = p.next; p.next = newNode; newNode.next = null; } /** * 在表頭刪除結點,效率非常高 */ public void removeFirst() { if (!isEmpty()) head = head.next; else System.out.println("The list have been emptied!"); } /** * 在表尾刪除結點,效率很低 */ public void removeLast() { Node<T> prev = null, curr = head; while(curr.next != null) { prev = curr; curr = curr.next; if(curr.next == null) prev.next = null; } } /** * <p>插入一個新結點</p> * <ul>插入操作可能有四種情況: * <li>①表為空, 返回false</li> * <li>②表非空,指定的數據不存在</li> * <li>③指定的數據是表的第一個元素</li> * <li>④指定的數據在表的中間</li></ul> * @param appointedItem 指定的nodeValue * @param item 要插入的結點 * @return 成功插入返回true; */ public boolean insert(T appointedItem, T item) { Node<T> prev = head, curr = head.next, newNode; newNode = new Node<T>(item); if(!isEmpty()) { while((curr != null) && (!appointedItem.equals(curr.nodeValue))) { //兩個判斷條件不能換 prev = curr; curr = curr.next; } newNode.next = curr; //②③④ prev.next = newNode; return true; } return false; //① } /** * <p>移除此列表中首次出現的指定元素</p> * <ul>刪除操作可能出現的情況: * <li>①prev為空,這意味著curr為head. head = curr.next; --> removeFirst();</li> * <li>②匹配出現在列表中的某個中間位置,此時執行的操作是 --> prev.next = curr.next;,</li></ul> * <p>在列表中定位某個結點需要兩個引用:一個對前一結點(prev左)的引用以及一個對當前結點(curr右)的引用.</p> * prev = curr; * curr = curr.next; */ public void remove(T item) { Node<T> curr = head, prev = null; boolean found = false; while (curr != null && !found) { if (item.equals(curr.nodeValue)) { if(prev == null) removeFirst(); else prev.next = curr.next; found = true; } else { prev = curr; curr = curr.next; } } } /** * 返回此列表中首次出現的指定元素的索引,如果列表中不包含此元素,則返回 -1. */ public int indexOf(T item) { int index = 0; Node<T> p; for(p = head; p != null; p = p.next) { if(item.equals(p.nodeValue)) return index; index++; } return -1; } /** * 如果此列表包含指定元素,則返回 true。 */ public boolean contains(T item) { return indexOf(item) != -1; } public static void main(String[] args) { SingleLinkedList<String> t = new SingleLinkedList<String>(); t.addToHead("A"); //t.addFirst("addFirst"); t.addToTail("B"); t.addToTail("C"); System.out.println(t.indexOf("C")); // 2 System.out.println(t.contains("A")); // true //t.addLast("addLast"); //t.removeLast(); //t.insert("B", "insert"); //t.removeFirst(); //t.remove("B"); // A C t.printList(); // A B C } }
參考:http://blog.csdn.net/tayanxunhua/article/details/11100097
/********************************
* 本文來自博客 ?“李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
總結
以上是生活随笔為你收集整理的【算法数据结构Java实现】Java实现单链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【算法数据结构Java实现】欧几里得算法
- 下一篇: python 中文乱码问题解决方案