Help with linked list

Hello,
So I'm working on an assignment and I'm stuck on a few things. I need to create a linked list to add employees and their data to. The first part is adding an employee with their data. I thought I had this and it's saying the variable isn't initialized. Here is the function:

1
2
3
4
5
6
7
  void  InsertBeginning(int n, string s, double d, Node* L)
{
	Node* TmpCell = new Node;
	TmpCell->Num = n;
	TmpCell->Dependents = d;
	L->Next = TmpCell;
}


Here is the input/output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int numberofEmployees = 1;
	string s;
	double b;

	do {
		cout << " \n What would you like to do? \n";
		cout << " 1. Add New Employee\n 2. Print all Employees and their data\n 3. Print Employees with 0 Dependents\n 4. Give employees that make less than $50000 a 10% raise\n 5. Add dependent to certain employee\n 6. Exit\n";
		cin >> choice;

		switch (choice)
		{
		case 1: cout << " \nCreating a new employee...\n";
			cout << "Enter employee name: ";
			cin >> s;
			cout << "Enter the number of dependents ";
			InsertBeginning(numberofEmployees, s, d, L);
			numberofEmployees++;
			break;


It's saying that the variable d isn't initialized.

The last problem I'm having is printing all the employees with 0 dependents. I know I would have to use my print function but I'm not sure how.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void  Print(Node* L)
{
	Node* P = L;                                         // P points to the empty header
	cout << "\n  List:  ";

	while (P->Next != NULL)
	{
		P = P->Next;                                 // move one node forward
		cout << P->Num << "  " << P->Name << "  " << P->Dependents << endl;                  // print out only the element part
	}  // stop when there is no next node

	cout << endl;
}


Thanks for your help

Last edited on
it's saying the variable d isn't initialized.

I don't see where you initially define d in your input/output snippet of code.
You need to make sure you've given d a proper value before calling line 16 (InsertBeginning).

printing all the employees with 0 dependents.
1
2
if (p->Dependents == 0)
    cout << P->Num << "  " << P->Name << "  " << P->Dependents << endl;

Something like that?

Could be more creative and do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void  Print(Node* L, bool only_print_zero_dep)
{
	Node* P = L;                                         // P points to the empty header
	cout << "\n  List:  ";

	while (P->Next != NULL)
	{
		P = P->Next;                                 // move one node forward
		if (only_print_zero_dep && P->Dependents != 0)
			continue;

		cout << P->Num << "  " << P->Name << "  " << P->Dependents << endl;                  // print out only the element part
	}  // stop when there is no next node

	cout << endl;
}

Also should be safe and do check to make sure L itself isn't null.
Last edited on
Look at InsertBeginning(). This inserts after the node L. So that isn't the beginning.

Also, it doesn't insert, so much as replace: whatever L->next contained before you called InsertBeginning() gets unlinked from the list.
Topic archived. No new replies allowed.