Hi all,

So i'm creating a program that will read a file into a linked list. And the user will be able to add, remove and display the list. Im suppose to create a search function in order to find the link to remove or add or display.

Here is what i did so far:

Header file:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct NodeType
{
	long Id_Num;
	string Name;
	string Phone;
	char Status;
	float Income;
	NodeType* Next;		//Pointer to the next link
	NodeType(NodeType newelement, long id, string nam, string pho, char stats, float inc) :  Id_Num(id), Name(nam), 
		Phone(pho), Status(stats), Income(inc){	};

};

class ClubMember
{
private:
	NodeType* first;			//pointer to first link
public:
	ClubMember()
	{
		first = NULL;
	}

	void add(long Id, string name, string phone, char stats, float inc);
	void remove(long Id);
	void input();
	void display();
};

//clubmembership.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "clubmembership.h"
using namespace std;

NodeType search(long id);

void ClubMember::input()
{
	ifstream infile("member.dat");

	long id;
	string name;
	string phone;
	char stats;
	float income;

	while(!infile.eof())
	{
		infile >> id >> name >> phone >> stats >> income;
		add(id,name,phone,stats,income);
	}
	
}

/*void ClubMember::display()
{
	int idnum;
	cout << "Enter ID Number: ";
	cin >> idnum;
	cout << endl;
	NodeType result;


	result = search(idnum);
	if(result.Id_Num != 0000)
	{
		cout << "Id Number: " << result.Id_Num << endl;
		cout << "Name: " << result.Name << endl;
		cout << "Phone: " << result.Phone << endl;
		cout << "Status: " << result.Status << endl;
		cout << "Income: " << result.Income << endl;
		cout << endl;
	} 
	else 
	{
		cout << "ID Number not found. Please make another selection." << endl << endl;
	}
}//*/

void ClubMember::add(long Id, string name, string phone, char stats, float inc)
{
	NodeType* temp;

	NodeType* newItem = new NodeType(Id, name, phone,stats, inc);

	temp = newItem;
	temp->Next = first;
	first = temp;
}

void ClubMember::remove(long Id)
{
	if(first->Id_Num == Id)
	{
		first = first->Next;
		first->Next = NULL;
	}

	NodeType *current = first;
	NodeType *previous;

		while(current->Next != NULL)
		{
			previous = current;
			current = current->Next;

			if(current->Id_Num==Id)
			{
				previous->Next = current->Next;
				current->Next = NULL;
			}
		}
}


NodeType search(long id)
{
	NodeType *temp;
	NodeType invalid;

	invalid.Id_Num = 0000;
	invalid.Name = "Invalid";
	invalid.Phone = "000-0000";
	invalid.Status = 0;
	invalid.Income = 0000000;

	while(temp != NULL)
	{
		if(temp->Id_Num == id)
		{
			return *temp;
		}
		else
		{
			temp = temp->Next;
		}
	}
return invalid;
}

// main.cpp

#include <iostream>
#include "clubmembership.h"

using namespace std;


int main()
{

	ClubMember zoop;

	zoop.input();
	zoop.display();
	zoop.add(123,"maws","mom",'m',1238);
	zoop.display();
	zoop.remove(123);
	zoop.display();

	return 0;
}

So the compiler is giving me this error:
: error C2512: 'NodeType' : no appropriate default constructor available


Thanks for the help,

Doug

Nevermind i got it

you have declared your NodeType constructor like this:

NodeType(NodeType newelement, long id, string nam, string pho, char stats, float inc)

so you need to make sure that each time you 'new' your NodeType, you pass in the correct arguments to the constructor. i can see at least once where you dont do this:

void ClubMember::add(long Id, string name, string phone, char stats, float inc)
{
	NodeType* temp;

	NodeType* newItem = new NodeType(Id, name, phone,stats, inc);

	temp = newItem;
	temp->Next = first;
	first = temp;
}

in the example above you do not pass in the first 'NodeType newelement' argument.

Cool, thanks for the notice.

I'm stuck in my search function now, here is what i did so far:

NodeType search(long id)
{
	NodeType *temp;
	NodeType invalid;
	NodeType start_ptr;

	invalid.Id_Num = 0000;
	invalid.Name = "Invalid";
	invalid.Phone = "000-0000";
	invalid.Status = 0;
	invalid.Income = 0000000;

	temp = start_ptr;

	while(temp != NULL)
	{
		if(temp->Id_Num == id)
		{
			return *temp;
		}
		else
		{
			temp = temp->Next;
		}
	}
return invalid;
}

Compiler error:

error C2440: '=' : cannot convert from 'NodeType' to 'NodeType *'

Tnanks for the help

temp = start_ptr;

temp is a pointer.
start_ptr is not.

do

temp = &start_ptr;

this will get rid of the compiler error.

although, im not too sure why you are doing this. what is the purpoise of start_ptr and why do you want to set temp to point at it ?

So i got it figure out, but now i'm having problems with my remove function.
It compiles and everything, but it crashes when i try to display the remove function

void ClubMember::remove(long Id)
{
	NodeType* temp;
	NodeType result;
	
	result = search(Id);
	temp = &result;

	while(temp != NULL)
	{

		if(temp->Id_Num==0)
		{
			cout << "Cannot find that number. Try Again" << endl << endl;
		}
		if(temp->Id_Num == first->Id_Num)
		{
			first = first->Next;
			delete temp;
		}
	}

}

thanks for the help

help help help ;)

Anyone there?

My, my, impatient little poster aren't we.

If you are going to allow removal of a random node from within a list there are several protocols I've seen. In the first scenario you need to know the node to remove, and, if the node to remove isn't the first node in the list, the node pointing to the node to remove. Then you can assign nodeToRemove->next to nodePointingToNodeToRemove->next, change nodeToRemove->next to Null, and then delete nodeToRemove.

I've also seen some people keep track of just one node and use a convention similar to nodePointingToNodeToRemove->next for for the node to remove. In this scenario the syntax then becomes find the node nodePointingToNodeToRemove->next->ID == desired value, assign nodePointingToNodeToRemove->next to temp, then assign nodePointingToNodeToRemove->next->next to nodePointing ToRemove->next and then delete temp, or something like that.

You can try one approach or the other, perfect it, and adapt it as necessary for your code.

Good luck.

here's my advice. when you create pointers, always always always set them to NULL. same goes for when you delete them. in your 'replace' function, you go into a 'while temp is not NULL' loop, then half way through you delete temp, but do not set it to null. the while loop will still continue looping because temp is not null. but it HAS been deleted ! hence the crash.

anyway thats just my assumption on what is happening, and one possible reason why it could be crashing.

another bit of advice, when you have a crash, try and post which line of code it crashes on. people are far more likely to respond if you do this because they will be able to see what is happening without guessing (which is what i have done here !)

and i agree with Lerner too. if you google for c++ link lists, you should be able to get some code that already does what you want, crash free. you will be able to tweak it to suit your needs.

good luck ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.