Help with Coding

Hi, I'm new to this forum, and I was wondering if anyone can help me with some code that I'm working on. This is what I'm trying to do:

1) Develop a linked list class.

2) In the application file, do the following:

2a) Display a menu like this:

*********************************

* 1. Add a number *

* 2. Delete a number *

* 3. Display all the numbers *

* 4. Exit *

*********************************

enter your choice (1-4):

2b) Based on the user input, do the following:

1) add a number: ask the user to give a number and add it to the list. Keep the list in order from the smallest number to the largest.

2) delete a number: the user need to provide a number. Search the list. If the number is found, display "A x has been removed from the list". Otherwise, display "x is not in the list." In either case, x is the number the user entered.

3) display all the numbers -- display all the numbers in the list and use a " - " to separate the numbers.

4) end the program.

2c) Except option 4, all the other options, after it is done, should go back to display the menu and wait for the user's next command.

Here is my code so far:
Header file called mynode.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef MYNODE
#define MYNODE
namespace linkedlistspace
{
	class node
	{
	public:
		node() { data_field = 0;	link_field = NULL;}
		node(int vdata, node* vlink)
		{
			data_field = vdata;
			link_field = vlink;
		}
		void setdata(int v)	{data_field = v;}
		void setlink(node* vlink) {	link_field = vlink;	}
		int data() 		{	return data_field; }
		node* link()  {	return link_field;}
	private:
		int data_field;
		node *link_field;
	};

	void list_head_insert(node* headpointer, int vdata)
	{
		headpointer = new node(vdata, headpointer);
	}


}
#endif 


Source file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "mynode.h"
using namespace linkedlistspace;

int menu()
{
	cout << "***************************"<< endl;
	cout << "*  1. Add a number        *"<< endl;
	cout << "*  2. Delete a number     *"<< endl;
	cout << "*  3. Display all numbers *"<< endl;
	cout << "*  4. Exit                *"<< endl;
	cout << "***************************"<<endl;
	cout << "Enter your choice (1-4):" ;
	int c;
	cin >> c;
	return c;
}

void addnumber( node* headptr)
{
	int value;
	cout << "Enter the number:";
	cin >> value;
	list_head_insert( headptr, value);
}

void deletenumber( node* headptr)
{
	int value;
	cout << "Enter the number:";
	cin >> value;
	list_head_insert( headptr, value);
}

void displaynumbers( node* headptr)
{
	int value;
	cout << "Display all numbers:";
	cin >> value;
	list_head_insert( headptr, value);
}
int _tmain(int argc, _TCHAR* argv[])
{


	node* head = NULL;
	int choice = 0;
	while (choice != 4)
	{
		choice = menu();
		switch(choice)
		{	case 1:
				addnumber(head);
				break;
			case 2:
				deletenumber(head);
				break;
			case 3:
				displaynumbers(head);
				break;
			case 4:
				system("pause");
				return 0;
		}
	}

	system("pause");
	return 0;
}


I know this code is incomplete and most likely contains errors. Could someone please help me fix the errors and get the code to do what it's supposed to do?

Also, I'm asking this because I haven't done C++ in a while, and I'm trying to refresh my memory; so forgive me if I sound like a noob. Thanks.
Hello.

I didn't run your code but it looks like you need some code to connect the nodes when you add new node with given item(value). I think you should add connection part into the function "list_head_insert".
I think it will make more sense if you create two classes, a node class (which you have) and a separate list class. The list class contains a pointer to a node (the head of the list) and the various methods to add/delete items.
Topic archived. No new replies allowed.