Need help with stacks

This is a discussion on Need help with stacks within the C++ Programming forums, part of the General Programming Boards category; hi guys, im trying to complete a function where you have stack S and stack T. Both integer type and ...

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

    Need help with stacks

    hi guys, im trying to complete a function where you have stack S and stack T. Both integer type and both have 4 elements in each stack which the elements are in ascending order (Top to bottom (eg..1 2 3 4 7 8)). Now i have to combine both stacks and make one union stack which combines both stacks S and T and puts that stack in ascending order.

    my program only outputs 5 elements and not 8 (combining of both S and T).

    Code:
    stack <int> unionS(stack<int>& S, stack<int>& T)
    {
      stack <int> unionStack;
      stack <int> a;
      stack <int> b;
    
      S.push(2);//top
      S.push(4);
      S.push(8);
      S.push(9);//bottom
      
      T.push(1);//top
      T.push(3);
      T.push(5);
      T.push(7);//bottom
      
      for(int j = S.size(); j>0; j--)
      {
          a.push(S.top());
          S.pop();
      }
      
      
      for(int g = T.size(); g>0; g--)
      {
          b.push(T.top());
          T.pop();
      }
      
      cout<<a.size()<<" ELEMENTS IN STACK a";
      cout<<endl;
      cout<<b.size()<<" ELEMENTS IN STACK b";
      cout<<endl;
      
      
      
      for(int x = 0; x<=(a.size())+(b.size()); x++)
      {
    
           if(a.top()<=b.top())
           {
            unionStack.push(a.top());
            a.pop();
           }
           else
           {
            unionStack.push(b.top());
            b.pop();
           }  
    
    
       }
       
       cout<<unionStack.size();
       cout<<endl;
     
       
       
    }
    any1 know why i can only get 5 and not all 8 into the unionStack that unites both S and T and puts all the numbers in ascending order? thank u!
    Last edited by muran_pling; 06-03-2007 at 05:06 AM.

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    its ok guys, i got it working

    Code:
    stack <int> unionS(stack<int>& S, stack<int>& T)
    {
      stack <int> unionStack;
      stack <int> unionStackFinal;
      stack <int> unionStackFinal2;
      stack <int> a;
      stack <int> b;
    
      S.push(1);//top
      S.push(6);
      S.push(8);
      S.push(9);//bottom
      
      T.push(4);//top
      T.push(5);
      T.push(6);
      T.push(7);//bottom
      
      for(int j = S.size(); j>0; j--)
      {
          a.push(S.top());
          S.pop();
      }
      
      
      for(int g = T.size(); g>0; g--)
      {
          b.push(T.top());
          T.pop();
      }
      
      cout<<a.size()<<" ELEMENTS IN STACK a";
      cout<<endl;
      cout<<b.size()<<" ELEMENTS IN STACK b";
      cout<<endl;
    
      
      for(int x = 0; x<=(a.size() + b.size()); x++)
      {
    
         if(a.top()<=b.top())
         {
           unionStack.push(a.top());
           a.pop();
         }
         
         if(b.top()<=a.top())
         {
           unionStack.push(b.top());
           b.pop();
         }
         
         if(a.size()==0)
           {
             for(int z = b.size(); z>0; z--)
             {
              unionStack.push(b.top());
              b.pop();
             }
           }
           
         if(b.size()==0)
           {
             for(int f = a.size(); f>0; f--)
             {
              unionStack.push(a.top());
              a.pop();
             }
           }
     
    
    }
       cout<<endl;
       cout<<unionStack.size();
       cout<<endl;
       
     
       for(int h = unionStack.size(); h>=1; h--)
       {
          unionStackFinal.push(unionStack.top());
          unionStack.pop();
       }
       cout<<endl;
       for(int b = unionStackFinal.size(); b>=1; b--)
       {
          cout<<unionStackFinal.top();
          unionStackFinal.pop();
       }
       
       cout<<endl;
     
    }

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    There are still a number of problems with that code.
    Your 'x' for loop is not right. Each iteration the size of a or b decreases and x also increases.
    You'd be better off using an infinite loop "for(;; )" and "break"ing out of the loop when either stack becomes empty. I also suggest putting the test for an empty stack first so that combining two stacks where one was empty to begin with, also works.

    Also, each iteration, one OR two items are added to the unionStack. You should instead make these use an if-else instead:
    Code:
         if(a.top()<=b.top())
    ...
         if(b.top()<=a.top())
    ...
    becomes:
    Code:
         if(a.top()<=b.top())
    ...
         else
    ...
    Lastly, you shouldn't use ".size()==0". To specifically test for an empty container you should use ".empty()". Cleaner, and potentially more efficient when all you care about is whether it is zero or not, rather than the exact value.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by iMalc View Post
    There are still a number of problems with that code.
    Your 'x' for loop is not right. Each iteration the size of a or b decreases and x also increases.
    You'd be better off using an infinite loop "for(;; )" and "break"ing out of the loop when either stack becomes empty. I also suggest putting the test for an empty stack first so that combining two stacks where one was empty to begin with, also works.

    Also, each iteration, one OR two items are added to the unionStack. You should instead make these use an if-else instead:
    Code:
         if(a.top()<=b.top())
    ...
         if(b.top()<=a.top())
    ...
    becomes:
    Code:
         if(a.top()<=b.top())
    ...
         else
    ...
    Lastly, you shouldn't use ".size()==0". To specifically test for an empty container you should use ".empty()". Cleaner, and potentially more efficient when all you care about is whether it is zero or not, rather than the exact value.
    thanks for this. i will have a look at it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 07:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  3. Avioding Stacks
    By LoafOfBread34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 05:20 AM
  4. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 05:48 PM
  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