Solved

Looking for help in creating a class to handle a Queue

Posted on 2007-11-15
2
162 Views
Last Modified: 2013-11-23
Hi Fellow EE'rs

Im looking for help to get the following code working to handle a queue and be able to print the queue contents, as I understand would the best way to do this to have a 2nd temp view and pass the values out display the value and pass into the 2nd queue?

Anyway heres the code..

public interface Queue  
 
{   public boolean queueEmpty();  
     public boolean queueFull();  
     public void queueAdd(Object x);  
     public Object queueTake();  
     public Object queueShow();  
}  

-----------------------------------------------------

public class ContigQ implements Queue  
 
{  public static final int SIZE = 1000;  
private int max;  
private Object data[];  
private int head;  
private int tail;  
}

-----------------------------------------------------

public class LinkQueue implements Queue  
{  private Node head;  
    private Node tail;  
 
public LinkQueue()  
{  heal = null;  
    tail = head;  
}  
 
public boolean queueEmpty()  
{  return (head == null)  
}  
 
public boolean queueFull()  
{  return false;
}  

public void queueAdd(x)  
{   if (this.queueFull()) ;  
else  
{  Node temp = new Node(x, null);  
    if (head == null)  
            head = temp;  
    else tail.setNext(temp);  
    tail = temp;  
    }  
}  
 
public Object queueShow()  
{  if (this.queueEmpty()) ;  
 else return head.getContents();  
}  
 
public Object queueTake()  
{  if (this.queueEmpty()) ... ;  
else  
{  Node temp = head;  
    head = head.getNext();  
         if (head == null) tail = null;  
    return temp.getContents();  
    }  
}  
}

Many Thanks
Steve
0
Comment
Question by:Stephen Manderson
2 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 500 total points
ID: 20294676
public interface Queue  
 
{   public boolean queueEmpty();  
     public boolean queueFull();  
     public void queueAdd(Object x);  
     public Object queueTake();  
     public Object queueShow();  
}

==========================================

public abstract class ContigQ implements Queue // class must be abstract
 
{  public static final int SIZE = 1000;  
private int max;  
private Object data[];  
private int head;  
private int tail;  
}

==========================================

public class LinkQueue implements Queue  
{
      // I suppose you already have this class...
      private static class Node {
            private Object o;
            private Node next;

            Node(Object o, Node next) {
                  this.o = o;
                  this.next = next;
            }

            void setNext(Node next) { this.next = next; }

            Node getNext() { return next; }

            Object getContents() {
                  return o;
            }
      }

      private Node head;  
    private Node tail;  
 
public LinkQueue()  
{  head = null;  // head, you wrote heal...
    tail = head;  
}  
 
public boolean queueEmpty()  
{  return (head == null); // corrected...
}  
 
public boolean queueFull()  
{  return false;
}  

public void queueAdd(Object x) // corrected...  
{   if (queueFull()) {}
      // you don't need to use "this" key word
      // also since you are sure that queue can't be full
      // you don't need to check this.
else  
{  Node temp = new Node(x, null);  
    if (head == null)  
            head = temp;  
    else tail.setNext(temp);  
    tail = temp;  
    }  
}  
 
public Object queueShow() {
      if (queueEmpty()) {
            return null;
      }
      else return head.getContents();  
}  
 
public Object queueTake()  
{  if (queueEmpty()) {
      return null;
      // or you can throw an Exception if you want.
   }
else  
{  Node temp = head;  
    head = head.getNext();  
         if (head == null) tail = null;  
    return temp.getContents();  
    }  
}

      // added method - String representation...
      public String toString() {
            StringBuffer s = new StringBuffer();
            Node n = head;
            while (n != null) {
                  s.append(n.getContents());
                  if (n.getNext() != null)
                        s.append(',');
                  n = n.getNext();
            }
            return s.toString();
      }
}

=========================================

public class Main {
      public static void main(String[] args) {
            LinkQueue queue = new LinkQueue();

            queue.queueAdd(new Integer(1));
            queue.queueAdd(new Integer(2));
            queue.queueAdd(new Integer(3));
            queue.queueAdd(new Integer(4));
            queue.queueAdd(new Integer(5));
            
            queue.queueTake();
            queue.queueTake();

            System.out.println(queue);
      
            // 3,4,5
      }
}
0
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 20294717
Many Thanks, trying to do this in notepad... knightmare :P

Thanks Again
Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now