This will help you visualize that a Doubly Linked List might on average cut your
ID: 3671059 • Letter: T
Question
This will help you visualize that a Doubly Linked List might on average cut your search time in half by sorting the data, then choosing which end to start the search.
When complete print out each word on a new line in a file called revsorted.txt. Start at the back of the list so you end up with a reverse sorted list (words starting with z's first).
For this assignment, you should have setup a class called dictionary that holds a word as type string, with a single private value, a default constructor and appropriate get/set functions. This way you could (in the future) expand the definition
Some Important tips (please read):
http://www.cplusplus.com/reference/list/list/ (Links to an external site.)
- Remember like a vector, you have a list of a particular type (dictionary in this case)
- A list<dictionary>::iterator is a pointer to a list containing dictionary items. Not every operator is defined, but ++ and -- are defined, so you can increment or decrement the pointer.
- There are member functions for a list .begin() which is pointing to the first item on the list and .end() which is the last pointer NULL. So, you generally want to loop from .begin() to != .end()
To start at the back (tail pointer) and work you way to the front you want to reverse the process. so instead of an iterator (which points to the head of the list), you want to use reverse_iterator. And instead of .begin() to != .end(), you want to go from .rbegin() to != .rend()
In order to use the .sort() function for lists, you must overload the operator <, as a friend function of the class. The .sort() function uses this operator to sort.
Remember if you are looking at the VALUE of what the pointer points to, you must dereference the pointer. If you are pointing to a class with a public member function called findValue, you would write that as xPtr->getWords(), or (*xPtr).getWords NOT xPtr.findValue.
code for dictionary.h:
Explanation / Answer
//
// File: dictionary.h
// Author: HARE KRISHNA
//
// Created on 24 February, 2016, 8:01 AM
//
#ifndef _DICTIONARY_H
#define _DICTIONARY_H
typedef string wordType
class dictionary
{
public:
//typedef string wordType
string name;
dictionary *next;
dictionary *previous;
dictionary()
wordType getWord()
void setWord(wordType _word)
friend bool operator <(const dictionary& first, const dictionary& second)
private:
wordType word
}
class list: private dictionary
{
dictionary *first;
public:
list();
void firstfile();
void display();
void secondfile();
};
list::list()
{
first = NULL;
}
void list::firstfile(){
dictionary *current;
dictionary *prev;
ifstream fin;
fin.open("D:/finwords.txt");
prev = NULL;
while(!fin.eof())
{
current=new item;
if (first == NULL) // we set first the first time
first = current;
fin>>current->name;
current->previous=prev;
current->next = NULL;
if (prev != NULL)
prev->next=current;
prev = current; // we store the current as the prev for next iteration
}
fin.close();
}
void list::secondfile(){
dictionary *current;
dictionary *prev;
ifstream fin;
fin.open("D:/revsorted.txt");
prev = NULL;
while(!fin.eof())
{
current=new item;
if (first == NULL) // we set first the first time
first = current;
fin>>current->name;
current->previous=prev;
current->next = NULL;
if (prev != NULL)
prev->next=current;
prev = current; // we store the current as the prev for next iteration
}
fin.close();
}
/*******************************/
int searchForward(list<dictionary> &wordList,wordType &findString)
int searchBackward(list<dictionary> &wordList,wordType &findString)
void revPrintList(ostream& output, list<dictionary> &wordList)
bool comp(dictionary& first, dictionary& second)
#endif /* _DICTIONARY_H */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.