473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

recursion in stack to get the maximum element and copying stacks

am still new to stacks and recursion and am trying to implement two functions using recursion to get the maximum element in a stack and copying one stack into the other
but it keeps repeating some elements and i dont understand why!!

this is the code for the functions:

Expand|Select|Wrap|Line Numbers
  1. void copyStack(MyStack &firstStack, MyStack &secondStack)
  2. {
  3.     if(!firstStack.empty())    {
  4.         string x=firstStack.top();
  5.         firstStack.pop();
  6.         //secondStack.push(x);
  7.         copyStack(firstStack,secondStack);
  8.         secondStack.push(x);
  9.         firstStack.push(x);
  10.     }
  11. }
  12. string getMax(MyStack &stack)
  13. {
  14.     string a=stack.top();
  15.     stack.pop();
  16.     getMax(stack,a);
  17.     stack.push(a);
  18.     return a;    
  19. }
  20.  
  21. void getMax(MyStack &stack,string &max)
  22. {
  23.     if(stack.empty()!=true)
  24.     {
  25.         string x=stack.top();
  26.         if(x>max)
  27.             max=x;
  28.         stack.pop();
  29.         getMax(stack,max);
  30.         stack.push(x);
  31.     }
  32. }


any answer can help me alot!!
thank you in advance ^_^
Nov 22 '12 #1
2 2888
It looks okey, except that those will revert the order of the stack, what is the implementation of the class MyStack?
Nov 22 '12 #2
hehehe it has an inheritance from a linked list :P

this is the linked list
Expand|Select|Wrap|Line Numbers
  1. #include "LList.h"
  2.  
  3. LList::LList(void)
  4. {
  5.     first=0;
  6. }
  7.  
  8. LList::~LList(void)
  9. {
  10.     initializeList();
  11. }
  12. bool LList::isEmpty()
  13. {
  14.     if(first==0)
  15.         return true;
  16.     else
  17.         return false;
  18. }
  19. bool LList::isFull()
  20. {
  21.     return false;
  22. }
  23. void LList::initializeList()
  24. {
  25.     Node* temp;
  26.     if(first!=0)
  27.     {
  28.         temp=first->getLink();
  29.         delete first;
  30.         first=temp;
  31.         initializeList();
  32.     }
  33. }
  34. int LList::getSize()
  35. {
  36.     int count=0;
  37.     if(first!=0){    Node* temp=first;
  38.  
  39.     while(temp!=0)
  40.     {
  41.         temp=temp->getLink();
  42.         count++;
  43.     }}
  44.     return count;
  45. }
  46. string LList::front()
  47. {
  48.     if(first!=0)
  49.         return first->getInfo();
  50.     else
  51.         return "";
  52. }
  53. string LList::back()
  54. {
  55.     Node*temp=first;
  56.     if(temp==0)
  57.         return "";
  58.     else
  59.         while(temp->getLink()!=0)
  60.             temp=temp->getLink();
  61.     return temp->getInfo();
  62. }
  63. void LList::insertAt(int position,string value)
  64. {
  65.     Node*current;
  66.     Node* newN=new Node;
  67.     int counter=0;
  68.     newN->setInfo(value);
  69.     if(first==0)
  70.     {
  71.         first=new Node;
  72.         first->setInfo(value);
  73.         first->setLink(0);
  74.     }
  75.     if(position==0)
  76.     {
  77.         newN->setLink(first);
  78.         first=newN;
  79.     }
  80.     else
  81.     {
  82.         current=first;
  83.         while(counter<position&&current->getLink()!=0)
  84.         {
  85.             current=current->getLink();
  86.             ++counter;
  87.         }
  88.         newN->setLink(current->getLink());
  89.         current->setLink(newN);
  90.     }
  91.  
  92. }
  93. void LList::deleteAt(int pos)
  94. {        int count=0;
  95. Node* current=first;
  96. Node*trail=new Node;
  97. if(first!=0)
  98. {
  99.     if(pos==0)
  100.         if(first->getLink()!=0)
  101.         {
  102.             trail=current->getLink();
  103.             delete current;
  104.             first=trail;
  105.         }
  106.         else
  107.         {
  108.             delete first;first=0;
  109.         }
  110.     else{
  111.  
  112.  
  113.         while(count<pos&&current->getLink()!=0)
  114.         {
  115.             trail=current;
  116.             current=current->getLink();
  117.             ++count;
  118.         }
  119.         trail->setLink(current->getLink());
  120.         delete current;}
  121. }
  122.  
  123. }
  124.  



an this is the stack
Expand|Select|Wrap|Line Numbers
  1. void MyStack::push(string value)
  2. {
  3.     insertAt(0,value);
  4. }    
  5. void MyStack::pop()
  6. {
  7.     deleteAt(0);
  8. }
  9. string MyStack::top()
  10. {
  11.     return front();
  12. }
  13. bool MyStack::empty()
  14. {
  15.     return isEmpty();
  16. }
  17. bool MyStack::full()
  18. {
  19.     return isFull();
  20. }
  21. int MyStack::size()
  22. {
  23.     return getSize();
  24. }
and the linked list has a first of type Node
this is Node class
Expand|Select|Wrap|Line Numbers
  1. Node* Node::getLink()
  2. {
  3. return link;
  4. }
  5. void Node::setLink(Node* newLink)
  6. {
  7. link=newLink;
  8. }
  9. string Node::getInfo()
  10. {
  11. return info;
  12. }
  13. void Node::setInfo(string newInfo)
  14. {
  15. info=newInfo;
  16. }
Nov 23 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Andy Leszczynski | last post by:
I need to pickle quite complex objects and first limitation was default 200 for the recursion. sys.setrecursionlimit helped, but still bigger objects fail to be pickled because of XP stack size...
3
by: sternr | last post by:
Hey, I'm using in my application a System.Collections.Stack object. Is there a way to define a maximum size for the this stack object, That when the max size is reached, the oldest value is...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
5
by: Sunny123 | last post by:
hello i am trying to find the maximum value of a function and where it occur. i.e there is an array x |y ============= | |
2
by: Daniel | last post by:
Hi, I have a question regarding the memory managment in stl stacks. I want to use stacks to store a very large amount of numbers (some millions), thus I'm interested in how the stack behaves...
14
by: MLH | last post by:
Suppose you needed to print a large number of consecutively numbered documents the size of a post-card ==or perhaps even a business card using a laser printer and 60# bond. Suppose you planned to...
2
by: raghu | last post by:
What is a stack of stack? Can anyone please give me the idea to implement it.... Thanks a lot... Happy New Year Regards, Raghu
7
by: samelzoro | last post by:
here is a problem in recursion: unexpected result ? by this program I just want to convert xml dom's document object to xml-string. (for all browsers) //load a xml function...
4
by: jaime.dyson | last post by:
Hello all, I have the unenviable task of turning about 20K strangely formatted XML documents from different sources into something resembling a clean, standard, uniform format. I like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.