Linked List ..

No idea whats wrong with this code
can someone point on my mistake please

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
108
109
110
 #include<iostream>
using namespace std;
#include <list>
class queue {
private:
	list<int> a;
	queue();
	queue Init();
	bool isEmpty(queue);
	void push(queue, int);
	int pop(queue);
	int peek(queue);
	void printQu(queue);
	int popChild(queue);

public:

	queue() {
		a = new list<int>;
	}
	queue Init() {
		if (a != NULL)
			a = NULL;
		return new queue();
	}

	bool isEmpty(queue qu) {
		return(qu.a.isEmpty());
	}

	void push(queue qu, int x) {
		qu.a.addLast(x);

	}

	int pop(queue qu) {
		return (int)qu.a.pollFirst();
	}

	int peek(queue qu) {
		return (int)qu.a.peekFirst();
	}
	void printQu(queue qu) {
		cout << "[";
		for (int i = 0; i < qu.a.size(); i++) {
			if (i != qu.a.size() - 1)
				cout << (" " + qu.list.get(i) + " ->");
			else {
				cout << (qu.list.get(i));
			}
		}
		cout << "]";
		cout << endl;
	}
	//check if their is a child in the queue and print his id
	int popChild(queue qu) {

		for (int i = 0; i < qu.a.size(); i++) {
			if ((int)qu.a.get(i) > 999) {
				return qu.a.remove(i);
			}
		}
		return 0;
	}



	int main()
	{
		queue s = new queue();
		bool flag = true;
		int x = 0;

		while (flag) {
			cout << "Please choose ONE of the following: " << endl;
			cout << "1 - push ,2 - pop, 3 - popChild, 4 - print, 5 - exit" << endl;
			cin >> x;
			switch (x) {
			case 1:
				cout << "add id number: " << endl;
				int y;
				cin >> y;
				s.push(s, y);
				break;
			case 2:
				int patient;
				patient = s.pop(s);
				cout << "the patient id is: " + patient << endl;
				break;
			case 3:
				int childId;
				childId = s.popChild(s);
				cout << "first seek for a child " + childId << endl;
				break;
			case 4:
				s.printQu(s);
				break;
			case 5:
				cout << "Byebye" << endl;
				flag = false; // exit from program
			default:
				if (x > 6 || x < 0)
					cout << "Bad Number, Please Choose Again..." << endl;// all other values
				break;
			}
		}
		system("pause");
		return 0;

	}
Last edited on
Lines 7-14, 18-64: You're defining your member functions twice. Delete lines 7-14.

Lines 27-56: You're trying to pass a class instance to each function. Each function of a class refers to a class instance. It's not necessary to pass a class instance as an argument.

Line 19,70: new returns a pointer. a is not a pointer. This is not Java. Line 19 is not required since the list was declared at line 6. Line 70 should be simply:
 
  queue s;


Lines 21-25: Init() is not required. a is not a pointer. You can't compare it to NULL. Init() is never called.

Line 28: std::list (a) has no isEmpty() function. Use:
1
2
3
bool isEmpty() const
	{	return a.begin() == a.end();
	}


Line 32: std::list (a) has no addLast() function. Use:
1
2
3
void push(int x)
	{	a.push_back(x);
	}


Line 47,49: queue has no member called list.

Line 47,49,59: list has no member get().

Line 65: Your class declaration is missing a closing };

Line 100: case is missing a break statement.

Line 102: Why bother with an if statement. You already know it's not 1-5 or you wouldn't be here. BTW, your test is wrong. What if x is 0 or 6?

Lots more errors. Compile, fix the first error. Compile again. Fix the next error until you have corrected all the errors.
Last edited on
Topic archived. No new replies allowed.