List

"Make a function, which print on the screen, the elements with maximum key value of a List with start pointer START." What does it mean? I have a example but i dont know what to do. ( also #include <process> gives an error)


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <process>
using namespace std;
// Definition and initialization if structure LIST
struct elem
{
	int key;
	elem* next;
} *start = NULL;
// Prototypes of all functions
void add_b(int n);
void add_bk(int n, int k);
void add_ak(int n, int k);
void add_e(int n);
void list();
int del_b(int& n);
int del_n(int k);
int del_e(int& n);
int search_iter(int n);
int main()
{
	int k, num, m;
	do
	{
		system("cls");
		cout << "\n MENU\n";
		cout << "1 - Add the first element\n";
		cout << "2 - Add the element before k\n";
		cout << "3 - Add the element after k\n";
		cout << "4 - Add the last element\n";
		cout << "5 - Del the first element\n";
		cout << "6 - Del the element with value k\n";
		cout << "7 - Del the last element\n";
		cout << "8 - Search element\n";
		cout << "9 - Show the list\n";
		cout << "10 - Exit";
		cout << "\n\nYour choice:";
		cin >> m;
		if ((m > 0) && (m < 5) || (m == 8))
		{
			cout << "\nInput num:";
			cin >> num;
		}
		if ((m == 2) || (m == 3) || (m == 6))
		{
			cout << "\nInput k:";
			cin >> k;
		}
		switch (m)
		{
		case (1): {add_b(num); list(); break; }
		case (2): {add_bk(num, k); list(); break; }
		case (3): {add_ak(num, k); list(); break; }
		case (4): {add_e(num); list(); break; }
		case (5): {del_b(num);
			cout << "\nYou delete " << num << '\n';
			list(); break; }
		case (6): {del_n(k); list(); break; }
		case (7): {del_e(num);
			cout << "\nYou delete :" << num << '\n';
			list(); break; }
		case (8):
		{
			if (search_iter(num))
				cout << "\n" << num << " is found!\n";
			else
				cout << "\n " << num << " is not found!\n";
			break;
		}
		case (9): { cout << "\n"; list(); break; }
		} //switch
	} while (m != 10);
} //main()
void add_b(int n)
{
	// write a function
}
void add_bk(int n, int k)
{
	// write a function
}
void add_ak(int n, int k)
{
	// write a function
}
void add_e(int n)
{
	// write a function
}
void list()
{
	// write a function
}
int del_b(int& n)
{
	// write a function
}
int del_n(int k)
{ // write a function
}
int del_e(int& n)
{
	// write a function
}
int search_iter(int n)
{
	// write a function} 
Hello kerem59,


( also #include <process> gives an error)


So, post the error message. I have no idea what it could be other thanyou compiler does not have the header file "<process>" or maybe you are using the wrong spelling or name?

Your code is in serious need of some blank lines to make it easier to read.

After I get you code loaded into my IDE I will know more and let you know.

Andy
Why have #include <process> ? The code as posted doesn't use any process functions/classes?

Before you get to use the list to get the maximum value etc, you first need to provide the code to add items to the list - as where in the above code it says 'write a function'.

I'd suggest you start with 1), 4) and 9), get them working correctly and then the other functions.
Hello kerem59,

I did find "<process.h>", but this is a C header file and you do not use anything from this header file in your code. When I put a comment on that line it compiled with no problem with VS2017. Also I am not sure where this header file is located, but it is not in with the standard C++ header files. This usually it is best not to unless you have to.

Your variables int k, num, m;. "k" and "m" may mean something to you, but they need a better name like "menuChoice" for "m". I have not figured out what "k" is for yet, but it should have a name that describes what it is used for.

Next question is do you have to create your own list or is this program to use "std::list" from the "list" header file?

Some small things:
You do not need a "cout" for every line of your menu. Using the "\n"s as you did is good.

You can change your menu to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << 
    "\n               MENU\n"
    " ---------------------------------\n"
    "  1 - Add the first element\n"
    "  2 - Add the element before k\n"
    "  3 - Add the element after k\n"
    "  4 - Add the last element\n"
    "  5 - Del the first element\n"
    "  6 - Del the element with value k\n"
    "  7 - Del the last element\n"
    "  8 - Search element\n"
    "  9 - Show the list\n"
    " 10 - Exit\n"  // <--- Changed. Added the "\n".
    "\n  Your choice: ";  // <--- Changed. Removed a "\n". Added a space after the (:).
cin >> menuChoice;

The really neat part about this is that each quoted string is considered just 1 big string.

In the switch you do not need the {} after "case 1:" You only need the {}s if you define a variable in a case. Then this variable is local to the {}s and is lost when you reach the closing brace.

I would suggest using:
1
2
3
4
5
6
7
8
switch (menuChoice)
{
    case (1):
        add_b(num); 
             
        list();

        break;

This layout until you get use to it. Leave the 1 liners for later when you are use to it. It really helps in the beginning.

Since you do not check what you enter for "menuChoice" the switch could use a "default" case to catch any number out of range of your choices.

To do your "cin" for "menuChoice" properly you should be checking for any non numeric input that would cause "cin" to fail. Because if it does fail it will be unusable the rest of the program. I will have to work up the code for this and show you in a bit.

Andy
Topic archived. No new replies allowed.