簡體   English   中英

通過單鏈表插入實現的優先隊列

[英]Priorityqueue implemented via singly linked list insert

我正在使用通用鏈表來實現優先級隊列,其中我使用可比較的 function 插入 function ,它會找到一個大小大於當前節點的插槽。 我實際上有問題添加 function 根據優先級隊列要求插入元素。 優先級隊列從小到大應該是 go。

編輯:我意識到問題在於插入一個比頭部大的數字。 在添加 11 期間,function 僅與 5 比較並在 5 之后添加,這會破壞序列。

當前 output

PQ: 5
PQ: 5,10 
PQ: 5,9,10
PQ: 5,11,9,10
PQ: 5,7,11,9,10
PQ: 2,5,7,11,9,10

所需 output

PQ: 2,5,7,9,10,11

我加 function

public class PQLinkedList<T extends Comparable<T>>{

private class Node{
    private T data;
    private Node next;


    // Constructor that takes in data to input for the node
    public Node(T data) {
        this.data = data;
        this.next = null;
    }
}


private int length = 0;
private Node head;
int cmp = 0;
public PQLinkedList() {
    head = null;
}

//Compare with each element till it finds a suitable position to insert itself
//Ascending order
//INCOMPLETE
//Compare part of this code is not complete!
public void addPQ( T data) {
    Node node = new Node(data);
    Node temp2 = null;

    if ( head == null) {
        addFirst(data);
    }

    else {
        Node curr = head;
        int count = getSize();


        while ( count != 0) {    

            cmp = data.compareTo(curr.data);


            if ( cmp < 0 ) {
                addFirst(data);
                break;
            }
            else if (cmp>=0 ){         
                if ( curr.next == null) {
                curr.next = node;
                break;
                }
                // if there curr.next is not empty
                // Move all the nodes towards to tail by 1
                else {

                    // after = one space after pos
                    // PROBLEM
                temp2 = curr.next;
                  Node after = curr.next;
                    while( after != null) {
                        after = after.next;
                    }
                    node.next = temp2;    
                    curr.next = node;
                    break;
                }


            }



            else {
            curr = curr.next;
            }
            count--;
        }
    }
    length++;

}





private void addFirst(T data) {
    Node node = new Node(data);
    if ( head == null ) {
        head = node;
    }
    else {
        Node temp = head;
        head = node;
        node.next = temp;
    }
    length++;
}

//TO-DO
public void remove( T data) {
    if ( !search(data)) {
        System.out.println("Linked list does not contain that element!");
        return;
    }
    else if ( head.data == data) {
head = head.next;
    }

    else {
        // If curr is the node to be deleted.
        // link from previous node to curr, link from curr to next node
        //Traverse through the linkedlist till it finds the node to be deleted and skip it
        Node curr = head;
        while ( curr != null & curr.next != null ) {
            if ( curr.next.data == data) {
                //Check if the node 2 next after curr is null
                //if so, remove curr.next which contains the value that we want to delete
                if ( curr.next.next != null) {
                    curr.next = curr.next.next;
                }
                //curr.next.next is null so just curr.next which contains the value we want to delete
                else {
                    curr.next = null;
                }
            }
            //Traverse the curr node
            else {
                curr = curr.next;
            }

        }
        length--;
    }
}


// Retrieves and removes the head of the priority queue
public T poll() {
    if ( isEmpty()) {
        System.out.println("Linked list is empty!");
        return null;
    }
    else {
        Node temp = null;
        temp = head ;
        head = head.next;
        length--;
        return temp.data;
    }

}

// Retrieves the head of the priority queue
    public T peek() {
        if ( isEmpty()) {
            System.out.println("Linked list is empty!");
            return null;
        }
        else {
            return head.data;
        }

    }

    public void clear() {
        Node curr = head;
        while ( curr != null) {
            curr = curr.next;
        }
    }


public int getSize() {
    return length;
}



public boolean search(T data) {
    if ( isEmpty()) {
        System.out.println("Linked list is empty!");
    return false;
    }
    else {
        Node node = head;
        while ( node != null ) {
            if ( node.data == data) {
                return true;
                }
            node = node.next;
        }
        return false;
    }
}


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

@Override
public String toString() {
    String str = "PQ: ";
    Node node = head;
    while ( node != null) {
        str = str + node.data;
        if ( node.next != null) {
            str = str + ",";
        }
        node = node.next;

    }
    return str;
}

}

我的主要

public class priorityQueueImplementation {

public static void main(String[] args) {

    PQLinkedList<Integer> test = new PQLinkedList<Integer>();
    test.add(5);
    test.add(10);
    test.add(9);
    test.add(11);
    test.add(7);
    test.add(2);

    System.out.println( test.toString());


}

  }

由於這是一個單鏈表,我們不能將節點添加到前一個節點。 我們只能添加到后續節點。 在節點插入期間,我們需要將data與列表進行比較。 每個節點一個,有5種情況:

  1. current為null, data成為新頭
  2. data小於currentdata成為新的頭
  3. data大於currentdata小於next currentnext之間插入data
  4. data大於currentnext是 null。 current之后插入data
  5. data大於currentdata大於next 等待下一次迭代
    public void addPQ(T data) {
        Node node = new Node(data);

        Node curr = head;

        if (head == null) {
            // current is null, data become the new head
            addFirst(data);
        } else {

            while (curr != null) {
                int cmpLeft = data.compareTo(curr.data);

                if (cmpLeft < 0) {
                    // data is smaller than current, `data` become the new head
                    addFirst(data);
                    break;
                } else {
                    // data is greater than current
                    if (curr.next == null) {
                        // next is null. Insert `data` after `current`
                        addAfter(curr, node);
                        break;
                    } else {
                        int cmpRight = data.compareTo(curr.next.data);
                        if (cmpRight < 0) {
                            // data is smaller then next. Insert data between current and next
                            addAfter(curr, node);
                            break;
                        }
                    }
                    // data is greater then next. Wait for next iteration
                    curr = curr.next;
                }

            }

        }

    }

    private void addAfter(Node currNode, Node newNode) {
        if (currNode == null) {
            currNode = newNode;
        } else {
            Node tempTail = currNode.next;
            currNode.next = newNode;
            newNode.next = tempTail;
        }
        length++;
    }

output

PQ: 2,5,7,9,10,11

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM