shaokel
2019-12-11 20:39
采纳率: 98.6%
浏览 126
已采纳

请求大佬支援!!!单链表合并错误

结果应该是[1]2]3]4]5]6]7]8]

合并后得到的结果却是[5][6][7][8]

我只是想把两个单链表单纯的连在一起

public class SingleList{

    private Node head;//头指针
    private Node rear;//尾指针
    private Node point;//临时指针
    private int length;//长度

    public SingleList() {//创建无数据的头结点
        head = new Node();
        rear = head;  //尾指针指向头指针
        length = 0;
    }

    public void rearadd(Object element) {//从尾部插入指针
        point = new Node(element);
        rear.next = point;
        rear = point;
        length++;
    }

    public boolean isEmpty() {
        int length = size();
        if(length==0) {
            return true;
        }
        return false;
    }

    public int size() {
        return length;
    }

    public Object get(int index) {
        Node temp = head;
        int i = 1;

        if(index == 0) {
            return head;
        }

        while(temp.next!=null) {
            if(index==i) {
                return temp;
            }
            temp = temp.next;
            i++;
        }

        return temp;
    }

    public Object get(Object element) {
        return null;
    }

    //更改指定位置的元素
    public void set(int index, Object element) {
        if (index >= 0 && index < length) {
            point = movePoint(index);
            point.next.data = element;
            point.data = null;
        } else {
            System.out.println("修改失败,没有指定位置元素");
        }
    }

    //连接两条链表
    public void connect(SingleList b) {
        this.rear.next = b.head.next;
        this.length += b.length;
    }

    public int add(Object element) {
        int i=0;

        //新建一个节点
        Node newnode = new Node(element);

        Node temp = head;    //一个移动的指针(把头结点看做一个指向结点的指针)
        while(temp.next != null){    //遍历单链表,直到遍历到最后一个则跳出循环。
            temp = temp.next;        //往后移一个结点,指向下一个结点。
            i++;
        }
        temp.next = newnode;    //temp为最后一个结点或者是头结点,将其next指向新结点

        //链表为空
        return i;
    }


    public void insert(int index, Node element) {
        //首先需要判断指定位置是否合法,
        if(index<1||index>size()){
            System.out.println("插入位置不合法。");
            return;
        }

        point = movePoint(index);
        Node temp = new Node(element);

        temp.next = point.next;
        point.next = temp;
        length++;

    }

    public Object remove(int index) {
        //判断index是否合理
        if(index<1 || index>size()){
            System.out.println("给定的位置不合理");
            return null;
        }

        point = movePoint(index);
        Node temp = point.next;

        point.next = temp.next;

        length--;

        return temp;
    }

    //移动指针到指定位置
    private Node movePoint(int position) {
        if(position<0) {
            return head;
        }
        if(position>length) {
            return rear;
        }
        if(position >= 0 && position <= length) {
            point = head;
            while(point!=null) {
                if(position==0) 
                    break;
                position--;
                point = point.next;
            }
        }
        return point;
    }

    //清除链表内容
    public void clear() {
        while(head.next!=null) {
            head.next = head.next.next;
        }
        rear =head;//回归初始状态
        point= null;
        length = 0;
        System.gc(); //请求系统清理垃圾
    }

    public void tostring() {
        point = head;
        while(point.next!=null) {
            System.out.print("["+point.next.data+"]");
            point = point.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        SingleList a = new SingleList();
        SingleList b = new SingleList();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(4);
        b.add(5);
        b.add(6);
        b.add(7);
        b.add(8);
        a.connect(b);
        a.tostring();
    }
}

class Node{
    Node next = null;
    Object data;

    public Node() {
        this.data = null;
    }

    public Node(Object data) {
        this.data = data;
    }
}

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题