Linked List with Char Array problem

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 3
Reputation: Drifter666 is an unknown quantity at this point 
Solved Threads: 0
Drifter666 Drifter666 is offline Offline
Newbie Poster

Linked List with Char Array problem

 
0
  #1
Apr 1st, 2009
Hey All,
I'm having this weird issue and I'm not sure what to do. In the main function when I put one letter it'll work, once I put a string of letters it doesn't work. I've tried an assortment of stuff and I dunno what to do anymore.

This is the code that works when I put one letter in. Right now in the main it has "first", "second", and "third" and I get error C2664: cannot convert parameter 1 from 'const char[6]' to 'char'.

Thanks in advanced.

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class CharNode
  6. {
  7. public:
  8. CharNode(){}
  9. CharNode(char d, CharNode *lPtr)
  10. : data(d), linkPtr(lPtr){}
  11. CharNode *getLink() const {return linkPtr;}
  12. char getData() const {return data;}
  13. void setData(char d) {data=d;}
  14. void setLink(CharNode *lPtr) {linkPtr = lPtr;}
  15. void headInsert(CharNode* &head, char d);
  16. void deleteTail (CharNode *before);
  17. void printAll();
  18. private:
  19. char data;
  20. CharNode *linkPtr;
  21. };
  22.  
  23. void CharNode::headInsert(CharNode* &head, char d)
  24. {
  25. head = new CharNode(d, head);
  26. }
  27.  
  28. void CharNode::deleteTail(CharNode *head)
  29. {
  30. CharNode *discard, *tail;
  31. tail = head;
  32.  
  33. // move tail to the last but one node
  34.  
  35. while ((tail->getLink())->getLink() != NULL)
  36. tail = tail -> getLink();
  37.  
  38. tail -> setLink(NULL);
  39. discard = tail->getLink();
  40. delete discard;
  41. }
  42.  
  43. void CharNode::printAll()
  44. {
  45. if (this == NULL)
  46. cout << "Empty list\n";
  47.  
  48. else
  49. {
  50. int i = 1;
  51. CharNode *tempPtr = this;
  52. cout << "The linked list is: \n";
  53. while (tempPtr != NULL)
  54. {
  55. cout << "Object " << i++ << ": " << tempPtr->getData() << endl;
  56. tempPtr = tempPtr->getLink();
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. CharNode *head = new CharNode("First", NULL);
  64. head->headInsert(head,"Second");
  65. head->headInsert(head,"Third");
  66. head->printAll();
  67. delete head;
  68. head=NULL;
  69. head->printAll();
  70.  
  71. system("PAUSE");
  72. return 0;
  73. }
Last edited by Drifter666; Apr 1st, 2009 at 10:39 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 145
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 35
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Linked List with Char Array problem

 
0
  #2
Apr 1st, 2009
Your CharNode contains member "char data;" and you're trying to assign a string "First" to that char. That isn't going to work because "First" is actually a pointer to a null terminated array of chars and you're trying to say: char = pointer to char.
If you want strings in your linked list you need to provide a different member type for the storage in the CharNode class.
This could be an STL string, a char* to memory you'll have to allocate yourself or, the simplest solution is a fixed length char array (if you do this make sure you copy all chars to it, don't just try and copy the pointer).
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: Drifter666 is an unknown quantity at this point 
Solved Threads: 0
Drifter666 Drifter666 is offline Offline
Newbie Poster

Re: Linked List with Char Array problem

 
0
  #3
Apr 1st, 2009
Thanks MrSpigot. Makes sense.


Ok. I made them pointers. The program runs but it only prints out the first letter of each string. So I tried to make the headinsert function like this:

head = new CharNode((strlen(d)+1), head);

but that doesn't work either.

I'm just looking for an easy way out right now. Let's say the fixed character length is 20. But when I try to implement I get tons more errors. I'm not familiar with linked lists at all, so this is all kinda new to me.

Again, thanks in advanced.

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class CharNode
  6. {
  7. public:
  8. CharNode(){}
  9. CharNode(char *d, CharNode *lPtr)
  10. : data(d), linkPtr(lPtr){}
  11. CharNode *getLink() const {return linkPtr;}
  12. char getData() const {return *data;}
  13. void setData(char *d) {data=d;}
  14. void setLink(CharNode *lPtr) {linkPtr = lPtr;}
  15. void headInsert(CharNode* &head, char *d);
  16. void deleteTail (CharNode *before);
  17. void printAll();
  18. private:
  19. char *data;
  20. CharNode *linkPtr;
  21. };
  22.  
  23. void CharNode::headInsert(CharNode* &head, char *d)
  24. {
  25. head = new CharNode(d, head);
  26. }
  27.  
  28. void CharNode::deleteTail(CharNode *head)
  29. {
  30. CharNode *discard, *tail;
  31. tail = head;
  32.  
  33. // move tail to the last but one node
  34.  
  35. while ((tail->getLink())->getLink() != NULL)
  36. tail = tail -> getLink();
  37.  
  38. tail -> setLink(NULL);
  39. discard = tail->getLink();
  40. delete discard;
  41. }
  42.  
  43. void CharNode::printAll()
  44. {
  45. if (this == NULL)
  46. cout << "Empty list\n";
  47.  
  48. else
  49. {
  50. int i = 1;
  51. CharNode *tempPtr = this;
  52. cout << "The linked list is: \n";
  53. while (tempPtr != NULL)
  54. {
  55. cout << "Object " << i++ << ": " << tempPtr->getData() << endl;
  56. tempPtr = tempPtr->getLink();
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. CharNode *head = new CharNode("First", NULL);
  64. head->headInsert(head,"Second");
  65. head->headInsert(head,"Third");
  66. head->printAll();
  67. delete head;
  68. head=NULL;
  69. head->printAll();
  70.  
  71. system("PAUSE");
  72. return 0;
  73. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List with Char Array problem

 
0
  #4
Apr 1st, 2009
  1. void CharNode::headInsert(CharNode* &head, char *d)
  2. {
  3. head = new CharNode(d, head);
  4. }

What the above says is create a new list every time you insert a new item. T'm almost positive that isn't what you want. I strongly recommend you have two classes, not one. One to define a node that will be inserted in a list, and the other to define the list itself. The node class will have one or more data member variables and one (or more if you want more than a singly linked list) pointer variable(s) to the node type itself. When you want to insert a new node into a list you declare memory for a new node, not a new list. Each list should have a node pointer to keep track of the first node in the list which is often called head.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: Drifter666 is an unknown quantity at this point 
Solved Threads: 0
Drifter666 Drifter666 is offline Offline
Newbie Poster

Re: Linked List with Char Array problem

 
0
  #5
Apr 1st, 2009
Thanks Lerner. I will take that approach next time.

I messed with the code again and with a little help from a local friend, we fixed it finally. Something I overlooked in the class definition. Thanks for your help guys.

here's the final code in case someone else needs to take a look.

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class CharNode
  6.  
  7. {
  8. public:
  9. CharNode(){}
  10. CharNode(char d[], CharNode *lPtr)
  11. : linkPtr(lPtr)
  12. {
  13. strcpy(data,d);
  14. }
  15. CharNode *getLink() const {return linkPtr;}
  16. char *getData() {return data;}
  17. void setLink(CharNode *lPtr) {linkPtr = lPtr;}
  18. void headInsert(CharNode* &head, char *d);
  19. void deleteTail (CharNode *before);
  20. void printAll();
  21. private:
  22. char data[20];
  23. CharNode *linkPtr;
  24. };
  25.  
  26. void CharNode::headInsert(CharNode* &head, char *d)
  27. {
  28. head = new CharNode(d, head);
  29. }
  30.  
  31. void CharNode::deleteTail(CharNode *head)
  32. {
  33. CharNode *discard, *tail;
  34. tail = head;
  35.  
  36. // move tail to the last but one node
  37.  
  38. while ((tail->getLink())->getLink() != NULL)
  39. tail = tail -> getLink();
  40.  
  41. tail -> setLink(NULL);
  42. discard = tail->getLink();
  43. delete discard;
  44. }
  45.  
  46. void CharNode::printAll()
  47. {
  48. if (this == NULL)
  49. cout << "Empty list\n";
  50.  
  51. else
  52. {
  53. int i = 1;
  54. CharNode *tempPtr = this;
  55. cout << "The list is: ";
  56. while (tempPtr != NULL)
  57. {
  58. cout << tempPtr->getData() << " ";
  59. tempPtr = tempPtr->getLink();
  60. }
  61. }
  62. }
  63.  
  64. int main()
  65. {
  66. CharNode *head = new CharNode("First", NULL);
  67. head->headInsert(head,"Second");
  68. head->headInsert(head,"Third");
  69. head->printAll();
  70. cout << endl;
  71. delete head;
  72. head=NULL;
  73. head->printAll();
  74.  
  75. system("PAUSE");
  76. return 0;
  77. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 679 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC