Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Remove a number from a stack

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Remove a number from a stack

    Hello! I am pretty new to java, so pleasee be patient with me !!

    Ok, so I am trying to remove a value from a stack that is not in the top of the stack using only stacks, so I basically created 2 stacks pila and pilaTemp and move values from one another and then push back the values a move to the second stack, not sure if I explain my self, but anyways here is the code. I have a problem with the loops I think, but I've been trying to understand what I am doing wrong but nothing comes to my mind, and what I try does not work. For example.. I am able to remove the top value (32).. but if I want to remove 3, it won't do anything! If anyone can send me in the right direction I'll appreciate it! thanks!!

    Here is the code:

     
    public class Pila {
     
        Stack<Integer> pila = new Stack<>();
        Stack<Integer> pilaTemp = new Stack<>();
        //Stack<Integer> pilaDestino = new Stack<>();
     
     
        public void Pila() {
            pila.push(4);
            pila.push(20);
            pila.push(25);
            pila.push(3);
            pila.push(32);
        }
     
      public void sacaNumero(int n){
     
          for(int i = 0; i<pila.size(); i++){
     
            if(pila.peek()== n){
              pila.pop();
                } else if(pila.peek()!= n){
                    do{
                    pilaTemp.push(pila.pop());
                        if(pila.peek() == n)
                        pila.pop();
                        pila.push(pilaTemp.pop());
     
                } while(pila.peek() != n && pila != null);
     
          }  else if(pila.isEmpty() || pila.search(n) == -1){
              System.out.print("No se puede completar la acción. El número no existe o la pila está vacía");
          }
            System.out.print(pila);
          }
      }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Remove a number from a stack

    The do/while loop in the sacaNumero() method is an infinite loop. You can verify this by adding a print statement inside the loop somewhere. You might try writing down the steps to find the desired item in the stack, discard it, and then rebuild the original stack without it in your native language - maybe in code comments (novel idea) - and then code that.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Remove a number from a stack

    Hi Greg!

    Ok! I understand what you are saying and trust you that it is an infinite loop (no wonder why the CPU usage is 100% haha), I thought that while (pila.peek() != n || pila.isEmpty()) this was the stop. I mean I want it to go through the whole stack (pila) and do the pila.pop() and push that value into pilaTemp (temporary stack). I mean it is just four values and I trying to access the second one (#3) so in my head it should stop when 3 = 3. It is obviously wrong, but I just can't see it, it's frustrating! Is this a normal feeling when you begin programming? hehe

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Remove a number from a stack

    Again, I think writing down the steps would be helpful. As a hint, your current code is much more complicated than it needs to be. I suggest you start that method over from a blank sheet of paper, beginning with the list of steps before writing any code.

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Remove a number from a stack

    Hi Greg!
    I was able to do it as you said, I built my stack without the usage of collections and worked great! Thanks for the tip, drafting it in paper worked for me.

    I am now having an issue with queues... I was able to built my queue and it works fine, however I have an issue, I´ve been trying to change to my loops but I always get the same result... I want to remove a number from my queque maintaining the queue order, I am able to remove the number, but I am not able to maintain the order of it, can you help me with it? I should be removing the name and continue moving my queue to my aux queue but it is not happening that.. Ive tried different ways but I always get the same result.. not sure how to make it continue.

     
    public NodoC extrae(int x, Cola fuente) {
     
            NodoC retorno = null; 
            Cola aux = new Cola(); 
            NodoC n = fuente.atiende(); 
     
            while (n != null && retorno == null) { 
                if (n.getDato() == x) { 
     
                    retorno = n;
                } else {
                    aux.encola(n);           
                    n = fuente.atiende();
                }
            }
     
            n = aux.atiende();
            while (n != null) {
                fuente.encola(n);
                n = aux.atiende();
            }
            return retorno;
        }

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Remove a number from a stack

    Removing an item from a List or Queue is very simple.
    head = head.next;
    Improving the world one idiot at a time!

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  3. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  4. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM