need help with Linked list

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

Join Date: Sep 2008
Posts: 15
Reputation: opposition is an unknown quantity at this point 
Solved Threads: 0
opposition opposition is offline Offline
Newbie Poster

need help with Linked list

 
0
  #1
Sep 9th, 2008
Hey, Im having some trouble with linked lists, I have been givin a task where I am asked to crreate a linked list which reads in a random set of chars from a .txt file and stores them in a linked list, then prints them out in a seperate function.

here is my code


  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8. char base;
  9. node* next;
  10. };
  11.  
  12. void initialise(node*& head);
  13. void read(node*& head);
  14. void print(node*& head);
  15.  
  16. int main()
  17. {
  18. node* head = NULL;
  19.  
  20. cout << "Set linked list to NULL" << endl;
  21. initialise(head);
  22.  
  23. cout << "Reading from file into linked list" << endl;
  24. read(head);
  25.  
  26. cout << "Printing out linked list" << endl;
  27. print(head);
  28.  
  29. return 0;
  30. }
  31. void initialise(node*& head)
  32. {
  33. head = NULL;
  34. }
  35. void read(node*& head)
  36. {
  37. node* temp;
  38. char filename[40];
  39. ifstream ins;
  40.  
  41. cout << "Filename: ";
  42. cin >> filename;
  43.  
  44. ins.open(filename, ios::in);
  45. temp = new node;
  46. if (temp == NULL)
  47. {
  48. cout << "An error occured: " << endl;
  49. exit(0);
  50. }
  51. if(ins.good())
  52. {
  53. while(!ins.eof())
  54. {
  55. ins >> temp->base;
  56. temp->next = NULL;
  57.  
  58. if (head == NULL)
  59. head = temp;
  60. else
  61. {
  62. node* z;
  63. z = head;
  64.  
  65. while (z->next != NULL)
  66. {
  67. z = z->next;
  68. }
  69. z->next=temp;
  70. }
  71. }
  72. }
  73. }
  74. void print(node*& head)
  75. {
  76. node* temp;
  77. temp = head;
  78. while (temp != NULL)
  79. {
  80. cout << temp->base << endl;
  81. temp = temp->next;
  82. }
  83. }

text file contains random chars, like "AINFUEH EIUDNUIEW IWEUNF" and whitespaces are ignored.

for some reason, when it prints out, it gets stuck in a loop and only prints out the last char in the text file over and over...

can someone please tell me what im doing wrong?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help with Linked list

 
1
  #2
Sep 9th, 2008
Looks to me like no new nodes are created in this loop:

  1. while(!ins.eof())
  2. {
  3. ins >> temp->base;
  4. temp->next = NULL;
  5.  
  6. if (head == NULL)
  7. head = temp;
  8. else
  9. {
  10. node* z;
  11. z = head;
  12.  
  13. while (z->next != NULL)
  14. {
  15. z = z->next;
  16. }
  17. z->next=temp;
  18. }
  19. }

You are just continually putting a new value into temp->base and overwriting the old value. As for the infinite loop, I haven't tested it out, but I'm guessing that temp == temp->next and possibly head == temp. You need to create a new node every time you read a new value from the input file.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 15
Reputation: opposition is an unknown quantity at this point 
Solved Threads: 0
opposition opposition is offline Offline
Newbie Poster

Re: need help with Linked list

 
0
  #3
Sep 9th, 2008
Originally Posted by VernonDozier View Post
You are just continually putting a new value into temp->base and overwriting the old value.
haha thank you very much, that was exactly my problem, I originally had it reading from cin, and forgot to move my new node line into the eof loop, works perfectly now

+rep
Last edited by opposition; Sep 9th, 2008 at 4:47 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 770 | Replies: 2
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC