944,092 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2913
  • C++ RSS
Jul 22nd, 2006
0

function for merging two lists

Expand Post »
I am working out a function that merges two lists and returns a pointer to a third list which contains the merged lists. Can anyone help? Here is what I have thus far:

C++ Syntax (Toggle Plain Text)
  1. /STLList - use the list container of std library
  2. #include <list>
  3. #include <string>
  4. #include <cstdio>
  5. #include <iostream>
  6. //declare three list of objects
  7. using namespace std;
  8. list <string> Anames;
  9. list <string> BastroSigns;
  10. list <string> MergedNamesSigns;
  11. int main()
  12. {
  13. // input string
  14. cout << "input data - name - input 0 to end" << endl;
  15. while (true)
  16. {
  17. string name;
  18. cin >> name;
  19. if (( name.compare("x") == 0) || (name.compare ("x") == 0))
  20. {
  21. break;
  22. }
  23. Anames.push_back (name);
  24. }
  25. // sort list
  26. Anames.sortedInsert ();
  27. //display list A
  28. cout << "output is: " << endl;
  29.  
  30. while (!Anames.empty())
  31. {
  32. // get first on list
  33. string name = Anames.front();
  34. cout << name<< endl;
  35. // remove displayed name from list
  36. Anames.pop_front();
  37. }
  38. / input string
  39. cout << "input data - astrological sign - input 0 to end" << endl;
  40. while (true)
  41. {
  42. string sign;
  43. cin >> sign;
  44. if (( sign.compare("x") == 0) || (sign.compare ("x") == 0))
  45. {
  46. break;
  47. }
  48. sign.push_back (sign);
  49. }
  50. // sort list
  51. BastroSigns.sortedInsert ();
  52. //display list B
  53. cout << "output is: " << endl;
  54.  
  55. while (!BastroSigns.empty())
  56. {
  57. // get first on list
  58. string sign = BastroSign.front();
  59. cout << sign<< endl;
  60. // remove displayed sign from list
  61. BastroSigns.pop_front();
  62. }
  63. createsortedList()
  64. {
  65.  
  66.  
  67.  
  68. }
  69.  
  70. mergelists()
  71. //someOperation
  72. //someNewlist
  73. //somepointer to someNewList
  74.  
  75.  
  76. system ("PAUSE") ;
  77. return 0;

Last edited by WolfPack; Jul 22nd, 2006 at 10:02 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CyberLev is offline Offline
6 posts
since Jul 2006
Jul 22nd, 2006
0

Re: function for merging two lists

Please format your code
http://www.daniweb.com/techtalkforum...cement8-3.html

Colourful, but variable spaced and unindented - bad.
Monochrome, fixed width and indented - good.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 22nd, 2006
0

Re: function for merging two lists

Haven't you even tried writing the merge function? You may have written the other parts, but that is not relevent here. Merging two lists can't be that hard. It is just a matter of connecting the tail of one list, with the head of the other list.

Please remember that when you ask a question, you must try to post only the parts that are needed for us to answer that particular question. Posting all code and expecting us to weed through that is outright rude.

Since you are using the standard list, you can use the splice method.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 22nd, 2006
0

Re: function for merging two lists

I am sorry - I am new to this -
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CyberLev is offline Offline
6 posts
since Jul 2006
Jul 23rd, 2006
0

Re: function for merging two lists

Hi Wolfpack. thank you for responding. Sorry about the bad manners. I am new to this..I assure you I am a professional and polite person. The problem I am working on is basically to do the merge of two lists without using the std library. I have looked every where and I cannot find the code for the stl merge (). Is there a place I can find such things. I think this is analogous to finding the code behind operators and keywords. What are your thoughts? Can you help - I think I am being asked to re-invent the wheel..Thank you for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CyberLev is offline Offline
6 posts
since Jul 2006
Jul 23rd, 2006
0

Re: function for merging two lists

Basically, you look at the first element of each input list, and decide which one you want first.
You then remove that from the head of whichever input list and append it to your result list.
Keep doing that until both input lists are empty.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 23rd, 2006
0

Re: function for merging two lists

No need to appologise. We know you are a good fellow.

Quote ...
The problem I am working on is basically to do the merge of two lists without using the std library.
If you are already using the standard list, what is keeping you from using the merge function?

Quote ...
I have looked every where and I cannot find the code for the stl merge (). Is there a place I can find such things.
Well this code is old, but you can take a look. The copyright says that it is free as long as you do not delete the disclaimer. If anybody sees any legal issues in posting this link here, notify me and I will delete it.
Quote ...
I think this is analogous to finding the code behind operators and keywords. What are your thoughts? Can you help - I think I am being asked to re-invent the wheel.
Unless you are writing this code for academic purposes, I'd advice you to use the reliable standard library.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 25th, 2006
0

Re: function for merging two lists

Thank you Wolfpack. Yes it is for academic purposes. I am an intellectual property and computer law attorney and my opinion is that you have complied with the disclaimer..thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CyberLev is offline Offline
6 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: My Emergency Airtime Recharge Service
Next Thread in C++ Forum Timeline: How to make windows form from c++ code (GUI)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC