Solved

Looking for help in creating a class to handle a Queue

Posted on 2007-11-15
2
156 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 86
changePi Challenge 15 77
Securing Jmx Console and web console 2 48
Java Timer (static) 9 19
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 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

10 Experts available now in Live!

Get 1:1 Help Now