将两个已排序的链接列表合并到第三个链接列表中(排序和合并错误)

时间:2014-03-31 22:08:56

标签: java sorting singly-linked-list

以下代码用于执行列出的任务

1:创建3个链接列表 2:填充前两个 3:排序前两个 4:通过从前两个中删除节点来合并前两个链表和第三个链表,保存数据,并将其插入第三个列表

执行此操作的代码如下

import java.util.*;
import java.util.Scanner;

class Node { //stores int values

public int data;
public Node next;

public Node(int d){
    data = d;
    next = null;
}
//-------------------------------------------------------------------
public void displayNode() { 
System.out.print(data);
}
//-------------------------------------------------------------------
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
class NodeList {

private Node first;

public NodeList(){
    first = null;
}
//-------------------------------------------------------------------
public boolean isEmpty(){
    return first == null;
}
//-------------------------------------------------------------------
public void inputList(int n){

int x = 0;
Node newNode = null;
Node last = null;
Random randomGenerator = new Random();

Scanner in = new Scanner(System.in);

for (int i = 0; i < n; i++){

    //System.out.printf("Enter data for node %d: ", i + 1);
    x = randomGenerator.nextInt(10);
    newNode = new Node(x);

    if (isEmpty()){ 
        first = newNode;
        last = newNode; 
        }
    else { 
        last.next = newNode;
        last = last.next; 
        }
    } 
System.out.println();
}
//-------------------------------------------------------------------
public void selectionSort(){

Node p = null;
Node q = null;
Node r = null;
int t = 0;

if (isEmpty()){
    System.out.println("List is empty");
    }
else if(first.next != null){ 
    p = first;
    while (p.next != null){
        q = first;
        while (q.next != null){
            if (q.data > q.next.data){
                r = q.next;
            }
            q = q.next;
        }
        //System.out.printf("%d\n", r.data);
        t = p.data; 
        p.data = r.data;
        r.data = t;
        p = p.next;
    }
    //System.out.println("List is sorted");
    }
}
//-------------------------------------------------------------------
public int deleteFirst(){ 

    int x = 0;

    if (isEmpty()){
        System.out.println("List is empty");
    }
    else {
        x = first.data;
        first = first.next;
        //System.out.println("First node is deleted");
    }
    return x;
}
//-------------------------------------------------------------------
public void displayList(){

Node curr = null; 

if (isEmpty()){ 
    System.out.println("null");
}
else{
    curr = first;
    while (curr != null){

        curr.displayNode();
        System.out.printf("\n");
        curr = curr.next;
        }
    }
System.out.printf("\n");
}
//-------------------------------------------------------------------
public void insert(int key){ 

    Node prev = null;
    Node curr = first;
    Node newNode = new Node(key);

    while ( (curr != null) &&(curr.data < key) ){

        prev = curr;
        curr = curr.next;
        }

    if (curr == first){
        first = newNode;
        }

    else {
        prev.next = newNode;
        newNode.next = curr;
    }
}
//-------------------------------------------------------------------
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
class MergeLists {

public static void main(String[] args){
Scanner in = new Scanner(System.in);

NodeList List_A = new NodeList();
NodeList List_B = new NodeList();
NodeList List_C = new NodeList();
String s;
String next;
char choice;
boolean continued = true;

List_A.inputList(5);
List_A.selectionSort();
List_A.displayList();

List_B.inputList(5);
List_B.selectionSort();
List_B.displayList();

while (continued){
System.out.printf("enter choice: \n");
System.out.printf("A: merge two ordered lists\n");
System.out.printf("B: view merged ordered list\n");
System.out.printf("C: exit\n");

s = in.nextLine();
choice = s.charAt(0);

switch(choice){

    case 'a':
        Mergelist(List_A, List_B, List_C);
        break;

    case 'b':
        List_C.displayList();
        break;

    case 'c':
        continued = false;
        break;

    default:
        System.out.print("Invalid entry\n");
    }//end switch
}//end while

}
public static void Mergelist(NodeList a, NodeList b, NodeList c){
    int x = 0;

    if (a.isEmpty() == false){
        while (a.isEmpty() == false){
            x = a.deleteFirst();
            c.insert(x);
        }
    }

    if (b.isEmpty() == false){
        while (b.isEmpty() == false){
            x = b.deleteFirst();
            c.insert(x);
        }
    }
}
}
然而,问题在于数字3和4.排序列表的代码不稳定。有时候它会完全分类,有时甚至几乎没有分类。此外,对于第4个,在某些情况下,只有2个节点被添加到第3个列表,其他时候,所有都将被添加,按顺序

我不确定为什么第三个不排序,但我相信第三个问题导致第四个问题

如果可以,我们将不胜感激任何协助

0 个答案:

没有答案