User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,589 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,591 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1679 | Replies: 4
Reply
Join Date: Nov 2007
Posts: 22
Reputation: d0ugg is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
d0ugg d0ugg is offline Offline
Newbie Poster

Help Linked List Problem

  #1  
Nov 4th, 2007
Hello everyone,

I need to load a multi-word strings from a file to a linked list in one of my programs, and i'm not sure how to do that.

my text file looks like this- (the name of the text file is: names.txt)

Linda
Martinho
Marla
Jose
Mary
Luka
Joseph
Thiago
Ruba

here is some of my code so far...


#include <iostream>
#include <cassert>
#include <String>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

template <class TYPE>
struct NODE
{
	TYPE data;											//Data Item
	NODE<TYPE> *next;									
};

template<class TYPE>
class multiword
{
protected:
	int count;											//Variable to store the values of the numbers
	NODE<TYPE> *first;									//pointer to the first NODE
public:	
	class NotFound {};
	void Initialize();									//Initialize the list with no values			
	bool Empty();										//Check if the list is empty
	void Destroy();
	void fillNames(ifstream& inFile, multiword<TYPE>& cList);		//Delete every value of the list
	int binarysearch(const TYPE& item);					//Binary search		
	void additem(const TYPE& add);						//Add a value
	void Insertion();									//Insertion
	void display();										//Display
	multiword();										//Constructor(no-arg)
	~multiword();										//Destructor
};

template<class TYPE>
void multiword<TYPE>::fillNames(ifstream& inFile, multiword<TYPE>& cList)
{
	string firstN;
	int i;

	TYPE temp;
	const int number = 10

	for(i = 0; i < number; i++)
	{
		inFile>>firstN;
		temp.setName(firstN);
		cList.insertAt(i, temp);
	}
}
	
template<class TYPE>									//Check if the list is empty
bool multiword<TYPE>::Empty()
{
	return(first == NULL);
}

template<class TYPE>									//Constructor
multiword<TYPE>::multiword()
{
	first = NULL;
	count = 0;
}

template<class TYPE>									//Destructor
multiword<TYPE>::~multiword()
{
	Destroy();
}
	
template<class TYPE>
void multiword<TYPE>::Insertion()
{
	nodeType<TYPE> *lastInOrder;
	nodeType<TYPE> *firstOutOfOrder;
	nodeType<TYPE> *current;
	nodeType<TYPE> *trailCurrent;

	lastInOrder = first;

	if(first = NULL)
		cerr << "Cannot sort an empty list." << endl;
	else
		if(first->link == NULL)
			cout << "The list of length1, it is already in order." << endl;
		else
			while(lastInOrder->link !=NULL)
			{
				firstOutOfOrder = lastInOrder->link;
				if(firstOutOfOrder->info < first->info)
				{
					lastInOrder->link = firstOutOfOrder->link;
					firstOutOfOrder->link = first;
					first = firstOutOfOrder;
				}
				else
				{
					trailCurrent = first;
					current = first->link;
					while(current->info < firstOutOfOrder->info)
					{
						trailCurrent = current;
						current = current ->link;
					}

					if(current != firstOutOfOrder)
					{
						lastInOrder->link = firstOutOfOrder->link;
						firstOutOfOrder->link = current;
						trailCurrent->link = firstOufOfOrder;
					}
					else
						lastInOrder = lastInOrder->link
				}
			}
}
 

template<class TYPE>
int multiword<TYPE>::binarysearch(const TYPE& item)
{
	int first = 0;
	int last = length -1;
	int mid;

	bool found = false;

	while(first <= last && !found)
	{
		mid = (first + last) / 2;

		if(list[mid] == item)
			found = true;
		else
			if(list[mid] > item)
				last = mid -1;
			else 
				first = mid + 1;
	}
	if(found)
		return mid;
	else 
		return -1;
}

template<class TYPE>									//Destroy every element on the list
void multiword<TYPE>::Destroy()
{
	NODE<TYPE> *temp;									//deallocates memory
	while(first != NULL)								//while there are value in the list
	{
		temp = first;									//set temp to the first value in the list
		first = first->next;							//put first to the next value
		delete temp;									//delete memory
	}
	count = 0;
}

template<class TYPE>									//Initialize the list
void multiword<TYPE>::Initialize()
{
	Destroy();											//If any values are found, remove them.
}

Thank you very much in advance,

Doug
Last edited by d0ugg : Nov 4th, 2007 at 6:42 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Linked List Problem

  #2  
Nov 4th, 2007
You need to be just a little more careful with names
Insertion() is a bad name because it is not obvious that it sorts the list. InsertionSort() is a good name.

Destroy() is not a good name because it doesn't destroy your object; it only deletes every item in the list. Name it something like Clear() or MakeEmpty(). By the way, Initialize() has the same logical functionality: both clear the list if not already emtpy. I would get rid of Initialize() and just use Clear() or whatever you name it. Actual initialization already occurs properly in the constructor.

Be careful with your types
In general, you should not assume that TYPE has a member function setName(). If you are just making a linked list of strings, there is no real reason to create a new class just to hold it. Just use std::string. Also, in template declarations, use the word typename instead of class. (Yes, yes, I know... semantics. Even so...)

More on fillNames()
You are trying to do two things here: initialize *this from file and initialize some that (which you named cList). If you need two copies of the multiword list, make a copy constructor and copy it that way; Don't force your user to fill two multiword lists just to load from file.

Also, you are using a member function named insertAt() which you have not defined to exist in the multiword class.

I recommend that you get rid of the cList argument and just load words from file into *this's list.

Lastly, you should not hardcode the number of lines in your file. What if you later want to load a list of names 100 names long? Or two? Just read names and append them to the end of the list as long as there are lines in the file.


Oy, that's enough for now. I haven't looked over your sort or search algorithms... but you seem to have a pretty good start. Work on straightening up those things I mentioned and then come back with compiler errors or what is not working right when you use the class.

Good luck.
Last edited by Duoas : Nov 4th, 2007 at 9:13 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 22
Reputation: d0ugg is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
d0ugg d0ugg is offline Offline
Newbie Poster

Re: Linked List Problem

  #3  
Nov 4th, 2007
Hey,
Thank you for the help...
I will work on those ideas that you gave me.
Doug
Reply With Quote  
Join Date: Nov 2007
Posts: 22
Reputation: d0ugg is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
d0ugg d0ugg is offline Offline
Newbie Poster

Re: Linked List Problem

  #4  
Nov 4th, 2007
Hey,

So I changed a couple things that you told me already, but I have no idea how to do what you told me on the fillname();

Right now it looks like this:
template<typename TYPE>
void multiword<TYPE>::fillNames(ifstream& inFile, multiword<TYPE>& cList)
{
	string firstN;
	int i;

	TYPE temp;
	const int number = 10

	for(i = 0; i < number; i++)
	{
		inFile>>firstN;
		temp.setName(firstN);
		cList.insertAt(i, temp);
	}
}

I'm not sure why I added setName and insertAt. Is there any other way to add the text file without calling other classes such as setname and insertat?

Thank you,

Doug
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Linked List Problem

  #5  
Nov 4th, 2007
Think about the types of things you have.

You have a NODE class, which is used by the multiword class to store a list of some unspecified TYPE.

The fillNames() function shouldn't know or care what type of thing TYPE is. Just read it from file and store it.

Give it some thought...


Now, when it is time to use your linked list class, what type of thing are you storing in a list? Does it matter if the multiword class has any idea what type that is? Should I be able to say:
multiword<int> my_int_list;
or:
multiword<bool> my_bool_list;
or:
multiword<Employee> my_employee_list; (where Employee is some class defined elsewhere with the >> and << I/O operators properly overloaded)

Think about it.
Last edited by Duoas : Nov 4th, 2007 at 11:57 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:40 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC