Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-29-2008, 12:53 AM
Member
 
Join Date: Jan 2008
Posts: 12
rhm54 is on a distinguished road
Implementing a Stack Using two Queues
Greetings,

I am extremely new to Java and programming in General but I am trying to implement a stack using two queues for a homework assignment. I think I have the right idea but for some reason when the program runs I get:

Exception in thread "main" java.lang.NullPointerException
at com.shadowconcept.mcdougal.StackImplementationUsin gTwoQueues.push(StackImplementationUsingTwoQueues. java:27)
at com.shadowconcept.mcdougal.StackImplementationUsin gTwoQueues.main(StackImplementationUsingTwoQueues. java:57)


Here is the code:

Code:
package com.shadowconcept.mcdougal; import java.util.*; public class StackImplementationUsingTwoQueues { Queue<Integer> q1; Queue<Integer> q2; public int pop() { if (q1 == null) { System.out.println("The stack is empty, nothing to return"); int i = 0; return i; } else { int temp = q1.remove(); if (q2 != null){ q1.add(q2.remove()); } return temp; } } public void push(int data){ if (q1 == null){ q1.add(data); } else { q2.add(q1.remove()); q1.add(data); } } public int top(){ if (q1 == null){ int temp = 0; System.out.println("The Stack is empty returning value 0"); return temp; } return q1.peek(); } public int size(){ if (q1 == null && q2 == null){ int size = 0; return size; } else{ int size = q1.size() + q2.size(); return size; } } public static void main(String[] args) { StackImplementationUsingTwoQueues s1 = new StackImplementationUsingTwoQueues(); s1.push(8); s1.push(10); s1.push(3); s1.pop(); s1.pop(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-29-2008, 01:36 AM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
I suspect that the reason your code is crashing is due to the code

Code:
if (q1 == null){ q1.add(data); }
you test the q1 reference to be null, and having found it is, you then try to use it. I suspect there are two things you need to change, firstly you need to instantiate the Queues,currently you create reference variables ( q1 and q2 ) that are capable of referring to a Queue but you never actually create the Queues. Maybe something like

Code:
Queue<Integer> q1 = new LinkedList<Integer>(); Queue<Integer> q2= new LinkedList<Integer>();
also you are getting confused between

Code:
q1 == null
which says that the variable is not currently referencing a Queue and

Code:
q1.peek() == null
which implies the Queue is empty - though there are edge cases where this may not be true, just don't go there yet
__________________
-- Hope that helps

Last edited by jelly : 01-29-2008 at 01:39 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-29-2008, 04:00 AM
Member
 
Join Date: Jan 2008
Posts: 12
rhm54 is on a distinguished road
Thanks for your help! The variables not being intialized and the null comparison were at fault. Now I seem to have a logic problem that I cannot wrap my head around. The problem appears to be in the push method. I push on 5 numbers 1, 2, 3, 4, 5 in that order so when I pop() them I should get 5, 4, 3, 2, 1 however I get this instead:

1
5
3
2
The stack is empty, nothing to return
0

Here is the code:

Code:
package com.shadowconcept.mcdougal; import java.util.*; public class StackImplementationUsingTwoQueues { Queue<Integer> q1 = new LinkedList<Integer>(); Queue<Integer> q2 = new LinkedList<Integer>(); public int pop() { if (q1.peek() == null) { System.out.println("The stack is empty, nothing to return"); int i = 0; return i; } else { int pop = q1.remove(); return pop; } } public void push(int data){ if (q1.peek() == null){ q1.add(data); } else { for (int i = 0; i < q1.size(); i++){ q2.add(q1.remove()); } q1.add(data); for (int j = 0; j < q2.size(); j++){ q1.add(q2.remove()); } } } public int top(){ if (q1.peek() == null){ int temp = 0; System.out.println("The Stack is empty returning value 0"); return temp; } return q1.peek(); } public int size(){ return q1.size(); } public static void main(String[] args) { StackImplementationUsingTwoQueues s1 = new StackImplementationUsingTwoQueues(); s1.push(1); s1.push(2); s1.push(3); s1.push(4); s1.push(5); System.out.println(s1.pop()); System.out.println(s1.pop()); System.out.println(s1.pop()); System.out.println(s1.pop()); System.out.println(s1.pop()); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stack not popping bugger New To Java 2 01-28-2008 05:59 PM
Stack Trace Java Tip Java Tips 0 12-10-2007 06:29 PM
Implementing more than one Interfaces JavaForums Java Blogs 0 12-06-2007 07:42 PM
I want to be able to do this with stacks and queues as well as with vectors carl New To Java 1 08-07-2007 08:05 AM
Help with heap and stack coco Advanced Java 1 08-06-2007 03:21 PM


All times are GMT +3. The time now is 08:23 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org