0
void menu()
{
	int userinput;
	static value *head = NULL;
	static value *tail = NULL;

	cout << "1 - Insert value" << endl;
	cout << "2 - Print list" << endl;
	cout << "3 - Delete value" << endl;
	cout << "4 - Quit" << endl;
	cout << "5 - Error checking" << endl << endl;
	cout << "Your Choice : ";
	cin >> userinput;

	if(userinput == 1)
	{
		insertvalue(head, tail);
	}
}

void insertvalue(value *head, value *tail)
{
	int newv;

	value *newvalue = new value;

	system("cls");
	cout << "Insert a value for linked list: ";
	cin >> newv;

	newvalue->v = newv;
	newvalue->link = NULL;

	if(head == NULL)
	{
		head = tail = newvalue;
	}

	else
	{
		tail->link = newvalue;
		tail = newvalue;
	}

	cout << endl << "Returning to menu" << endl;
	cout << head->v << endl;
	system("pause");
	system("cls");
	menu();
}

It seems like my head is always reset back to NULL when i called back menu() in insertvalue() despite the static there.

so is it possible to let my head be the same value throughout the whole looping?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by abhi_elementx
0

use a global count variable to keep track of the nodes u are inserting.
if count = 0, then only add a node as head...

Getting the idea?

0

Also, using the count var can tell u how many nodes u've added.
But make sure you decrease the count when u free the nodes.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.