Home > other >  How to turn a linked list into a priority queue?
How to turn a linked list into a priority queue?

Time:07-31

I'm trying to take a linked list and have it mimic a priority queue. I'm trying to add names alphabetically to my list and I'm able to add them correctly to the very front and back of the list, but I can't get them to fill in anywhere else.

public static void myPush(String names)
    {
        if(myList.isEmpty())
        {
            myList.add(names);
        }
        else if(myList.size() == 1)
        {
            if(names.compareTo(myList.get(0)) < 0)
            {
                myList.push(names);
            }
            else 
            {
                myList.add(names);
            }
        }
        //checks the first name
        else if(names.compareTo(myList.get(0)) < 0)
            {
                myList.push(names);
            }
        //checks the last name
        else if(names.compareTo(myList.get(myList.size() - 1)) > 0)
            {
                myList.add(names);
            }
            //checks the rest of the queue
            else
            {
                for(int i = 1; i < myList.size() - 1; i  )
                {
                    if(names.compareTo(myList.get(i)) <= 0 && names.compareTo(myList.get(i - 1)) >= 0)
                    {
                        myList.add(i,names);
                    }
                }
            }
        }

The above code will run, but won't add the name to the list if it isn't being added to the front or back. For example, if I add bob and ann to my list, it works fine, but if I try to add the name art, which should go in between the two names, I end up with just bob and ann being printed. I'm not sure what I'm doing wrong, but how can I fix my code so names can be added to the middle of the list?

Here's the rest of the code for reference:


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Iterator;

public class Lab05MU {

    static LinkedList<String> myList = new LinkedList<String>();
    static ArrayList<String> myarrayList = new ArrayList<String>();
    static Object[] myArray = myList.toArray();
    static Iterator<String> it = myList.iterator();
    
    
    
    //convert from linkedlist to arraylist
    
    
    
    
    
    
    
    //pushes a name in the correct order
    public static void myPush(String names)
    {
        if(myList.isEmpty())
        {
            myList.add(names);
        }
        else if(myList.size() == 1)
        {
            if(names.compareTo(myList.get(0)) < 0)
            {
                myList.push(names);
            }
            else 
            {
                myList.add(names);
            }
        }
        //checks the first name
        else if(names.compareTo(myList.get(0)) < 0)
            {
                myList.push(names);
            }
        //checks the last name
        else if(names.compareTo(myList.get(myList.size() - 1)) > 0)
            {
                myList.add(names);
            }
            //checks the rest of the queue
        else
            {
                for(int i = 1; i < myList.size() - 1; i  )
                {
                    if(names.compareTo(myList.get(i)) <= 0 && names.compareTo(myList.get(i - 1)) >= 0)
                    {
                        myList.add(i,names);
                    }
                }
            }
        }
        
                    
                        
                
                
            
        
            
        
            
    
    
    //prints the current list
    public static void printList()
    {
        
        if(myList.isEmpty())
        {
            System.out.println("The queue is empty");
        }
        else
        {
            String list = "";
            for(int i = 0; i < myList.size(); i  )
            {
                list  = myList.get(i)   " -> ";
            }
            
            list = list.substring(0,list.length() - 4);
            System.out.println(list);
            System.out.println();
        }
        
    }
    
    
    public static void main(String[] args) {
        
        int choice;
        
        
        System.out.println("Welcome to the Priority Queue System");
        
        
        
        do 
        {
            
        
            System.out.println("Select an Option");
            System.out.println("1. Push an element on the priority queue");
            System.out.println("2. Peek at an element on the priority queue");
            System.out.println("3. Pop an element from the priority queue");
            System.out.println("4. Check if the priority queue is empty");
            System.out.println("5. Get the size of the priority queue");
            System.out.println("6. Print the contents of the priority queue");
            System.out.println("7. Quit");
            
            Scanner scn = new Scanner(System.in);
            choice = scn.nextInt();
            
            
            switch(choice)
            {
            case 1:
                //push
                
                System.out.println("Enter a name to add to the priority queue.");
                System.out.println();
                String name = scn.next();
                myPush(name);
                
                break;
                
            case 2:
                //peek
                break;
                
            case 3:
                //pop
                break;
                
            case 4:
                //isempty
                System.out.println(myList.isEmpty()?"The queue is empty":"The queue is not empty");
                break;
                
            case 5:
                //getsize
                System.out.println("The size of the queue is "   myList.size());
                break;
                
            case 6:
                //print
                printList();
                
                break;
                
            case 7:
                //quit
                System.out.println("Goodbye");
                break;
            }

    }
        while(choice != 7);
    }
}


CodePudding user response:

Your code never executes the for loop when the linked list has 2 elements ( i starts from 1 and the condition is i < 2-1 ). In order to execute it properly you have to change the break condition as

for(int i = 1; i < myList.size(); i  ) //not myList.size()-1

furthermore you have to exit from loop when the element has been added to the list otherwise myList.size() raises of 1 and the loop never exits.

if(names.compareTo(myList.get(i)) <= 0 && names.compareTo(myList.get(i - 1)) >= 0)
{
    myList.add(i,names);
    break;
}
  • Related