在Java中旋转单链列表



我的函数:

import java.util.Collection;
import java.util.Collections;

public class LinkedList {
    private Node head;
    private int listCount;
    // konstruktor na povrzanata lista
    public LinkedList()
    {
        // ova e prazna lista, pa zatoa 'head' pokazuva kon nov jazok bez elementi (null)
        head = new Node(null);
        listCount = 0;
    }
    // dodavame element (data) na krajot od listata
    public void add(Object data)
    {
        Node temp = new Node(data);
        Node current = head;
        head.setData(temp.getData());
        // pocnuvajki od 'head' (pocetniot element) pominuvame niz site lemenenti
        while(current.getNext() != null)
        {
            current = current.getNext();
        }
        current.setNext(temp);
        // go zgolemuvame brojot na elementi vo listata
        listCount++;
    }
    // dodavame element (data) na posebno opredelena pozicija (index)
    public void add(Object data, int index)
    {
        Node temp = new Node(data);
        Node current = head;
        head.setData(temp.getData());
        // odime do baraniot index, ili do posledniot element od listata (do koj stigneme prvo)
        for (int i = 1; i < index && current.getNext() != null; i++)
        {
            current = current.getNext();
        }
        // go postavuvame sledniot element na novata jazla 
        // da pokazuva kon sledniot element na ovaa jazla
        temp.setNext(current.getNext());
        // a sega go postavuvame sledniot element na ovaa jazla
        // da pokazuva kon novata jazla
        current.setNext(temp);
        // go nakacuvame vkupniot broj na elementi vo listata
        listCount++;
    }
    // vraka element na opredelena pozicija vo listata
    public Object get(int index)
    {
        if(index <= 0)
        {
            return null;
        }
        Node current = head.getNext();
        for(int i = 1; i < index; i++)
        {
            if(current.getNext() == null)
            {
                return null;
            }
            current = current.getNext();
        }
        return current.getData();
    }
    // go vrakame brojot na elementi vo listata
    public int size()
    {
        return listCount;
    }
    // go brisime elementot na opredelenata pozicija vo listata
    public boolean remove(int index)
    {
        if(index < 1 || index > size())
        {
            return false;
        }
        Node current = head;
        for(int i = 1; i < index; i++)
        {
            if(current.getNext() == null)
            {
                return false;
            }
            current = current.getNext();
        }
        current.setNext(current.getNext().getNext());
        // go namaluvame brojot na elementi vo listata
        listCount--;
        return true;
    }
    public boolean contains(Object data)
    {
        Node temp = new Node(data);
        Node current = head;
        for(int i = 0; i < size(); i++)
        {
            if(temp.getData().equals(current.getData()))
            {
                return true;
            }
            else
            {
                current = current.getNext();
            }
        }
        return false;
    }
    public Node inserAfter(Object data, Node n)
    {
        Node temp = new Node(data);
        Node current = n.getNext();
        temp.setNext(current);
        n.setNext(temp);
        return temp;
    }
    public void rotateLeft()
    {
        Node temp = head;
        //head = head.getNext();
        //Node tail = head.getNext();
        if (head != null) {                       //otherwise it is empty list
            //Node temp = head;
            if (head.getNext() != null) {           //otherwise it is single item list
                head = head.getNext();
            }
        }
        Node tail;
        if (head.getNext() != null) {
            //more than 2 items in the list
            tail = head.getNext();
        } else {
            //only 2 items in the list
            tail = head;
        }
        while(tail.getNext() != null)
        {
            if (tail.getNext() != null) {
                tail = tail.getNext();
            }
            //tail = tail.getNext();
        }
        tail.setNext(temp);
        temp.setNext(null);
    }
    public void rotateRight()
    {
        Node temp = null;
        Node current = head;
        while(current.getNext() != null)
        {
            temp = current;
            current = current.getNext();
        }
        current.setNext(head);
        head = current;
        temp.setNext(null);
    }
    public void reverse()
    {
        Node reversedPart = null;
        Node current = head;
        while(current != null)
        {
            Node next = current.next;
            current.next = reversedPart;
            reversedPart = current;
            current = next;
        }
        head = reversedPart;
    }
    public Node copyList(Node source)
    {
        Node copyHead = null;
        Node copyTail = null;
        Node temp = new Node(source);
        Node current = head.getNext();
        for(int i = 0; i < size(); i++)
        {
            Node newNode = new Node(temp.getData());
            if(copyHead == null)
            {
                copyHead = newNode;
                copyTail = copyHead;
            }
            else
            {
                copyTail.setNext(newNode);
                copyTail = copyTail.getNext();
            }
        }
        return copyHead;
    }
    public Object setDataIndexOf(Object data, int index)
    {
        Node node = nodeAt(index);
        if(node == null)
        {
            return null;
        }
        else
        {
            Object old = node.getData();
            node.setData(data);
            return old;
        }
    }
    public Object dataAt(int index)
    {
        Node current = head.getNext();
        if(index < 1 || index > size())
        {
            return null;
        }
        for(int i = 0; i < index; i ++)
        {
            if(i != index - 1)
            {
                current = current.getNext();
            }
        }
        return current.getData();
    }
    public Node nodeAt(int index)
    {
        Node current = head.getNext();
        if(index < 1 || index > size())
        {
            return null;
        }
        for(int i = 0; i < index; i++)
        {
            if(i != index - 1)
            {
                current = current.getNext();
            }
        }
        return current;
    }
    public int indexOf(Object data)
    {
        Node temp = new Node(data);
        Node current = head.getNext();
        for(int i = 0; i < size(); i++)
        {
            if(current.getData().equals(temp.getData()))
            {
                return i;
            }
            current = current.getNext();
        }
        return -1;
    }
    public Object min()
    {
        Integer min = (Integer)head.getData();
        Node current = head;
        while(current.getNext() != null)
        {
            if((Integer)current.getData() < min)
            {
                min = (Integer)current.getData();
            }
            current = current.getNext();
        }
        return min;
    }
    public Object max()
    {
        Integer max = (Integer)head.getData();
        Node current = head;
        while(current.getNext() != null)
        {
            if((Integer)current.getData() > max)
            {
                max = (Integer)current.getData();
            }
            current = current.getNext();
        }
        return max;
    }
    public void removeSecondAppear(Object data)
    {
        Node temp = new Node(data);
        Node current = head;
        Node previous = null;
        boolean found = false;
        while(current != null)
        {                       
            if(current.getData().equals(temp.getData()) && current.getData() != null)
            {
                if(found == true)
                {
                    previous.setNext(current.getNext());
                    break;
                }
                else if(found == false)
                {
                    found = true;
                }
            }
            else{
                found = false;
            }
            previous = current;
            current = current.getNext();
        }
    }
    public String toString()
    {
        Node current = head.getNext();
        String output = "";
        while(current != null)
        {
            output += "[" + current.getData().toString() + "]";
            current = current.getNext();
        }
        return output;
    }
}

我的节点:

public class Node {
    Node next;
    Object data;
    public Node(Object _data)
    {
        next = null;
        data = _data;
    }
    public Node(Object _data, Node _next)
    {
        next = _next;
        data = _data;
    }
    public Object getData()
    {
        return data;
    }
    public void setData(Object _data)
    {
        data = _data;
    }
    public Node getNext()
    {
        return next;
    }
    public void setNext(Node _next)
    {
        next = _next;
    }
}

我正在尝试创建一个函数来旋转Java中的单链接列表。我为左和右做了两个函数。

我的左侧功能似乎可以工作,但还不完全。例如如果我的列表包含以下元素:1 2 3

然后我的轮换左将使列表变成23。1将消失。但它应该是在前三名的基础上出现的。

至于rotateRight,由于某种原因,该功能无法正常运行,已经尝试了几个小时,但找不到解决方案。

    LinkedList LL = new LinkedList();
    LL.add("1");
    LL.add("2");
    LL.add("3");
    LL.add("4");
    LL.add("4");
    LL.add("5");
    LL.rotateLeft();

2,3,4,4,5,null为输出。我的添加方法如下:

public void add(Object data)
{
    Node temp = new Node(data);
    Node current = head;
    while(current.getNext() != null)
    {
        current = current.getNext();
    }
    current.setNext(temp);
    listCount++;
}

尝试此更正:

public void rotateLeft() {
  if (head != null) {                       //otherwise it is empty list
    Node temp = head;
    if (head.getNext() != null) {           //otherwise it is single item list
        head = head.getNext();
    }
    Node tail;
    if (head.getNext() != null) {
        //more than 2 items in the list
        tail = head.getNext();
    } else {
        //only 2 items in the list
        tail = head;
    }
    while (tail.getNext() != null) {
        if (tail.getNext() != null) {
            tail = tail.getNext();
        }
    }
    tail.setNext(temp);
    temp.setNext(null);
  }
}
public void rotateRight() {
  if (head != null) {                       //otherwise it is empty list
    Node tail = null;
    Node current = head;
    while (current.getNext() != null) {
        tail = current;
        current = current.getNext();
    }
    if (tail != null) {             //otherwise it is single item list
        tail.setNext(null);
        current.setNext(head);
        head = current;
    }
  }
}

您也没有处理空列表、单个项目列表或列表中只有2个项目的情况。

编辑:

代码:

public class LinkedList {
    Node head;
    public LinkedList() {
        this.head = null;
    }
    public LinkedList(Node head) {
        this.head = head;
    }
    public void rotateLeft() {
        System.out.println("<-");
        if (head != null) { // otherwise it is empty list
            Node temp = head;
            if (head.getNext() != null) { // otherwise it is single item list
                head = head.getNext();
            }
            Node tail;
            if (head.getNext() != null) {
                // more than 2 items in the list
                tail = head.getNext();
            } else {
                // only 2 items in the list
                tail = head;
            }
            while (tail.getNext() != null) {
                if (tail.getNext() != null) {
                    tail = tail.getNext();
                }
            }
            tail.setNext(temp);
            temp.setNext(null);
        }
    }
    public void rotateRight() {
        System.out.println("->");
        if (head != null) { // otherwise it is empty list
            Node tail = null;
            Node current = head;
            while (current.getNext() != null) {
                tail = current;
                current = current.getNext();
            }
            if (tail != null) { // otherwise it is single item list
                tail.setNext(null);
                current.setNext(head);
                head = current;
            }
        }
    }
    public void printList() {
        Node cursor = head;
        while (cursor != null) {
            System.out.print(cursor.data + ", ");
            cursor = cursor.getNext();
        }
        System.out.println();
    }
    public void add(Object data) {
        Node temp = new Node(data);
        if (head == null) {
            head = temp;
        } else {
            Node current = head;
            while (current.getNext() != null) {
                current = current.getNext();
            }
            current.setNext(temp);
        }
    }
    public static void main(String[] args) {
        LinkedList r = new LinkedList();
        r.add(1);
        r.add(2);
        r.add(3);
        r.add(4);
        r.add(4);
        r.add(5);
        r.printList();
        r.rotateLeft();
        r.printList();
        r.rotateLeft();
        r.printList();
        r.rotateRight();
        r.printList();
        r.rotateRight();
        r.printList();
    }
}

输出:

1, 2, 3, 4, 4, 5, 
<-
2, 3, 4, 4, 5, 1, 
<-
3, 4, 4, 5, 1, 2, 
->
2, 3, 4, 4, 5, 1, 
->
1, 2, 3, 4, 4, 5, 

相关内容

  • 没有找到相关文章

最新更新