Concordance, Linked Lists and classes

Reply

Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Concordance, Linked Lists and classes

 
0
  #1
Oct 15th, 2005
I've got a program to do for a class I'm taking. I've got most the code written but I can't get the two classes to play together like I want them to.

Here is the relavent part of the assignment:
A concordance is an alphabetical listing of all words in a text
with a list associated with each word of the line numbers in which the
word appears

Design and implement two C++ classes for linked lists (see below).

Design, code and test a C++ program using the linked lists that
reads a text file (using file redirection) and generates a concordance of
all the words from that text.

You will need a linked list of strings and linked list of numbers, Each
node of the string list must point to a list of numbers.

One class, wordlist, uses the other class numlist to keep a list of line numbers in which a certain word was found. When I test both classes alone, they work as I think they should, but when I try to use numlist called from wordlist, it only remembers the first line number inserted when you put the same word in.

wordlist.h
  1. #ifndef linkdlst_h
  2. #include <iostream>
  3. #include <string>
  4. /* HUGELY IMPORTANT NOTE:
  5.  *
  6.  * This class is dependant upon the other node class! It will fail without it! */
  7. #include "numlist.h"
  8.  
  9.  
  10. using namespace std;
  11.  
  12. class concordance
  13. {
  14. private:
  15.  
  16. struct node
  17. {
  18. string data;
  19. numlist line_nums;
  20. node *next;
  21. };
  22. node *list_head;
  23.  
  24.  
  25.  
  26. // Methods
  27. bool is_before(string list_data, string data);
  28. bool is_same(string list_data, string data);
  29. bool is_after(string list_data, string data);
  30. //Note: is_first is implied--it's a one line thing
  31. string tolowercase(string str);
  32.  
  33. public:
  34. concordance();
  35. ~concordance();
  36.  
  37. void append(string data, int n);
  38. void insert(string data, int n);
  39. void print();
  40. };
  41.  
  42. #define linkdlst_h
  43. #endif

Exert from wordlist.cpp (I didn't include the private functions such as touppercase and is_same--they work.)
  1. concordance::concordance()
  2. {
  3. list_head = NULL;
  4. }
  5.  
  6. concordance::~concordance()
  7. { //The destructor may have to do something here.
  8. node *previous;
  9. node *current;
  10. previous = current = list_head;
  11. while (current != NULL) //Delete each item in the list.
  12. {
  13. current = previous -> next;
  14. previous -> line_nums.~numlist();
  15. delete previous;
  16. previous = current;
  17. }
  18. previous = current = list_head = NULL;
  19. }
  20.  
  21. void concordance::insert(string data, int n)
  22. {
  23. node* current = list_head;
  24. node* previous = list_head;
  25. node* newnode = NULL;
  26. bool done = false; // After the loop has executed, exit it.
  27.  
  28. // Create a node to insert the data into
  29. newnode = new(node);
  30. newnode -> data = data;
  31. newnode -> line_nums.append(n);
  32.  
  33. // Build the list by inserting data in an organized manner.
  34. if (list_head != NULL)
  35. {
  36. while (done == false)
  37. {
  38. if (is_before(current -> data, data))
  39. { //Insert into the list
  40. newnode -> next = previous -> next;
  41. previous -> next= newnode;
  42. done = true;
  43. }
  44. else if (is_same(current -> data, data))
  45. {
  46. // cout << "is_same ";
  47. //Append line number to line number list
  48. newnode -> line_nums.append(n);
  49. done = true;
  50. }
  51. else
  52. { //if !is_before and !is_same, it must be is_after.
  53. if (current -> next == NULL)
  54. {
  55. //The end of list has been reached.
  56. //Insert at the end
  57. newnode -> next = NULL;
  58. current -> next = newnode;
  59. done = true;
  60. }
  61. else
  62. {
  63. previous = current;
  64. current = current -> next;
  65. done = false;
  66. }
  67. }
  68. }
  69. }
  70. else
  71. {
  72. newnode -> next = NULL;
  73. list_head = newnode;
  74. }
  75. }
  76. void concordance::print()
  77. {
  78. node *current;
  79. current = list_head;
  80. while (current != NULL)
  81. {
  82. cout << current -> data << " <- data | position -> ";
  83. current -> line_nums.print();
  84. cout << endl;
  85. current = current -> next;
  86. }
  87. }


numlist.h
  1. /* Source: Program 0, Assignment 7. */
  2. #ifndef numlist_h
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class numlist
  8. {
  9. private:
  10. struct node
  11. {
  12. int data;
  13. node *next;
  14. };
  15. node *list;
  16.  
  17. public:
  18. typedef node *node_ptr;
  19. numlist();
  20. ~numlist();
  21. bool defined;
  22.  
  23. void append(int n);
  24. void print();
  25.  
  26. };
  27.  
  28. #define numlist_h
  29. #endif

numlist.cpp
  1. #include "numlist.h"
  2.  
  3. numlist::numlist()
  4. {
  5. list = NULL;
  6. }
  7.  
  8. numlist::~numlist()
  9. { //The destructor may have to do something here.
  10. node *previous;
  11. node *current;
  12. previous = current = list;
  13. while (current != NULL) //Delete each item in the list.
  14. {
  15. current = previous -> next;
  16. delete previous;
  17. previous = current;
  18. }
  19. previous = current = list = NULL;
  20. }
  21.  
  22.  
  23. void numlist::append(int str)
  24. {
  25. node *current;
  26. node *newnode;
  27. current = newnode = NULL;
  28.  
  29. //Create a new node to put the data in.
  30. newnode = new(node);
  31. newnode -> data = str;
  32. newnode -> next = NULL;
  33.  
  34. cout << " List Head: " << list << " Current: " << current << endl;
  35. //Decide where to put it.
  36. if (current == NULL) //Then this is an empty list.
  37. {
  38. list = newnode;
  39. }
  40. else //Find the end of the list
  41. {
  42. while (current -> next != NULL)
  43. {
  44. current = current -> next;
  45. }
  46. cout << " List Head: " << list << " Current: " << current << endl;
  47. current -> next = newnode;
  48. }
  49. // print();
  50. }
  51.  
  52.  
  53. void numlist::print()
  54. {
  55. node *current;
  56. current = list;
  57. while (current != NULL)
  58. {
  59. cout << current -> data << " ";
  60. current = current -> next;
  61. }
  62. }

My test program for wordlist.cpp simply asks for input (cin) and uses the insert procedure to add data to the list.

The test program for numlist.cpp just adds a bunch of data and prints out the list.

Thanks for any suggestions you have. I'm out of ideas here.
Attached Files
File Type: txt design.txt (2.9 KB, 6 views)
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,161
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Concordance, Linked Lists and classes

 
0
  #2
Oct 15th, 2005
This is a c++ program -- why are you wrinting your own linked lists instead of using STL <list> classes?
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: Concordance, Linked Lists and classes

 
0
  #3
Oct 15th, 2005
With the exception of string functions, use of the STL is not allowed in that class.
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: Concordance, Linked Lists and classes

 
0
  #4
Oct 17th, 2005
I was not able to get this to work properly before the due date. For some reason the program sets the pointer to the linked list of numbers to null if I call the insert procedure with anything other than what I had called it before.

This works:
insert("Hey", 3)
insert("Hey", 4)

Output:
Hey 3 4

This doesn't:
insert("Hey", 3)
insert("Hi", 4)

Hey [null]
Hi 4
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC