Garbage value in linked list queue program

Hye! In the output, I am getting garbage value! I don't know where I am making a mistake. Can anyone find the mistake?

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
#include <iostream>
using namespace std;
struct node
{
	int data;
	int waitingtime;
	int turnouttime;
	struct node *next;
};
int x;
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert()
{
	int waitime;
	int turntime;
	int val;
	cout << "How many entries you want to make " << endl;
	cin >> x;
	for (int i = 0; i < x; i++)
	{
		cout << "Insert the element in queue : " << endl;
		cin >> val;
		if (rear == NULL)
		{
			rear = (struct node *)malloc(sizeof(struct node));
			rear->next = NULL;
			rear->data = val;
			cout << "Enter the avg waiting time " << endl;
			cin >> rear->waitingtime;
			front = rear;
		}
		else
		{
			temp = (struct node *)malloc(sizeof(struct node));
			rear->next = temp;
			temp->data = val;
			temp->next = NULL;
			cout << "Enter the avg waiting time " << endl;
			cin >> rear->waitingtime;
			rear = temp;
		}
	}
}

void Display() {
	temp = front;
	if ((front == NULL) && (rear == NULL)) {
		cout << "Queue is empty" << endl;
		return;
	}
	cout << "Queue elements are: ";
	while (temp != NULL) {
		cout << temp->data << " ";
		cout << temp->waitingtime;
		temp = temp->next;
	}
	cout << endl;
}
void calculateturn()
{
	int count=0;
	cout << "The average turn out time is " << endl;
		while (temp != NULL) 
		{
			cout << temp->data << " ";
			count+=temp->waitingtime;   // ADD THE AVG TIME
			temp = temp->next;
		}
		cout << count / x;
}
int main() {
	int ch;
	cout << "1) Insert element to queue" << endl;
	cout << "2 Calculate the avg waiting time and avg turn out time" << endl;
	cout << "4) Display all the elements of queue" << endl;
	cout << "5) Exit" << endl;
	do {
		cout << "Enter your choice : " << endl;
		cin >> ch;
		switch (ch) {
		case 1: Insert();
			break;
		case 2: Display();
			calculateturn();
			break;
		case 3: cout << "Exit" << endl;
			break;
		default: cout << "Invalid choice" << endl;
		}
	} while (ch != 4);
	system("pause");
}
Last edited on
c++ should use new and delete, not malloc and free. the exception is if you need to use realloc, which you do not here. This is not an error, just a mixed language issue (when there is no reason otherwise, use c++ over C code).

what are you doing to see an error, just insert and display?

calculate turn looks broken. what is temp in that function?!
Last edited on
Line 41: You set rear->waitingtime, but that's the old value of rear, not the new one. So you're setting the waitingtime on the previous node.

It would be better to have one section of code that reads the new record into temp, and another section that inserts temp into the list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        for (int i = 0; i < x; i++)
        {
            // Create and read the new record:
            temp = (struct node *)malloc(sizeof(struct node));
            cout << "Insert the element in queue : " << endl;
            cin >> temp->data;
            cout << "Enter the avg waiting time " << endl;
            cin >> temp->waitingtime;
            temp->next = nullptr;
            // Insert temp into the list
            if (rear == NULL) {
                front = temp;
            } else {
                rear->next = temp;
            }
            rear = temp;
    }


Lines 75-78: Your menu options don't match the values in the switch statement. Also, I suggest that you print the menu each time through the loop.

Topic archived. No new replies allowed.