0

I think I'm having trouble with my Queue class because I know using a Queue uses the FIFO method, but want to make sure that when I add an element that it is added at the end of the Queue. In my main program I have added numbers 1-4, but when I want to print my entire Queue using the toString method in my Queue class it will only print out the first element which is 1. I would appreciate some help!

Thank you!

 public class QuestionFive
    {
       public static void main(String[] args)
       {
          // creating a queue
          Queue q = new Queue();
    
          // adding numbers 1,2,3 and 4
          q.insert(1);
          q.insert(2);
          q.insert(3);
          q.insert(4);

          System.out.println(q);
       }
    }
    class Queue 
    {
       //Private Data Member
       private Link _head;
       
       //Constructor: A constructor to create an empty Queue,
       public Queue() 
       {
          _head = null;
       }
       
       //Insert Method: A method to insert a given Object into the Queue.
       //Note: We will inserton
       public void insert(Object item) 
       {
          Link add = new Link();
          add.data = item;
          add.next = null;
          
          if(_head == null)
          {
             _head = add;
          }
          else
          {
             for(Link curr = _head; curr.next != null; curr = curr.next)
             {
                curr.next = add;
             }
          }
       }
       
       //Delete Method: A method to delete an Object from the Queue
       public Object delete() 
       {
            if ( _head == null ) return null; 
               
            Link prev = null;    
            Link curr = _head;     
            while (curr.next != null )    
            {      
                prev = curr;      
                curr = curr.next; 
            }     
            if ( prev != null ) 
            {
                prev.next = null; 
            }   
            else
            {       
                _head = null;   
            } 
            return curr.data;
        }
        
        // IsEmpty Method: A method to test for an empty Queue
        public boolean isEmpty() 
        {
          // queue is empty if front is null
          return (_head == null);
        }
        
        //toString Method:
        public String toString()
        {
          String s = "";
          for (Link curr = _head; curr != null; curr = curr.next)
          {
              s = s + " " + curr.data;
          }
          return s;
        }
    }
    
    //Link Class
    class Link 
    {
       public Object data;
       public Link next;
    }
  • Have you tried using the debugger and/or logging how your code is behaving when you add an item to the queue? It looks to me like your `insert` method is essentially re-writing the queue each time you add something so you end up with just the `_head` entry. – stridecolossus Dec 03 '20 at 13:20
  • 1
    `curr.next = add;` belongs **after** the loop, not inside it. – Kevin Anderson Dec 03 '20 at 13:21
  • @KevinAnderson If curr.next = add is outside the loop would it not work? – rockyDodgers Dec 03 '20 at 13:43
  • It wouldn't, **unless** you declare `curr` before the loop and write the loop as `for (curr = _head;...`. The idea is to let the loop move `curr` all the way to the last node, and then have `curr` still available afterward so you can put the new node at the end with `next = add;`, – Kevin Anderson Dec 03 '20 at 13:50

3 Answers3

1

A much simpler approach is to introduce a tail as well as a head making your queue double-ended, no need to iterate through the entire queue every time you add an item.

class Queue {
       private Link head;
       private Link tail;
       
       public void insert(Object item) {
          Link add = new Link();
          add.data = item;
          add.next = null;
          
          if(head == null)
          {
             head = add;
             tail = add;
          }
          else {
             tail.next = add;
             tail = add;
          }
       }
}

The method should really be called add since it is appending to the end not inserting.

stridecolossus
  • 1,449
  • 10
  • 24
0

The logic in your insert() method needs attention. I have modified the else part of the method as follows and it worked:

          else
          {
             for(Link curr = _head; ; curr = curr.next)
             {
                if(curr.next == null){
                    curr.next = add;
                    break;
                }
             }
          }

Here is the full working class:

 public class QuestionFive
    {
       public static void main(String[] args)
       {
          // creating a queue
          Queue q = new Queue();
    
          // adding numbers 1,2,3 and 4
          q.insert(1);
          q.insert(2);
          q.insert(3);
          q.insert(4);

          System.out.println(q);
       }
    }
    class Queue 
    {
       //Private Data Member
       private Link _head;
       
       //Constructor: A constructor to create an empty Queue,
       public Queue() 
       {
          _head = null;
       }
       
       //Insert Method: A method to insert a given Object into the Queue.
       //Note: We will inserton
       public void insert(Object item) 
       {
          Link add = new Link();
          add.data = item;
          add.next = null;
          
          if(_head == null)
          {
             _head = add;
          }
          else
          {
             for(Link curr = _head; ; curr = curr.next)
             {
                if(curr.next == null){
                    curr.next = add;
                    break;
                }
             }
          }
       }
       
       //Delete Method: A method to delete an Object from the Queue
       public Object delete() 
       {
            if ( _head == null ) return null; 
               
            Link prev = null;    
            Link curr = _head;     
            while (curr.next != null )    
            {      
                prev = curr;      
                curr = curr.next; 
            }     
            if ( prev != null ) 
            {
                prev.next = null; 
            }   
            else
            {       
                _head = null;   
            } 
            return curr.data;
        }
        
        // IsEmpty Method: A method to test for an empty Queue
        public boolean isEmpty() 
        {
          // queue is empty if front is null
          return (_head == null);
        }
        
        //toString Method:
        public String toString()
        {
          String s = "";
          for (Link curr = _head; curr != null; curr = curr.next)
          {
              s = s + " " + curr.data;
          }
          return s;
        }
    }
    
    //Link Class
    class Link 
    {
       public Object data;
       public Link next;
    }
Piaget Hadzizi
  • 702
  • 8
  • 15
0

Fixed in your Code(instead toString method printList):

 
public class QuestionFive
    {
       public static void main(String[] args)
       {
          // creating a queue
          Queue q = new Queue();
    
          // adding numbers 1,2,3 and 4
          q.insert(1);
          q.insert(2);
          q.insert(3);
          q.insert(4);
          q.printList();
          System.out.println(q);
       }
    }
    class Queue 
    {
       //Private Data Member
       private Link _head;
       
       //Constructor: A constructor to create an empty Queue,
       public Queue() 
       {
          _head = null;
       }
       
       //Insert Method: A method to insert a given Object into the Queue.
       //Note: We will inserton
       public void insert(Object item) 
       {
          Link add = new Link();
          add.data = item;
          add.next = _head;
          _head = add;
          
       }
       
       //Prints list data
       public void printList() {
           Link currentLink = _head;
           System.out.print("List: ");
           while(currentLink != null) {
               currentLink.printLink();
               currentLink = currentLink.next;
           }
           System.out.println("");
       }
       
       //Delete Method: A method to delete an Object from the Queue
       public Object delete() 
       {
            if ( _head == null ) return null; 
               
            Link prev = null;    
            Link curr = _head;     
            while (curr.next != null )    
            {      
                prev = curr;      
                curr = curr.next; 
            }     
            if ( prev != null ) 
            {
                prev.next = null; 
            }   
            else
            {       
                _head = null;   
            } 
            return curr.data;
        }
        
        // IsEmpty Method: A method to test for an empty Queue
        public boolean isEmpty() 
        {
          // queue is empty if front is null
          return (_head == null);
        }
        
    }
    
    //Link Class
    class Link 
    {
       public Object data;
       public Link next;
       //Print Link data
       public void printLink() {
           System.out.print("{" + data + "}");
       }
    }