Results 1 to 4 of 4
  1. #1
    rhm54 is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default 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:

    Java 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();
    
    	}
    
    }

  2. #2
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    I suspect that the reason your code is crashing is due to the code

    Java 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

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

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

    Java 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 :)
    Last edited by jelly; 01-29-2008 at 01:39 AM.
    -- Hope that helps

  3. #3
    rhm54 is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    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:

    Java 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());
    		}
    
    }

  4. #4
    mintsolutions is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Thumbs up Reply to implementing Stack using two queues

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    import java.util.*;

    /**
    *
    * @author Mahmood
    */
    public class StackImplUsingQueues {

    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 = q1.size(); i > 0; i--) {
    q2.add(q1.remove());
    }
    q1.add(data);
    for (int j = q2.size(); j > 0; j--) {
    q1.add(q2.remove());
    }

    }
    }

    public static void main(String[] args) {
    StackImplUsingQueues s1 = new StackImplUsingQueues();
    // Stack s1 = new Stack();
    s1.push(1);
    s1.push(2);
    s1.push(3);
    s1.push(4);
    s1.push(5);
    s1.push(6);
    s1.push(7);
    s1.push(8);
    s1.push(9);
    s1.push(10);
    // s1.push(6);
    System.out.println("1st = " + s1.pop());
    System.out.println("2nd = " + s1.pop());
    System.out.println("3rd = " + s1.pop());
    System.out.println("4th = " + s1.pop());
    System.out.println("5th = " + s1.pop());
    System.out.println("6th = " + s1.pop());
    System.out.println("7th = " + s1.pop());
    System.out.println("8th = " + s1.pop());
    System.out.println("9th = " + s1.pop());
    System.out.println("10th= " + s1.pop());
    }
    }
    :)

Similar Threads

  1. Stack not popping
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 05:59 PM
  2. Stack Trace
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 06:29 PM
  3. Replies: 1
    Last Post: 08-07-2007, 08:05 AM
  4. Help with heap and stack
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •