Stacks Question

This is a discussion on Stacks Question within the C++ Programming forums, part of the General Programming Boards category; Hi guys, just wondering how i would remove an unwanted string element from a string stack. Example: Code: void remove_string(stack ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    33

    Stacks Question

    Hi guys, just wondering how i would remove an unwanted string element from a string stack.

    Example:

    Code:
    void remove_string(stack <string>& string_stack, string unwanted);
    
    int main()
    {
        stack <string> S;
    
        string X = "Bob";
        string Y = "Bye";
        string T = "Hi";
        string U = "Hello";
    
       S.push(X);
       S.push(Y);
       S.push(T);
       S.push(U);
        
        
    system("PAUSE");
    return 0;
    }
    
    void remove_string(stack <string>& string_stack, string unwanted)
    {
    
    }
    much appreciated!
    Last edited by muran_pling; 05-27-2007 at 10:18 AM.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    147
    From some of the STL documentation from one vendor:

    A template container adaptor class that provides a restriction of functionality limiting access to the element most recently added to some underlying container type. The stack class is used when it is important to be clear that only stack operations are being performed on the container.

    What this means is that unless pushing or popping is enough, you should choose another container that has the feature of removing elements in the interior.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by JVene View Post
    From some of the STL documentation from one vendor:




    What this means is that unless pushing or popping is enough, you should choose another container that has the feature of removing elements in the interior.
    what if it asks to use stack container?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Use a temporary stack.

    Code:
    while current element is not the one you are looking for
      push the last element of the first stack on the second stack
      remove the last element of the first stack
    end while
    
    if the size of the first stack is now 0
      push the last element of the second stack on the first
      remove the last element of the second stack
    else
      remove the last element of the first stack (the element you were looking for)
      while the size of the second stack is different of 0
        push the last element of the second stack on the first stack
        remove the last element of the second stack
      end while
    end if

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by Desolation View Post
    Use a temporary stack.

    Code:
    while current element is not the one you are looking for
      push the last element of the first stack on the second stack
      remove the last element of the first stack
    end while
    
    if the size of the first stack is now 0
      push the last element of the second stack on the first
      remove the last element of the second stack
    else
      remove the last element of the first stack (the element you were looking for)
      while the size of the second stack is different of 0
        push the last element of the second stack on the first stack
        remove the last element of the second stack
      end while
    end if
    this is what i came up with.. my code doesnt seem to be working now..
    Code:
    void remove_string(stack <string>& string_stack, string unwanted)
    {
         stack <string> temp_stack;
         
         string W = "dont";
         string X = "you";
         string Y = "why";
         string Z = "high";
         
         
         unwanted = "high";
         
         
         string_stack.push(W);
         string_stack.push(X);
         string_stack.push(Y);
         string_stack.push(Z);
         
         
         cout<<string_stack.size();
         cout<<endl;
         
         
        
        while(!string_stack.top()==unwanted)
         {
            temp_stack.push(string_stack.top());        
            string_stack.pop();                   
         }
         
         
         if(string_stack.size()==0)
         {
           string_stack.push(temp_stack.top());
           temp_stack.pop();
         }
         else
         {
           string_stack.pop();
            while(!temp_stack.size()==0)
            {
             string_stack.push(temp_stack.top());
             temp_stack.pop();
            }
         }
         cout<<endl;
         cout<<string_stack.size();
    
    }
    any ideas?
    Last edited by muran_pling; 05-28-2007 at 11:23 AM.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Define "not working". I'm not guessing your problems. Sorry.

    Edit: Bahh.. I'm feeling generous. I'll tell you that you are missing parentheses. Think operator precedence.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by Desolation View Post
    Define "not working". I'm not guessing your problems. Sorry.
    Ok well, its actually only removing the last string. Its not removing the unwanted string.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    void remove_string(stack <string>& string_stack, string unwanted)
    {
         // ...
        while(!string_stack.top()==unwanted)
         {
            temp_stack.push(string_stack.top());        
            string_stack.pop();                   
         }
         
         
         if(string_stack.size()==0)
         {
           //...
         }
         else
         {
           string_stack.pop();
            while(!temp_stack.size()==0)
            {
             //...
            }
         }
         //...
    
    }

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by Desolation View Post
    Define "not working". I'm not guessing your problems. Sorry.

    Edit: Bahh.. I'm feeling generous. I'll tell you that you are missing parentheses. Think operator precedence.
    hehehe hrmmmm, this will take forever to figure out .. ill get there! thanks heaps anyway man. appreciate the help

    Edit: Ohhhh the exclamation mark must go in front of the equals sign ? and i put an extra equals sign...
    Last edited by muran_pling; 05-28-2007 at 12:12 PM.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    I've told you you had missing parentheses, I told you to look for operator precedence and I've even highlighted the two faulty lines.. ' ! '

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    void remove_string(stack <string>& string_stack, string unwanted)
    {
         // ...
        while(string_stack.top()!=unwanted)
         {
            temp_stack.push(string_stack.top());        
            string_stack.pop();                   
         }
         
         
         if(string_stack.size()==0)
         {
           //...
         }
         else
         {
           string_stack.pop();
            while(temp_stack.size()!=0)
            {
             //...
            }
         }
         //...
    
    }
    Simple as that..

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by Desolation View Post
    I've told you you had missing parentheses, I told you to look for operator precedence and I've even highlighted the two faulty lines.. ' ! '

    yeah i wrote those messages before i saw your hints... thanks man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  2. N00B question about stacks
    By LobbDogg in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2003, 12:26 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. question about stacks (and vectors too for that matter)
    By Silvercord in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2003, 11:26 AM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 02:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21