As a portion of a first year assignment, I need to create a class which uses a provided linked list implementation to implement a provided stack interface which is then tested by another provided class. I was able to easily complete the assignment using my own LL; however, I was told that I needed to use the one provided.
Exception in thread "main" java.lang.NullPointerException
at StackLL.<init>(StackLL.java:21)
at StackTester.main(StackTester.java:91)
Now I get null pointer exceptions whenever I try and run it. I thought this was being caused by the tester trying to grab the size of the list before the LL is initialized, but that doesn't seem to be the case and I am stumped.
Any tips on what I can do to fix the bug so that I can hand in the rest of the assignment? Thanks :)
The provided linked list implementation
LinkedList.java
/**
* LinkedList - a simple linked list of ints
*/
public class LinkedList
{
Node head;
int count;
public LinkedList ()
{
head = null;
count = 0;
}
/**
* Adds the given item to the start of the list
*/
public void addToStart (int item)
{
Node newNode = new Node();
newNode.value = item;
if (head != null)
newNode.next = head;
head = newNode;
count++;
}
/**
* Adds the given item to the end of the list
*/
public void addToEnd (int item)
{
if (size() == 0)
{
addToStart (item);
}
else
{
Node n = head;
while (n.next != null)
n = n.next;
n.next = new Node(item);
count++;
}
}
/**
* Remove and return the first item in the list
*/
public int removeFromStart ()
{
if (size() == 0)
throw new EmptyListException();
int valtoReturn = head.value;
head = head.next;
count--;
return valtoReturn;
}
/**
* Remove and return the last item in the list
*/
public int removeFromEnd ()
{
if (size() == 0)
throw new EmptyListException();
if (size() == 1)
return removeFromStart();
else
{
Node n = head;
while (n.next.next != null)
n = n.next;
int valtoReturn = n.next.value;
n.next = null;
count--;
return valtoReturn;
}
}
/**
* Return the number of items contained in this list
*/
public int size ()
{
return count;
}
/**
* A basic node class
*/
private class Node
{
int value;
Node next;
Node()
{
}
Node (int value)
{
this.value = value;
}
}
// random testing code for the Linked List
public static void main (String [] args)
{
LinkedList l = new LinkedList();
l.addToStart (5);
int val = l.removeFromStart();
System.out.println (val == 5 ? "passed" : "failed");
System.out.println (l.size() == 0 ? "passed" : "failed");
for (int x = 0; x < 10; x++)
l.addToEnd (x);
System.out.println (l.size() == 10 ? "passed" : "failed");
while (l.size() > 0)
System.out.print (l.removeFromEnd() + " ");
System.out.println ();
}
}
/**
* The exception class when a removal action is performed on
* an empty list.
*/
class EmptyListException extends RuntimeException
{
public EmptyListException ()
{
super();
}
public EmptyListException (String s)
{
super(s);
}
}
My implementation
StackLL.java
/**
* A linked list implementation of the Stack ADT.
*
*/
public class StackLL implements Stack
{
// The linked list that will contain the values in the stack
private LinkedList values;
public int size()
{
return values.size();
}
public boolean isEmpty()
{
if (values.size() <= 0) {
return true;
}
return false;
}
public void push(int element)
{
values.addToStart(element);
}
public int pop() throws StackEmptyException
{
if (values.size() == 0) {
throw new StackEmptyException();
}
else {
return values.removeFromStart();
}
}
public int peek() throws StackEmptyException
{
if (values.size() == 0) {
throw new StackEmptyException();
}
else { //This is a pretty silly way to do this, but I can't think of any other way without making my own linked list method.
int elementVal = values.removeFromStart();
values.addToStart(elementVal);
return elementVal;
}
}
}
The Provided Interface
Stack.java
/**
* Stack.java
*
* A specification of the Stack ADT
*
*/
public interface Stack
{
int size();
boolean isEmpty();
void push (int element);
int pop() throws StackEmptyException;
int peek() throws StackEmptyException;
}
class StackEmptyException extends Exception
{
public StackEmptyException ()
{
super();
}
public StackEmptyException (String s)
{
super(s);
}
}
The Tester
StackTester.java
/**
* StackTester.java
*
* Some test cases for a stack.
*/
public class StackTester
{
public static void testOne (Stack s)
{
try
{
if (s.size() != 0 || !s.isEmpty())
System.out.println("1: Failed size or isEmpty.");
s.push(1);
s.push(2);
if (s.size() != 2 || s.isEmpty())
System.out.println("2: Failed size or isEmpty.");
if (!(s.pop() == 2))
System.out.println("3: Failed pop");
if (!(s.peek() == 1))
System.out.println("4: Failed peek");
if (!(s.pop() == 1))
System.out.println("5: Failed pop");
if (s.size() != 0 || !s.isEmpty() )
System.out.println("6: Failed size or isEmpty.");
}
catch (StackEmptyException e)
{
System.out.println(e);
}
}
public static void testTwo (Stack s)
{
try
{
for (int i = 0; i < 100; i++)
{
s.push(i);
}
if (s.size() != 100)
System.out.println("7: Failed size.");
for (int i = 99; i >= 0; i--)
{
if (!(s.pop() == i))
{
System.out.println("Failed pop for: " + i);
break;
}
}
}
catch (StackEmptyException e)
{
System.out.println("Failed testTwo.");
System.out.println(e);
}
}
public static void testThree (Stack s)
{
try {
while (!s.isEmpty())
s.pop();
}
catch (StackEmptyException e) {
System.out.println ("Failed empty stack test (popped on a non empty stack threw exception)");
}
try
{
s.pop();
System.out.println("Failed empty stack test.");
}
catch (StackEmptyException e)
{
/* If we get here, we
* passed the previous test.
*/
}
}
public static void main (String args[])
{
Stack s1 = new StackLL();
Stack s2 = new StackLL();
Stack s3 = new StackLL();
testOne(s1);
testTwo(s2);
testThree(s3);
}
}
You have private LinkedList values; in StackLL.
That says "this class has a field called values of type LinkedList". It does not assign an object to values, so when you try to access it, a NullPointerException occurs.
You should be able to fix it by assigning a value to values, i.e.:
private LinkedList values = new LinkedList();
(I don't know if you've learned about generics yet, but if you have, remember to add the type, e.g. LinkedList<Person>.)
Look at the line of code given in the error message (I'm assuming its the return statement in your size() function) and think about the meaning of NullPointerException -- the variable which is null is not yet initialized. Then ask yourself, do I expect this variable to be initialized here? If yes, then ask why isn't it and where should it be initialized? If no, then you have a logic error at the given location.
You aren't ever instantiating a LinkedList object in StackLL. So the first time you try to access it, it blows a NPE.
Related
I'm sorry if this is a very long code, but the only problem is the remove() method in the LinkedList Class, and I've been struggling on this code for hours and couldn't seem to find a solution. Whenever I input ADD 456 for the main method, instead of printing
0+6+5+4
RESULT 15
I keep on getting
0+6+6+4
RESULT 16
That means either the remove() or insert() method went wrong, but when I checked the input of the insert() method, 5 was properly inserted when it had to. So I was wondering which part of the remove() method went wrong, and how I could solve it. Thanks.
These are the interfaces.
Interface Stack.
package ds.stack;
public interface Stack<E> {
/*
* Removes all of the elements in this stack.
*/
public void clear();
/*
* Pushes an item onto the top of this stack.
*
* #param item
* the item to be pushed onto this stack
*/
public void push(E item);
/**
* Removes the item at the top of this stack and returns that item as the
* value of this method.
*
* #return the item at the top of this stack, or null if this stack is empty
*/
public E pop();
/**
* Returns the number of elements in this stack.
*
* #return the number of elements in this stack
*/
public int length();
/**
* Returns true if this stack contains no elements.
*
* #return true if this stack contains no elements
*/
public boolean isEmpty();
}
Interface List.
package ds.list;
public interface List<E> {
/**
* Removes all of the elements from this list.
*/
public void clear();
/**
* Inserts the specified element at the specified position in this list.
*
* #param pos
* index at which the specified element is to be inserted
* #param item
* element to be inserted
*/
public void insert(int pos, E item);
/**
* Removes the element at the specified position in this list. Shifts any
* subsequent elements to the left (subtracts one from their indices).
* Returns the element that was removed from the list.
*
* #param pos
* the index of the element to be removed
* #return the element previously at the specified position
*/
public E remove(int pos);
/**
* Returns the number of elements in this list.
*
* #return the number of elements in this list.
*/
public int length();
/**
* Returns the element at the specified position in this list.
*
* #param pos
* index of the element to return
* #return the element at the specified position in this list
*/
public E getValue(int pos);
}
Here is my LinkedList Class
package ds.list;
public class LinkedList<E> implements List<E> {
private E element;
private LinkedList<E> next;
private LinkedList<E> head;
private LinkedList<E> tail;
private LinkedList<E> curr;
public int cnt=0; //length of the list
/*
* constructors below
*/
public LinkedList() { //The very initial constructor
curr = tail = head = this;
}
public LinkedList(LinkedList<E> nextval) { //when you start making more bundles
next = nextval;
}
public void setNext(LinkedList<E> nextval) {
next = nextval;
}
public void goNext() {
curr = next;
} // curr becomes the next bundle
public void setValue(E item) {
element = item;
}
#Override
public void clear() {
tail = head = new LinkedList<E>();
next = null;
cnt = 0;
}
#Override
public void insert(int pos, E item) {
if(pos<0||pos>cnt+1) {
return;
}
if(pos==0) {
curr = head;
head = new LinkedList<E>(curr);
curr = head;
curr.setValue(item);
}
curr = head;
for(int i=0;i<pos-1;i++) {
goNext();
} //curr points right before the index of pos
LinkedList<E> temp = curr.next;
curr.setNext(new LinkedList<E>(temp));
curr.goNext();
curr.setValue(item);
cnt++;
}
#Override
public E remove(int pos) {
if(pos<0||pos>cnt)
return null;
curr = head;
if(cnt==1) {
E it = element;
curr = head = tail = null;
cnt--;
return it;
}
for(int i=0;i<pos-1;i++) {
goNext();
}
E it = next.element;
curr.setNext(next.next);
cnt--;
return it;
}
#Override
public int length() {
return cnt;
}
#Override
public E getValue(int pos) {
if(pos<0||pos>cnt)
return null;
curr = head;
for(int i=0;i<pos-1;i++) {
goNext();
}
return next.element;
}
}
And this is my LinkedStack Class, utilizing the LinkedList Class
package ds.stack;
import ds.list.LinkedList;
public class LinkedStack<E> implements Stack<E> {
private LinkedList<E> stack = new LinkedList<E>();
#Override
public void clear() {
stack.clear();
}
#Override
public void push(E item) {
if(stack.cnt == 0) {
stack.setValue(item);
stack.cnt++;
return;
}
stack.insert(stack.length(),item);
}
#Override
public E pop() {
if(stack.length()==0) {
return null;
}
else {
return stack.remove(stack.length()-1);
}
}
#Override
public int length() {
return stack.length();
}
#Override
public boolean isEmpty() {
if(stack.length()==0)
return true;
return false;
}
}
Then this is my BabyCalculator Class that uses the LinkedStack Class
package ds.test;
import ds.stack.LinkedStack;
import ds.stack.Stack;
public class BabyCalculator {
Stack<Character> stack = new LinkedStack<Character>();
private int value=0;
public int murmurAdd(String polynomial) {
char[] charPol=polynomial.toCharArray();
int count=0;
for(int i=0;i<polynomial.length();i++) {
if(!(Character.isDigit(charPol[i])))
count++;
} // This counts the total number of ( and )s.
int numOf=count/2;
if (numOf==0) {
for(int i=0;i<polynomial.length();i++) {
stack.push(charPol[i]);
}
}
else {
for(int i=0;i<numOf;i++) {
int num1=0, num2 = 0; //will become the index of last ( and first )
for(int j=0;j<polynomial.length();j++) {
if(charPol[j]=='(')
num1 = j;
if(charPol[j]==')') {
num2 = j;
break;
}
}
for(int index=num1+1;index<num2;index++) {
stack.push(charPol[index]);
}
StringBuilder polytemp = new StringBuilder(polynomial);
polynomial=polytemp.replace(num1, num2+1, "").toString();
}
if(polynomial.length()>0) {
charPol = polynomial.toCharArray();
for(int i=0;i<polynomial.length();i++) {
stack.push(charPol[i]);
}
}
}
System.out.print(value);
while(!(stack.isEmpty())) {
Character a = stack.pop();
System.out.println(" a is "+a);
value += Character.getNumericValue(a);
System.out.print("+"+a);
}
System.out.println();
return value;
}
public int getValue() {
// TODO Implement this method
return value;
}
public void setValue(int newValue) {
// TODO Implement this method
value = newValue;
}
}
Finally, the main() method that uses BabyCalculator.
package ds.test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BabyCalculator babyCalculator = new BabyCalculator();
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String command = sc.next();
if ("ADD".equals(command)) {
String equation = sc.next();
babyCalculator.murmurAdd(equation);
System.out.println("RESULT "+babyCalculator.getValue());
// TODO
} else if ("SHOW".equals(command)) {
System.out.println("VALUE "+babyCalculator.getValue());
// TODO
} else if ("CLEAR".equals(command)) {
babyCalculator.setValue(0);
System.out.println("VALUE CLEARED");
// TODO
} else if ("SET".equals(command)) {
int newValue = sc.nextInt();
babyCalculator.setValue(newValue);
System.out.println("VALUE SET TO "+babyCalculator.getValue());
// TODO
} else if ("EXIT".equals(command)) {
System.out.println("FINAL VALUE "+ babyCalculator.getValue());
return;
// TODO
}
}
sc.close();
}
}
EDIT : When I tried ADD (2345), the result was
0+5+5+5+2
RESULT 17
Which means 5 kept popping out only until it was time for 2 to pop out. Why does this keep happening?I'm assuming it's a deep pointing issue in LinkedList class.
Well, I can say with certainty that your LinkedList is not correctly implemented. You need to do unit testing of your foundational classes before you build on top of them. A basic test involving nothing more than inserting a few elements into position 0 and then trying to get the value of items in positions 0, 1, and 2 fails.
This was a basic test I wrote and it fails with NullPointerException.
LinkedList<String> list = new LinkedList<>();
list.insert(0, "A");
list.insert(0, "B");
list.insert(0, "C");
System.out.println(list.getValue(0));
System.out.println(list.getValue(1));
System.out.println(list.getValue(2));
Add more logging throughout your code, use a debugger, implement toString methods on your classes to help you find the problems.
I can tell you that your LinkedList method getValue does not work as intended. To get my test above to work I had to change from this:
for(int i=0;i<pos-1;i++) {
goNext();
}
return next.element;
to this:
for (int i = 0; i < pos; i++) {
goNext();
}
return curr.element;
The reason is because "next" refers to the next element of whatever LinkedList you called getValue on, not the next element after the current one.
I can also tell you that you have a similar bug in your goNext method of LinkedList:
public void goNext() {
curr = next;
}
should be:
public void goNext() {
curr = curr.next;
}
There are almost certainly more issues with this class, so I highly recommend you thoroughly test and debug it as this will probably solve many of your problems.
I see more issues than one. The insert is wrong, the remove is wrong (you can validate) by calling ADD twice in the same run.
One such issue is in insert I changed your code as below:
//LinkedList<E> temp = curr.next;
//curr.setNext(new LinkedList<E>(temp));
LinkedList<E> temp = new LinkedList<E>(curr.next);
temp.setValue(curr.element);
And removed the loop
for(int i=0;i<pos-1;i++) {
goNext();
}
in remove method Atleast add test works. But you have more problems at hand. I didn't test much of other usecases.
I've attempted to implement a priority queue using an Array of Objects "Queue Items" which have some data (a string), and an integer which is the priority. I am trying to make those items comparable so that when I add a new object to the queue I can iterate through the items and add the new item in the correct location and move all items that are now behind it backwards, however when I add a new item to the queue I get a null pointer exception. I'll include all my code, but the toString method was just copied in from a queue so it won't work as expected.
class QueueItem implements Comparable<QueueItem> {
String data;
int pri;
public QueueItem(String data, int pri) {
this.data = data;
this.pri = pri;
}
#Override
public int compareTo(QueueItem item) {
return this.data.compareTo(item.data);
}
}
public class PriorityQueue implements Queue<String> {
private QueueItem[] arr;
private int frontPos, backPos;
public PriorityQueue() {
arr = new QueueItem[20];
backPos = -1;
frontPos = 0;
}
public boolean isEmpty() {
return frontPos == (backPos + 1) % arr.length;
}
public String front() {
if (frontPos == (backPos + 1) % arr.length)
throw new QueueException("Empty Queue - front");
return arr[frontPos].data;
}
public int frontPri() {
if (frontPos == (backPos + 1) % arr.length)
throw new QueueException("Empty Queue - frontPri");
return arr[frontPos].pri;
}
public void addToPQ(String str, int x) {
if (arr.length==0) {
arr[frontPos] = new QueueItem(str, x);
frontPos++;
return;
}
else {
for (int i = 0; i < arr.length; i++) {
arr[i].compareTo(new QueueItem(str, x));
}
}
}
public void deleteFront() {
if (frontPos==(backPos+1)%arr.length) {
throw new QueueException("Empty Queue - deleteFront");
}
frontPos = (frontPos+1)%arr.length;
}
public String toString() {
if (frontPos == (backPos + 1) % arr.length) {
return "<>";
}
StringBuffer sb = new StringBuffer();
sb.append('<');
int pos = frontPos;
while (pos != backPos) {
sb.append(arr[pos]);
sb.append(',');
pos = (pos + 1) % arr.length;
}
sb.append(arr[backPos]);
sb.append('>');
return (sb.toString());
}
}
public interface Queue<String> {
public void addToPQ(String str, int x);
public void deleteFront();
public String front();
public boolean isEmpty();
public int frontPri();
}
class QueueException extends RuntimeException {
QueueException(String s) {
super("Tried to apply " + s + " to empty queue");
}
}
public class pqTest {
public static void main(String[] args) {
PriorityQueue pQ = new PriorityQueue();
if (pQ.isEmpty()) {
System.out.println("Queue is Empty - isEmpty");
}
pQ.addToPQ("Dog", 4);
pQ.addToPQ("Cat", 20);
pQ.deleteFront();
pQ.addToPQ("Fish", 2);
}
}
The problem is that arr is size 20 so the first element won't even be added through the if statement in your addToPQ method because arr.length != 0. So it will then go to your else statement, which iterates through every single element in arr. But arr has 20 null elements since each spot within the array of QueueItems has not been initialized. So you should change your condition in the if statement to frontPos == 0 and change the terminating condition in your loop to i < frontPos so that the method won't iterate through null elements within arr
public void addToPQ(String str, int x) {
if (frontPos==0) {
arr[frontPos] = new QueueItem(str, x);
frontPos++;
return;
}
else {
QueueItem item = new QueueItem(str, x);
for (int i = 0; i < frontPos; i++) {
arr[i].compareTo(item);
}
}
}
You get NullPointerException, because when you are adding second item, you go to else statment where you iterate over array with one non-null element and 19 nulls. So you need to change your code to check if array element at i is null and if it is, assign new element to it.
I have code that I have been working on going on 10 hours now, and for the life of me, I am unable to get the output( ) of my Set.java to work. Unfortunately I am not allowed to just import the Iterator or HashTable classes from java library. Any ideas or advice would really help.
public class SLL {
public class Node {
private int data;
private Node next;
public Node() {
data = 0;
next = null;
}
public Node(int newData, Node linkValue) {
data = newData;
next = linkValue;
}
public int getData() {
return data;
}
public Node getLink() {
return next;
}
} // End of Node inner class
private Node head;
public SLL() {
head = null;
}
public void addToStart(int itemData) {
head = new Node(itemData, head);
}
public boolean contains(int item) {
return (find(item) != null);
}
/**
* Finds the first node containing the target item, and returns a reference
* to that node. If target is not in the list, null is returned.
*/
public Node find(int target) {
Node position = head;
int itemAtPosition;
while (position != null) {
itemAtPosition = position.data;
if (itemAtPosition == target) {
return position;
}
position = position.next;
}
return null; // target was not found
}
public void outputList() {
Node position = head;
while (position != null) {
System.out.print(position.data + " ");
position = position.next;
}
System.out.println();
}
}
This is the class that I have been working on:
public class Set {
private SLL[] hashArray; // DO NOT MODIFY THIS LINE
private int size = 10; // DO NOT MODIFY THIS LINE
// DO NOT MODIFY THIS METHOD
public Set() {
hashArray = new SLL[size];
}
// DO NOT MODIFY THIS METHOD
private int computeHash(int s) {
return s % size;
}
// COMPLETE BELOW
public void add(int x)
{
int hash = computeHash(x); // Get hash value
SLL list = hashArray[hash];
if(hashArray[hash] == null)
hashArray[hash] = new SLL();
else if(!list.contains(x));
{
// Only add the target if it's not already
// on the list.
hashArray[hash].addToStart(x);
}
}
public void output()
{
SLL tmp = new SLL();
SLL.Node temp = tmp.head;
for(int i = 0; i < size; i++)
{
if(temp == null)
//I think a new instance needs to be created
while(temp.getLink() != null)
{
System.out.println(i + temp.getData() + toString() + " ");
}
}
}
}
And this is the tester it should work with:
public class Tester{
// Have this method to display your name, instead.
static void displayName(){
System.out.println("Program written by Tony.\n");
}
// DO NOT MODIFY THE MAIN METHOD
public static void main(String[] args){
displayName();
Set set1 = new Set();
Set set2 = new Set();
set1.add(3);
set1.add(3);
set1.add(13);
set1.add(23);
set1.add(4);
set1.add(5);
set2.add(15);
set2.add(6);
set2.add(6);
System.out.println("Contents of set 'set1': ");
set1.output();
System.out.println("Contents of set 'set2': ");
set2.output();
System.out.println();
}
}
You need to iterate through the entries in your hash array, and for each non-null entry, iterate through the linked list:
public void output() {
for (int i = 0; i < hashArray.length; i++) {
if (hahArray[i] != null) {
hashArray[i].outputList();
}
}
}
In your for loop as below:
if(temp == null)
//I think a new instance needs to be created
while(temp.getLink() != null)
{
System.out.println(i + temp.getData() + toString() + " ");
}
You should be checking temp != null and you are not moving your node from one to other to traverse over each elements. So you should change it to:
while(temp != null) {
System.out.println(temp.getData() + " ");
temp = temp.getLink();
}
So changing the output( ) a bit to
public void output()
{
SLL tmp = new SLL();
SLL.Node temp = tmp.head;
for (SLL s : hashArray) {
System.out.println(toString() + " " + s);}
}
It still only prints "Programmed by Tony"
and "Contents of set 1:" "Contents of set 2: " without the actual set data?
Hot Potato Game: In this program you are expected to implement a
general simulation of the Hot Potato game. In this game children line
up in a circle and pass an item from neighbor to neighbor as fast as
they can. At a certain point in the game, the action is stopped and
the child who has the item (the potato) is removed from the circle.
Play continues until only one child is left. In your implementation
user should input a list of names and a constant num. Your program
must return the name of the last person remaining after repetitive
counting by num.
I needed to do that but i couldn't find out how to stop that while loop in the enqueuer() method of hotpotato class. If i have some other mistakes can you please tell me?
hotpotato class:
import java.util.*;
public class hotpotato
{
private static Scanner input1 = new Scanner(System.in);
private static NodeQueue<String> potato = new NodeQueue<String>();
private static Scanner input = new Scanner(System.in);
static int num;
public static void main(String[] args)
{
System.out.println("Enter names of the children.");
enqueuer(input1.next());
System.out.println("Enter the num");
num = input.nextInt();
potatothrower();
}
public static void enqueuer(String p)
{
String keyboard = input1.next();
while(!keyboard.equals("stop"))
{
potato.enqueue(p);
}
}
public static void potatothrower()
{
for(int i = 0; i< num; i++)
{
if(!potato.isEmpty()){
String tmp = potato.front();
potato.dequeue();
potato.enqueue(tmp);
}
else{
System.out.println("Queue is empty");
}
}
potato.dequeue();
}
}
Node Class:
public class Node<E> {
// Instance variables:
private E element;
private Node<E> next;
/** Creates a node with null references to its element and next node. */
public Node() {
this(null, null);
}
/** Creates a node with the given element and next node. */
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// Accessor methods:
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
// Modifier methods:
public void setElement(E newElem) {
element = newElem;
}
public void setNext(Node<E> newNext) {
next = newNext;
}
}
NodeQueue Class:
public class NodeQueue<E> implements Queue<E> {
protected Node<E> head;
protected Node<E> tail;
protected int size; // number of elements in the queue
public NodeQueue() { // constructs an empty stack
head = null;
tail = null;
size = 0;
}
public void enqueue(E elem) {
Node<E> node = new Node<E>();
node.setElement(elem);
node.setNext(null); // node will be new tail node
if (size == 0)
head = node; // special case of a previously empty queue
else
tail.setNext(node); // add node at the tail of the list
tail = node; // update the reference to the tail node
size++;
}
public E dequeue() {
if (size == 0)
System.out.println("Queue is empty.");
E tmp = head.getElement();
head = head.getNext();
size--;
if (size == 0)
tail = null; // the queue is now empty
return tmp;
}
public int size() { return size; }
public boolean isEmpty() {
return size == 0;
}
public E front() {
if (isEmpty()) System.out.println("Queue is empty.");
return head.getElement();
}
public String toString() {
Node<E> temp = head;
String s;
s = "[";
for (int i = 1; i <= size(); i++){
if(i==1)
s += temp.getElement();
else
s += ", " + temp.getElement();
temp = temp.getNext();
}
return s + "]";
}
}
Queue interface:
public interface Queue<E> {
/**
* Returns the number of elements in the queue.
* #return number of elements in the queue.
*/
public int size();
/**
* Returns whether the queue is empty.
* #return true if the queue is empty, false otherwise.
*/
public boolean isEmpty();
/**
* Inspects the element at the front of the queue.
* #return element at the front of the queue.
* #exception EmptyQueueException if the queue is empty.
*/
public E front();
/**
* Inserts an element at the rear of the queue.
* #param element new element to be inserted.
*/
public void enqueue (E element);
/**
* Removes the element at the front of the queue.
* #return element removed.
* #exception EmptyQueueException if the queue is empty.
*/
public E dequeue();
}
call:
System.out.println("Enter names of the children.");
enqueuer();
You're doing the read data only once and then enters an infinite WHILE loop because you have no way to write "stop". Therefore the function would be:
public static void enqueuer()
{
String p;
do {
p = input1.next();
if (!p.equals("stop"))
potato.enqueue(p);
} while(!p.equals("stop"));
}
The problem is really easy, just look better at your code:
public static void enqueuer(String p)
{
String keyboard = input1.next();
while(!keyboard.equals("stop"))
{
potato.enqueue(p);
}
}
You get the new input before the while. This means that keyboard will be the first argument, say Alice.
So, while alice != stop, do potato.enqueue.(p).
But since you get no new input inside the while, keyboard will ever be != then stop!
Bonus:
There is a bug i think:
public static void enqueuer(String p)
{
String keyboard = input1.next();
while(!keyboard.equals("stop"))
{
potato.enqueue(p); //you dont insert the keyboard value, but you insert every time p!
}
}
I'm trying to make a Queue in java.
The problem is that I don't know how to remove(?) a value from the array, the index value which I dequeue.
This is my code.
front() method is the dequeue part. and I used iter_ to set the current index position.
but as you can see, it dequeue's the corret value though the value still remains inside the array:(
public class IntQueue {
private int[] items_;
private int top_;
private int capacity_;
private int iter_;
public IntQueue(int capacity)
{
if(capacity <=0) capacity = 10;
capacity_ = capacity;
top_=0;
count_ = 0;
iter_=0;
items_= new int[capacity_];
}
public void push_back(int value)
{
if(top_>= capacity_)
overflow();
items_[top_++]=value;
count_++;
}
public int front()
{
if(top_<=0)
return 0;
int temp=0;
temp=items_[iter_];
count_--;
iter_++;
return temp;
}
public IntQueue clone()
{
IntQueue result = new IntQueue(capacity_);
for(int i=0 ; i<top_; ++i)
{
result.push_back(items_[i]);
}
/*for(int i=0 ; i<top_ ; ++i)
{
result.items_[i] = items_[i];
}*/
return result;
}
public void log()
{
for(int i=0 ; i <top_; ++i)
{
System.out.print(items_[i]);
if(i<top_ -1)
System.out.print(", ");
}
System.out.println();
}
}
private void overflow()
{
int[] newItem = new int[capacity_*2];
for(int i=0 ; i <top_; ++i)
newItem[i] = items_[i];
items_=newItem;
capacity_ *=2;
}
public static void main(String args[])
{
IntQueue queue = new IntQueue(2);
System.out.println("queue push 3: "); queue.push_back(3);
System.out.println("queue push 2: "); queue.push_back(2);
System.out.println("queue push 1: "); queue.push_back(1);
System.out.print("queue log: "); queue.log();
System.out.println("front " + queue.front());
System.out.println("front " + queue.front());
System.out.print("queue log: "); queue.log();
System.out.println("queue push 12: "); queue.push_back(12);
System.out.println("queue push 11: "); queue.push_back(11);
System.out.println("queue push 21: "); queue.push_back(21);
System.out.println("queue push 31: "); queue.push_back(31);
System.out.print("queue log: "); queue.log();
System.out.println("front " + queue.front());
System.out.println("front " + queue.front());
System.out.print("clone queue log: "); queue.clone().log();
}
}
What I don't get about your implementation is the following:
in the front method you're using iter_, but nowhere else. What is it good for?
if you're not using some kind of variable to keep track of what was removed without actually removing it, technically you'd need to shift all the items of the array to the left by one position, such that the first item is gone. This however is an O(N) operation.
It's easier to implement a queue using a linked list instead of an array.
When building a queue using an array, you can make a cyclic "pointer" to the head of the array, that you can use to retrieve the top.
Popping from the array is simply done by increasing this cyclic pointer.
Maintain an int variable: top, and once you need to pop an element do top = (top + 1) % items_.length
Retrieving the head is simple using items_[top].
Make sure you are guarding against popping elements that are not there (popping from an empty array).
You will probably also need to maintan a size variable for the queue's size.
Below I share my proposed solution for a simple thread-safe FIFO queue :D
public class Queue extends Object {
private int numElements;
private Node first;
private Node last;
/**
* Helper linked list class
*/
private static class Node {
private Object item;
private Node next;
}
/**
* Creates a new Queue object.
*/
public Queue() {
numElements = 0;
first = last = null;
}
/**
* Puts an object at the end of the queue.
*
* #param object
*/
public void putObject(Object object) {
synchronized (this) {
Node newNode = new Node();
newNode.item = object;
newNode.next = null;
if ( numElements == 0 ) {
first = last = newNode;
} else {
last.next = newNode;
last = newNode;
}
numElements += 1;
}
}
/**
* Gets an object from the beginning of the queue. The object is removed
* from the queue. If there are no objects in the queue, returns null.
*/
public Object getObject() {
synchronized (this) {
Object item = null;
if ( numElements > 0 ) {
item = first.item;
first = first.next;
numElements -= 1;
if (numElements == 0) {
last = null;
}
}
return item;
}
}
}
You cant remove an element from an array like a List. You need to move the values up and set the last index of the array as null. Take a look at the source of a java class that implement Queue.remove() method. For example following is the code from removeAt(int index) method of ArrayBlockingQueue.
void removeAt(int i) {
final E[] items = this.items;
// if removing front item, just advance
if (i == takeIndex) {
items[takeIndex] = null;
takeIndex = inc(takeIndex);
} else {
// slide over all others up through putIndex.
for (;;) {
int nexti = inc(i);
if (nexti != putIndex) {
items[i] = items[nexti];
i = nexti;
} else {
items[i] = null;
putIndex = i;
break;
}
}
}
--count;
notFull.signal();
}
when you design a queue, you need to decide the priority like how you are going to add and remove element from it.You can go through this link and implement it similar to this.
FIFO
A typical example is :
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<String> qe=new LinkedList<String>();
qe.add("b");
qe.add("a");
qe.add("c");
qe.add("e");
qe.add("d");
Iterator it=qe.iterator();
System.out.println("Initial Size of Queue :"+qe.size());
while(it.hasNext())
{
String iteratorValue=(String)it.next();
System.out.println("Queue Next Value :"+iteratorValue);
}
// get value and does not remove element from queue
System.out.println("Queue peek :"+qe.peek());
// get first value and remove that object from queue
System.out.println("Queue poll :"+qe.poll());
System.out.println("Final Size of Queue :"+qe.size());
}
}
First of all, you can't delete an item from an array, you can just overwrite it. You can use a List instead.
Another option is to use a circular queue, as amit's answer pointed out.
A simple solution using array is:
int queue[SIZE];
int first = 0;
int last = 0;
void enque(int i) {
if(last == SIZE)
throw new RuntimeExeption("Queue is full");
queue[last++] = i;
}
int deque() {
if(first == last)
throw new RuntimeExeption("Queue is empty");
return queue[first++];
}
Check this: it has enqueue and dequeue methods:
import java.io.*;
import java.lang.*;
class clrqueue
{
DataInputStream get=new DataInputStream(System.in);
int a[];
int i,front=0,rear=0,n,item,count=0;
void getdata()
{
try
{
System.out.println("Enter the limit");
n=Integer.parseInt(get.readLine());
a=new int[n];
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void enqueue()
{
try
{
if(count<n)
{
System.out.println("Enter the element to be added:");
item=Integer.parseInt(get.readLine());
a[rear]=item;
rear++;
count++;
}
else
System.out.println("QUEUE IS FULL");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void dequeue()
{
if(count!=0)
{
System.out.println("The item deleted is:"+a[front]);
front++;
count--;
}
else
System.out.println("QUEUE IS EMPTY");
if(rear==n)
rear=0;
}
void display()
{
int m=0;
if(count==0)
System.out.println("QUEUE IS EMPTY");
else
{
for(i=front;m<count;i++,m++)
System.out.println(" "+a[i]);
}
}
}
class Myqueue
{
public static void main(String arg[])
{
DataInputStream get=new DataInputStream(System.in);
int ch;
clrqueue obj=new clrqueue();
obj.getdata();
try
{
do
{
System.out.println(" 1.Enqueue 2.Dequeue 3.Display 4.Exit");
System.out.println("Enter the choice");
ch=Integer.parseInt(get.readLine());
switch (ch)
{
case 1:
obj.enqueue();
break;
case 2:
obj.dequeue();
break;
case 3:
obj.display();
break;
}
}
while(ch!=4);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
This is the code i implemented and is working fine in my system.
import java.util.Arrays;
public class Queue {
public int[] queue = new int[3];
int head = -1, tail =-1;
public void enqueue(int N){
if(tail == (queue.length-1))
queue = Arrays.copyOf(queue,2*queue.length);
if(head == -1){
head++;
tail++;
queue[head] = N;
}
else {
tail++;
queue[tail] = N;
}
}
public int dequeue(){
if(head == -1)
throw new IllegalStateException("Cannot dequeue if queue is empty");
int firstItem = queue[head];
if (head == tail) {
head = -1;
tail = -1;
}
else
head++;
return firstItem;
}
public void display(){
for (int i = head; i<= tail ; i++){
System.out.println("Display: " + queue[i]);
}
}
}
public class Main {
public static void main(String[] args) {
Queue queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.display();
int dequeue = queue.dequeue();
System.out.println(dequeue);
queue.display();
int dequeue1 = queue.dequeue();
System.out.println(dequeue1);
queue.display();
}
}