5 Replies - 1661 Views - Last Post: 30 October 2012 - 12:50 PM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

An example of a Linked list program with multiple fields?

Posted 29 October 2012 - 12:53 PM

Unfortunately all of the examples I have come across, only demonstrate using 1 field of data, that is usually an integer.

In line 14, of the following header, it uses only 1 field, which is a double, named value.
What if I was working with a record of multiple peoples?
A field of a person would be: int age, string name, double height.

Header
// Specification file for the NumberList class 
#ifndef NUMBERLIST_H 
#define NUMBERLIST_H
//#include <cstdlib> 
#include <iostream>		// for NULL
using namespace std;

class NumberList 
{ 
private: 
	// Declare a structure for the list 
	struct ListNode 
	{ 
		double value;			// The value in this node 
		struct ListNode *next;	// To point to the next node 
	}; 

	ListNode *head;				// List head pointer

public: 
	// Constructor: notice the constructor initializes the head pointer to NULL (0)
	// This establishes an empty linked list. 
	NumberList() 
	{	head = NULL;	} 

	// Destructor: destroys  the list by deleting all its nodes. 
	~NumberList();		//Is declared in NumberList.cpp
	//{}

	// Linked list operations 
	// These functions are defined in NumberList.cpp.
	void appendNode(double); 
	void insertNode(double); 
	void deleteNode(double); 
	void displayList() const; 
}; 
#endif 




A program that demonstrates using multiple fields for a linked list would be appreciated. I had no luck with searching on google.

Is This A Good Question/Topic? 0
  • +

Replies To: An example of a Linked list program with multiple fields?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: An example of a Linked list program with multiple fields?

Posted 29 October 2012 - 01:01 PM

Quote

A field of a person would be: int age, string name, double height.

So why don't you think about using a User Defined Type (more commonly called a structure or class) as the data type?

Jim

This post has been edited by jimblumberg: 29 October 2012 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: An example of a Linked list program with multiple fields?

Posted 29 October 2012 - 01:11 PM

Yes, I was thinking about that.

The structure of the multiple fields would be in int main, but what of line 14 in the original post?

This post has been edited by mgrex: 29 October 2012 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: An example of a Linked list program with multiple fields?

Posted 29 October 2012 - 01:30 PM

You can include the header of your own class in which case you can use "yourClass value;" instead of "double value;". You can also use a template and define the type later when you instantiate the class in main.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: An example of a Linked list program with multiple fields?

Posted 29 October 2012 - 01:31 PM

Quote

The structure of the multiple fields would be in int main

Why just in main()? Remember this is a data type, just like an int is a data type.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: An example of a Linked list program with multiple fields?

Posted 30 October 2012 - 12:50 PM

I had some luck by using a template vector.

Now I have logical error. It won't display the linked list's content in order. The statement on line 43 in the int main, month April), is always displayed before the initializations coming before it in lines 23-29.

#include <iostream>
#include <string>

#include "Listing.h" 
#include "Rainfall_stats.h" 
using namespace std;

int main()
{
	Listing<Rainfall_stats>list;

	string monthNames[] = {"January","Febuary","March","April","May","June","July","August","September","October","November", "December"};
	int numMonths;
	double rainAmount;

	cout << "How many months do you want to enter data for (1 thorugh 12): ";
	cin >> numMonths;

	
	Rainfall_stats * temp = new Rainfall_stats[numMonths];

	int i = 0;
	for(i = 0; i < numMonths; i++)
	{
		cout << "Rainfall amount for " << monthNames[i] << ": ";
		cin >> rainAmount;

		temp[i] = Rainfall_stats(monthNames[i], rainAmount);
		list.appendNode(temp[i]);
	}

	cout << "i amount: " << i;

	// Display the values in the list. 
	cout << "\nHere are the initial values:\n";
	list.displayList(); 
	cout << endl;

	cout << "Adding another month's data...";
	Rainfall_stats temp2(monthNames[i], 3);	// I had to overload > and < relational operators
	list.insertNode(temp2);						// logical error, line 40 isn't placed last, in line 49's output.
	
	Rainfall_stats temp3(monthNames[i+1], 2);	// I had to overload > and < relational operators
	list.insertNode(temp3);	

	Rainfall_stats temp4(monthNames[i+2], 4);	// I had to overload > and < relational operators
	list.insertNode(temp4);	

	cout << "\nFull list again:\n";
	list.displayList(); 
	cout << endl;

	delete [] temp;

	system("pause");
	return 0;
}



Rainfall_stats.h
// Specification file for the NumberList class 
#ifndef Rainfall_stats_H 
#define Rainfall_stats_H
//#include <cstdlib> 
#include <iostream>		// for NULL
#include <string>
using namespace std;

class Rainfall_stats 
{ 
private: 
	string month;
	double amount;

public: 
	// Constructor: notice the constructor initializes the head pointer to NULL (0)
	// This establishes an empty linked list. 
	Rainfall_stats() 
	{	month = "January";	
		amount = 0;			} 

	// Destructor: destroys  the list by deleting all its nodes. 
	Rainfall_stats(string m, double a)
	{	
		month = m;
		amount = a;
	}

	// Linked list operations 
	// These functions are defined in NumberList.cpp.
	void setMonth(string m)
	{
		month = m;
	}

	void setAmount(double a)
	{
		amount = a;
	}

	string getMonth() const
	{
		return month;
	}

	double getAmount() const
	{
		return amount;
	}

	bool operator > (const Rainfall_stats &right)	// Overloaded	> 
	{  
		bool status; 

		if (month > right.month) 
			status = true; 
		//else if (month == right.month && amount > right.amount) 
		//	status = true;
		else 
			status = false;
		
		return status; 
	}
	
		
	bool operator < (const Rainfall_stats &right)	// Overloaded < 
	{  
		bool status; 

		if (month < right.month) 
			status = true; 
		//else if (month == right.month && amount < right.amount) 
		//	status = true;
		else 
			status = false;
		
		return status; 
	}


	friend ostream &operator << (ostream &strm, const Rainfall_stats &obj) 
	{ 
		strm << "Month " << obj.month << ", had " << obj.amount << " inches of rainfall"; 
		return strm; 
	}
}; 
#endif 



Listing.h
// A class template for holding a linked list. 
//The limitation of the NumberList class is that it can only hold double values.

#ifndef LISTING_H 
#define LISTING_H 
#include <iostream> 	// For cout and NULL 
using namespace std; 

template <class T> 
class Listing 
{ 
private: 
	// Declare a structure for the list. 
	struct ListNode 
	{ 
		T value;					// The value in this node
		struct ListNode *next;		// To point to the next node 
	}; 

	ListNode *head; // List head pointer 

public: 
	// Constructor : Renamed from NumberList to LinkedList
	Listing() 
		{	head = NULL;	} 

	// Destructor : Renamed from NumberList to LinkedList
	~Listing();
	//{;}

	// Linked list operations 
	void appendNode(T); 
	void insertNode(T); 
	void deleteNode(T);
	void displayList() const; 
};

// Note that the template uses the ==, !=, and < relational operators to compare node values, 
// and it uses the << (line 93) operator with cout to display node values. Any type passed to  
// the template must support these operators. 


//********************************************************
// appendNode appends a node containing the value 
// passed into newValue, to the end of the list. 
//******************************************************** 
template <class T> 
void Listing<T>::appendNode(T newValue) 
{ 
	ListNode *newNode;		// To point to a new node 
	ListNode *nodePtr;		// To move through the list 


	// Allocate a new node and store nurn there. 
	newNode = new ListNode; 
	newNode->value = newValue;
	newNode->next = NULL; 

	// If there are no nodes in the list make newNode the 1st node. 
	if(!head) 
		head = newNode; 
	// Otherwise, insert newNode at end.
	else 
	{ 
		// Initialize nodePtr to head of list. 
		nodePtr = head; 
		
		// Find the last node in the list. 
		while (nodePtr->next) 
			nodePtr = nodePtr->next; 
		
		// Insert newNode as the last node. 
		nodePtr->next = newNode; 
	}
}

//************************************************** 
// displayList shows the value 
// stored in each node of the linked list 
// pointed to by head. 
//************************************************** 
template <class T> 
void Listing<T>::displayList() const
{ 
	// To move through the list
	ListNode *nodePtr;
	
	// Position nodePtr at the head of the list. 
	nodePtr = head;		

	// While nodeptr points to a node, traverse the list. 
	while (nodePtr)		
	{ 
		// Display the value in this node. 
		cout << nodePtr->value << endl; 

		// Move to the next node. 
		nodePtr = nodePtr->next; 
	}
}

//*****************************************************
// The insertNode function inserts a node with 
// newValue copied to its value member. 
//*****************************************************
template <class T> 
void Listing<T>::insertNode(T newValue) 
{
	ListNode *newNode;				// A new node 
	ListNode *nodePtr;				// To traverse the list 
	ListNode *previousNode = NULL;	// The previous node 
	
	
	// Allocate a new node and store num there. 
	newNode = new ListNode; 
	newNode->value = newValue; 
	
	// If there are no nodes in the list make newNode the first node. 
	if(!head) 
	{ 
		head = newNode; 
		newNode->next = NULL; 
	} 
	// Otherwise, insert newNode. 
	else 
	{ 
		// position nodePtr at the head of list. 
		nodePtr = head; 

		// Initialize previousNode to NULL. 
		previousNode = NULL; 

		// Skip all nodes whose value is less than num. 
		while (nodePtr != NULL && nodePtr->value < newValue) 
		{ 
			previousNode = nodePtr; 
			nodePtr = nodePtr->next; 
		} 

		// If the new node is to be the 1st in the list, 
		// insert it before all other nodes. 
		if (previousNode == NULL) 
		{
			head = newNode; 
			newNode->next = nodePtr; 
		}

		else // Otherwise insert after the previous node. 
		{ 
			previousNode->next = newNode; 
			newNode->next = nodePtr; 
		} 
	} 
} 

//******************************************************** 
// The deleteNode function searches for a node  
// with searchValue as its value. The node, if found, * 
// is deleted from the l ist and from memory. * 
//******************************************************** 
template <class T> 
void Listing<T>::deleteNode(T searchValue) 
{ 
	ListNode *nodePtr;			// To traverse the list 
	ListNode *previousNode;		// To point to the previous node

	// If the list is empty, do nothing. 
	if (!head) 
		return; 
	// Determine if the first node is the one. 
	if (head->value == searchValue) 
	{ 
		nodePtr = head->next; 
		delete head; 
		head = nodePtr; 
	} 
	else 
	{ 
		// Initialize nodePtr to head of list. 
		nodePtr = head; 

		// Skip a11 nodes whose value member is not equal to num.
		while (nodePtr != NULL && nodePtr->value != searchValue) 
		{ 
			previousNode = nodePtr; 
			nodePtr = nodePtr->next;
		} 

		// If nodePtr is not at the end of the list, link the prevLous
		// node to the node after nodePtr, then delete nodePtr.
		if (nodePtr) 
		{
			previousNode->next = nodePtr->next; 
			delete nodePtr;
		}
	} 
} 


//******************************************************** 
// Destructor 
// This function deletes every node in the list.
//******************************************************** 
template <class T> 
Listing<T>::~Listing() 
{ 
	ListNode *nodePtr;		// To traverse the list 
	ListNode *nextNode;		// To point to the next node 

	// position nodePtr at the head of the list. 
	nodePtr = head; 

	// While nodePtr is not at the end of the list... 
	while (nodePtr != NULL) 
	{ 
		// Save a pointer to the next node . 
		nextNode = nodePtr->next; 
		// Delete the current node. 
		delete nodePtr; 
		// position nodePtr at the next node . 
		nodePtr = nextNode; 
	} 
}
#endif 


This post has been edited by mgrex: 30 October 2012 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1