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?

Recommended Answers

All 2 Replies

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?

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.

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.