zoukankan      html  css  js  c++  java
  • 循环链表链式存储Java实现

    package com.wrh.lab.dataStructure.linearList;

    /**
    *
    *
    @author wrh
    * the interface of the linked list
    *
    */

    public interface LinkedList<E> {

    /**
    * insert the item at prior
    *
    @param item
    */

    public void insertAtPrior(E item);

    /**
    * insert the item at the end of the list
    *
    @param item
    */

    public void insertAtLast(E item);

    /**
    * insert the node item after the index
    *
    @param index
    *
    @param item
    */

    public void insert(int index, E item);

    /**
    * remove the first node from the list
    */

    public void removeFromFront();

    /**
    * remove the last node from the list
    *
    @return the removed node
    */

    public void removeFromLast();

    /**
    * remove the node in the index
    *
    @param index
    */

    public void remove(int index);
    /**
    * return true for empty and false for not empty
    *
    @return true : empty; false : not empty
    */

    public boolean isEmpty();

    /**
    *
    *
    @return the length of the list
    */

    public int getLength();

    /**
    * display all the nodes
    */

    public void display();
    }
    package com.wrh.lab.dataStructure.linearList;

    public class SingleListNode<E> {
    private SingleListNode<E> next;
    private E data;

    public SingleListNode(E data) {
    this.data = data;
    next = null;
    }

    public SingleListNode(E data, SingleListNode<E> nextNode) {
    this.data = data;
    next = nextNode;
    }

    /**
    *
    *
    @param data
    */

    public void setData(E data) {
    this.data = data;
    }

    /**
    *
    *
    @param next
    */

    public void setNext(SingleListNode<E> next) {
    this.next = next;
    }

    /**
    * get the data
    *
    @return the data
    */

    public E getData() {
    return data;
    }

    /**
    *
    *
    @return next node
    */

    public SingleListNode<E> getNext() {
    return next;
    }
    }
    package com.wrh.lab.dataStructure.linearList.impl;
    /**
    *
    @author wrh
    * the implementation of the CircularLinkedList
    */

    import com.wrh.lab.dataStructure.linearList.LinkedList;
    import com.wrh.lab.dataStructure.linearList.SingleListNode;

    public class CircularLinkedListImpl<E> implements LinkedList<E> {
    private SingleListNode<E> head;
    private int length; //the length of the list

    public CircularLinkedListImpl() {
    head = new SingleListNode(null, head);
    length = 0;
    }

    @Override
    public void insertAtPrior(E item) {
    SingleListNode<E> node = new SingleListNode<E>(item, null); //encpsule the item to an node
    node.setNext(head.getNext()); //node.next = head.next
    head.setNext(node); //head.next = node
    length ++;

    }

    @Override
    public void insertAtLast(E item) {

    SingleListNode<E> tmp = head;

    if (isEmpty()) { // if the list is null
    SingleListNode<E> node = new SingleListNode<E>(item, head); // .. if next == null ?
    head.setNext(node);

    } else {
    SingleListNode<E> node = new SingleListNode<E>(item, head);
    // find the end node of the list
    while (head != tmp.getNext()) {

    tmp = tmp.getNext();
    }
    tmp.setNext(node);
    }

    length++;
    }

    @Override
    public void insert(int index, E item) {
    SingleListNode<E> node = new SingleListNode<E>(item, null);
    SingleListNode<E> tmp = head;
    int i = 1;

    if (index > length || index < 0) {
    System.out.println("the index is out of bounds");
    } else if (0 == length && 1 == index) {
    node.setNext(head);
    head.setNext(node);
    length++;
    } else {
    //find the node index
    while (head != tmp.getNext() && i <= index) {

    tmp = tmp.getNext();
    i++;
    }
    node.setNext(tmp.getNext());
    tmp.setNext(node);
    length++;
    }
    }

    @Override
    public void removeFromFront() {
    SingleListNode<E> tmp = head;
    if (length < 1) {
    System.out.println("The list is null and you can not delete any node!");
    } else if (1 == length) {
    head.setNext(head);
    length--;
    } else {
    head.setNext(tmp.getNext().getNext());
    length--;
    }
    }

    @Override
    public void remove(int index) {
    if (length < 1 || index > length) {
    System.out.println("index is out of bounds");
    } else if (1 == length && 1 == index) {
    head.setNext(head);
    length--;
    } else {
    SingleListNode<E> tmp = head;
    int i = 1;

    //get the node before index
    while (head != tmp.getNext() && i < index) {

    tmp = tmp.getNext();
    i++;
    }
    tmp.setNext(tmp.getNext().getNext());
    length--;
    }
    }

    @Override
    public void removeFromLast() {
    if (length < 1) { // if the list is null
    System.out.println("The list is null and you can not delete");

    } else if (1 == length) {
    head.setNext(head);
    length--;
    } else {
    SingleListNode<E> tmp1 = head;
    SingleListNode<E> tmp2 = head.getNext(); //set tmp2 -tmp1 = 1

    while (head != tmp2.getNext()) {
    tmp2 = tmp2.getNext();
    tmp1 = tmp1.getNext();
    }
    tmp1.setNext(head);
    length--;
    }
    }

    @Override
    public int getLength() {
    return length;
    }

    @Override
    public boolean isEmpty() {
    boolean isEmpty = false;
    if (0 == length) {
    isEmpty = true;
    } else {
    isEmpty = false;
    }
    return isEmpty;
    }


    @Override
    public void display() {
    if (length < 1) {
    System.out.println("The list is null");
    } else {
    SingleListNode<E> tmp = head;
    while (head != tmp.getNext()) {
    tmp = tmp.getNext();
    System.out.print(tmp.getData() + " ");
    }
    }
    }

    //test the list
    public static void main(String[] args) {

    LinkedList<Integer> l = new SingleLinkedListImpl<Integer>();
    System.out.println(l.isEmpty());
    l.insertAtLast(1);
    l.insertAtPrior(2);
    l.insert(2, 5);
    System.out.println("the list is : ");
    l.display();
    System.out.println();
    System.out.println("the length is :" + l.getLength());
    l.remove(2);
    l.display();
    }
    }




  • 相关阅读:
    这一年来
    网络流复习笔记
    Codeforces Round #431
    Codeforces Round #398 (Div. 2)
    Codeforces Round #418 (Div. 2)
    【Codeforces 98E】 Help Shrek and Donkey 游戏策略神题
    【bzoj1878】[SDOI2009]HH的项链
    Round 403 div. 2
    Codeforces Round #417 (Div. 2)
    Codeforces Round #416 (Div. 2)
  • 原文地址:https://www.cnblogs.com/wrh526/p/2354591.html
Copyright © 2011-2022 走看看