Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1090457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:22:32+00:00 2026-05-16T23:22:32+00:00

Hello I am trying to implement a Priority Queue in Java from scratch with

  • 0

Hello
I am trying to implement a Priority Queue in Java from scratch with a linked list but I am having a problem sorting elements on insertion. Here is my program thus far, any help would be massively appreciated.

import java.util.Scanner;

public class T0 {

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

        PQ myList = new PQ();

        myList.addSort("Z");
        myList.addSort("B");
        myList.addSort("C");
        myList.addSort("B");
        myList.addSort("Z");

        System.out.println(myList.view(0));
        System.out.println(myList.view(1));
        System.out.println(myList.view(2));
        System.out.println(myList.view(3));
        System.out.println(myList.view(4));
    }
}


class PQ {

    Node tail = new Node(null, null);
    int elementCount = 0;

    Node lastAdded = tail;

        public void add(String word) {
        Node added = new Node(word, lastAdded);
        lastAdded=added;
        elementCount++;
    }

    public void addSort(String word){
                Node temp = new Node(null, null);
        for(int n = 0; n<elementCount && word.compareTo(lastAdded.next().toString()) >1; n++){
            temp=lastAdded.next();
        }
        Node added = new Node(word, lastAdded.next());
        lastAdded.changeNext(added);
        elementCount++;
    }

    public String view(int i){
        Node temp = lastAdded;

        for(int n = elementCount; n > i; n--){
            temp=temp.next();
        }
        return temp.toString();

    }
    public String toString() {
        return lastAdded.toString();
    }



    class Node {

        String name;
        Node nextNode;

        public Node(String s, Node n) {
            name = s;
            nextNode = n;
        }
        public void changeNext(Node n){
            nextNode=n;
        }
        public Node next() {
            return nextNode;
        }

        public String toString() {
            return name;
        }
    }
}

Currently outputs:

   run:
Z
B
C
B
Z
BUILD SUCCESSFUL (total time: 1 second)

Upadate:
Changed addSort to be:

    public void addSort(String word){
            Node temp = lastAdded;
for(int n = 0; n<elementCount && word.compareTo(lastAdded.next().toString()) > 0; n++){
    temp=lastAdded.next();
}
    Node added = new Node(word, lastAdded.next());
    lastAdded.changeNext(added);
    elementCount++;
    lastAdded=temp;
} 

This throws a null pointer exception at

System.out.println(myList.view(0));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:22:33+00:00Added an answer on May 16, 2026 at 11:22 pm

    In the loop

        for(int n = 0; n<elementCount && word.compareTo(lastAdded.next().toString()) >1; n++){
            temp=lastAdded.next();
        }
    

    you are always comparing the new word to the same element, instead of iterating through the list (1a) (and you keep assigning temp the same value inside the loop (1b)). [Update] And you compare the output of compareTo to 1 instead of 0 (2). Thus – depending on the implementation of compareTo – the result may always be false. (AFAIK it would not be for String.compareTo specifically, since it can return values greater than 1 – but this is not guaranteed in general.) [/Update]

    And then, regardless of the result of your checks, you always add the new element after the last added element (3).

    However, since you don’t adjust lastAdded (4), it will keep pointing to the same element (tail), so in effect tail will always be the first item in your list, not the last.

    Update 2: in your updated addSort, you fixed issues (2) and (4) above, but (1a-b) and (3) are still there.

    Part of the problem is that for a singly linked list to work, you need to keep a reference to its head at all times – otherwise you have no way to walk through it! You are sort of trying to use lastAdded for this purpose, but this is just mixing two different things, causing further confusion. Note that you don’t actually need a reference to the last added node – this information is of no use when you are about to insert the next element into the list. I recommend bringing a dedicated head reference into the picture, and changing the code accordingly (and dropping lastAdded unless you are sure you are going to need it later). Note that this does not eliminate the need for (4) – even if you have a head reference only, you still need to modify it (although not always – only when inserting at the beginning of the list).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys, I have been trying to implement the DSUM function but failed to
hello i am trying to load a list of filenames from a folder into
Hello I am trying to implement Readability API in my app, but all I
Hello im trying to implement a websocket communication between client and server. the problem
I'm trying to implement ActionBarSherlock in my existing application, but I've been having some
Hello i'm trying to implement an AsynController, here is my code: [NoCache] public class
Hello everyone i am trying to implement push notifications with urbanairship i followed the
I am trying to implement a hello world application for the iPhone and i've
Hello im trying to echo only the 50 letters, but something in my code
hello i am trying a sqlite database tutorial to build, but its not working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.