-1

I want to perform a search operation in a priority linked list in java, I have already write a search and delete method for that but i am facing difficulties in calling these methods to my main function? here is my code so far.

import java.util.*;

class Node {

public static Node next = null;
public static int item = 0;

public Node(int item) {
    this.item = item;
}

}

public class Main {

static PriorityQueue<String> stringQueue;

public static void main(String[] args) {

    stringQueue = new PriorityQueue<String>();

    stringQueue.add("1");
    stringQueue.add("2");
    stringQueue.add("3");
    stringQueue.add("6");
    stringQueue.add("6");
    stringQueue.add("5");
    stringQueue.add("9");
    stringQueue.add("8");
    stringQueue.add("7");
    stringQueue.add("4");

    while (stringQueue.size() > 0)
        System.out.println(stringQueue.remove());
    Node head = null;

}

public static Node searchNodeFIFO(Node head, int item) {
    System.out.println("In Search Node");

    Node cHead = head;

    while (cHead != null) {
        if (cHead.item == item)
            return cHead;

        cHead = cHead.next;
    }

    return null;
}

public Node deleteNodeFIFO(Node head, int item) {
    System.out.println("In Delete Node");

    if (head.item == item)
        return head.next;

    Node cNode = head;
    Node nNode = head.next;

    while (nNode != null) {
        if (nNode.item == item) {
            cNode.next = nNode.next;
            break;
        } else {
            cNode = nNode;
            nNode = nNode.next;
        }
    }

    return head;
}

public void printLinkList(Node head) {
    while (head != null) {
        System.out.println(head.item);
        head = head.next;
    }
}

}

  • Unfortunately "I am facing difficulties ..." is a statement, not a question. Please be specific about the problem you're having. – Bernhard Barker Apr 26 '13 at 06:37
  • I must say the provided code seems very unhelpful; what is `head`? Why is it an attribute of main? Why are the `Node` attributes static? Every instance of `Node` will share values; every item in the structure that holds the Nodes (which you've not provided) will be the same. – Anti Earth Apr 26 '13 at 06:41
  • `while (stringQueue.size() > 0) System.out.println(stringQueue.remove());` - you just removed all of them, there's nothing left to search. Also, you're mixing data structures, it appears that you shouldn't be using a `PriorityQueue` at all. – Bernhard Barker Apr 26 '13 at 09:23

1 Answers1

1

I'm going to take a wild guess and figure it's because of the attributes in your Node class.
Why are you declaring them as static?
This causes every instance of Node to share the same attribute (value and next) values.

Every item in the structure that holds them will be the same.

Trying removing the static modifier from your Node attribute declarations.

See how and where to use static modifiers

Community
  • 1
  • 1
Anti Earth
  • 4,671
  • 13
  • 52
  • 83