Java單鏈表的實現

來源:互聯網
上載者:User
/** *  */package com.handy.ds;/** * @author handy 2012-3-15 */class Node {int data;Node next;public Node() {}public Node(int data, Node next) {this.data = data;this.next = next;}}public class SingleLinkedList {private Node head;/** * @return the head */public Node getHead() {return head;}/** * @param head *            the head to set */public void setHead(Node head) {this.head = head;}public SingleLinkedList() {head = new Node();}public boolean isEmpty() {if (head.next == null)return true;elsereturn false;}public boolean addToLast(int elem) {if (isEmpty()) {head.next = new Node(elem, null);return true;} else {Node curr = head.next, prev = null;while (curr != null) {prev = curr;curr = curr.next;}Node newNode = new Node(elem, null);prev.next = newNode;return true;}}public boolean addToFirst(int elem) {if (isEmpty()) {head.next = new Node(elem, null);return true;} else {Node p = new Node(elem, head.next);head.next = p;return true;}}public void printList() {if (head.next == null) {System.out.print("空");}for (Node p = head.next; p != null; p = p.next)System.out.print(p.data + ",");System.out.println();}public boolean insert(int posValue, int elem) {if (isEmpty() && posValue == 0) {head.next = new Node(elem, null);return true;} else {Node prev = head, curr = head.next, newNode = new Node(elem, null);while (curr != null && curr.data != posValue) {if (curr.next == null)return false;prev = curr;curr = curr.next;}newNode.next = curr;prev.next = newNode;return true;}}public int removeFirst() {if (isEmpty()) {return -1;} else {Node temp = head.next;if (temp.next != null)// 有兩個以上節點head = temp.next;elsehead.next = null; // 一個節點return temp.data;}}public int removeLast() {if (isEmpty()) {return -1;} else {int temp = 0;Node prev = head, curr = head.next;while (curr != null) {if (curr.next == null) {temp = curr.data;prev.next = null;// System.out.println(1);}prev = curr;curr = curr.next;return temp;}return temp;}}public boolean remove(int elem) {if (isEmpty())return false;else {Node prev = head, curr = head.next;while (curr != null && curr.data != elem) {if (curr.next == null) {return false;}prev = curr;curr = curr.next;}prev.next = curr.next;return true;}}// 返還重複節點 的個數public int getSameNumber(int value) {int count = 0;Node curr = head.next;while (curr != null) {if (curr.data == value)count++;curr = curr.next;}return count;}// 去掉重複節點public boolean removeSame() {Node p = head;Node curr1 = head.next;Node curr2 = null;int temp;while (curr1 != null) {if (getSameNumber(curr1.data) > 1) {temp = curr1.data;curr2 = curr1;while (curr2 != null) {// 去掉所有值為temp的節點remove(temp);curr2 = curr2.next;}}curr1 = curr1.next;}return true;}// 1.將單鏈表逆置public boolean reverse1() {if (isEmpty())return false;Node p = head, q = head.next;Node r;while (q != null) {r = q.next;q.next = p;p = q;q = r;}head.next.next = null;head.next = p;return true;}// 2.將單鏈表逆置public boolean reverse2() {if (isEmpty())return false;int size = 0;for (Node p = head.next; p != null; p = p.next)size++;int[] array = new int[size];int i;Node p;for (p = head.next, i = 0; p != null && i < size; p = p.next, i++)array[i] = p.data;for (p = head.next, i = 0; p != null && i < size; p = p.next, i++)p.data = array[size - 1 - i];return true;}// 判斷鏈表是否存在迴圈public boolean isContainsLoop() {Node p = head, n = p;while (n.next != null && p != null) {p = p.next;n = n.next.next;if (p == n)return true;}return false;}// 使鏈表變為有迴圈的鏈表public void turnToLoopLink() {Node p = head;while (p.next != null) {p = p.next;}p.next = head.next;}// 去掉鏈表的迴圈public void turnToNoLoopLink() {Node p = head.next;while (p.next != null && p.next != head.next) {p = p.next;}p.next = null;}// 返還鏈表大小public int getSize() {Node p = head;int size = 0;while (p.next != null) {size++;p = p.next;}return size;}// 單鏈表按資料從小到大排序public void sortList() {Node p = head.next;int size = this.getSize();for (int i = 1; i < size; i++) {p = head.next;for (int j = 0; j < size - i; j++) {if (p.next != null && p.data > p.next.data) {int temp = p.data;p.data = p.next.data;p.next.data = temp;}p = p.next;}}}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.