| | |
Concordance, Linked Lists and classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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:
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
Exert from wordlist.cpp (I didn't include the private functions such as touppercase and is_same--they work.)
numlist.h
numlist.cpp
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.
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
C++ Syntax (Toggle Plain Text)
#ifndef linkdlst_h #include <iostream> #include <string> /* HUGELY IMPORTANT NOTE: * * This class is dependant upon the other node class! It will fail without it! */ #include "numlist.h" using namespace std; class concordance { private: struct node { string data; numlist line_nums; node *next; }; node *list_head; // Methods bool is_before(string list_data, string data); bool is_same(string list_data, string data); bool is_after(string list_data, string data); //Note: is_first is implied--it's a one line thing string tolowercase(string str); public: concordance(); ~concordance(); void append(string data, int n); void insert(string data, int n); void print(); }; #define linkdlst_h #endif
Exert from wordlist.cpp (I didn't include the private functions such as touppercase and is_same--they work.)
C++ Syntax (Toggle Plain Text)
concordance::concordance() { list_head = NULL; } concordance::~concordance() { //The destructor may have to do something here. node *previous; node *current; previous = current = list_head; while (current != NULL) //Delete each item in the list. { current = previous -> next; previous -> line_nums.~numlist(); delete previous; previous = current; } previous = current = list_head = NULL; } void concordance::insert(string data, int n) { node* current = list_head; node* previous = list_head; node* newnode = NULL; bool done = false; // After the loop has executed, exit it. // Create a node to insert the data into newnode = new(node); newnode -> data = data; newnode -> line_nums.append(n); // Build the list by inserting data in an organized manner. if (list_head != NULL) { while (done == false) { if (is_before(current -> data, data)) { //Insert into the list newnode -> next = previous -> next; previous -> next= newnode; done = true; } else if (is_same(current -> data, data)) { // cout << "is_same "; //Append line number to line number list newnode -> line_nums.append(n); done = true; } else { //if !is_before and !is_same, it must be is_after. if (current -> next == NULL) { //The end of list has been reached. //Insert at the end newnode -> next = NULL; current -> next = newnode; done = true; } else { previous = current; current = current -> next; done = false; } } } } else { newnode -> next = NULL; list_head = newnode; } } void concordance::print() { node *current; current = list_head; while (current != NULL) { cout << current -> data << " <- data | position -> "; current -> line_nums.print(); cout << endl; current = current -> next; } }
numlist.h
C++ Syntax (Toggle Plain Text)
/* Source: Program 0, Assignment 7. */ #ifndef numlist_h #include <iostream> using namespace std; class numlist { private: struct node { int data; node *next; }; node *list; public: typedef node *node_ptr; numlist(); ~numlist(); bool defined; void append(int n); void print(); }; #define numlist_h #endif
numlist.cpp
C++ Syntax (Toggle Plain Text)
#include "numlist.h" numlist::numlist() { list = NULL; } numlist::~numlist() { //The destructor may have to do something here. node *previous; node *current; previous = current = list; while (current != NULL) //Delete each item in the list. { current = previous -> next; delete previous; previous = current; } previous = current = list = NULL; } void numlist::append(int str) { node *current; node *newnode; current = newnode = NULL; //Create a new node to put the data in. newnode = new(node); newnode -> data = str; newnode -> next = NULL; cout << " List Head: " << list << " Current: " << current << endl; //Decide where to put it. if (current == NULL) //Then this is an empty list. { list = newnode; } else //Find the end of the list { while (current -> next != NULL) { current = current -> next; } cout << " List Head: " << list << " Current: " << current << endl; current -> next = newnode; } // print(); } void numlist::print() { node *current; current = list; while (current != NULL) { cout << current -> data << " "; current = current -> next; } }
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.
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.
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.
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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.
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.
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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
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.
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.
![]() |
Similar Threads
- help with linked lists (C++)
- Singly-Linked Lists: Ultimate (C++)
- Bug when creating linked lists in Dev C++ (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C++ Forum
- Previous Thread: C++
- Next Thread: Why is destructor called twice?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






