i am trying to make a Write a C++ program that has linked list the code i am using is given below but its showing some errors

#include<bits/stdc++.h>
using namespace std;
struct customer{
unsigned int sequence_number;
int month,day,year,hour,minute;
string name,service_required;
struct customer *next;
};
class task{

public:
struct customer *head,*last;

task()
{
head=last=NULL;
}
~task()
{
struct customer *start,*temp;
start=temp=head;
while(start!=NULL)
{
temp=start;
start=start->next;
free(temp);
}
}
void insert_node(string name,int day,int month,int year,int hour,int minute)
{ static int counter=0;
struct customer *node=NULL;

node = new struct customer();
node->name=name;
node->day=day;
node->month=month;
node->year=year;
node->hour=hour;
node->minute=minute;
node->sequence_number=counter++;
if(head==NULL && last==NULL)
{
head=last=node;
node->next=NULL;
}
else
{
node->next=head;
head=node;
}

}
int delete_node(string name)
{
struct customer *deleted,*start=head;
deleted=search(name,head);

if(deleted==NULL)
{
return 1;
}
else
{// cout<<deleted->name<<endl;
if(deleted!=head)
{
while(start->next!=deleted)
{
start=start->next;
}

start->next=deleted->next;
free(deleted);
}
else
{
head=deleted->next;
free(deleted);
}

return 0;
}
}

struct customer search(string name,struct customer start)
{
if(start==NULL)
return NULL;
else
{
if(name.compare(start->name)==0)
{
return start;
}
else
{
return search(name,start->next);

}
}
}

struct customer * serve_customer()
{
struct customer *start;
start=head;
while(start->next != last)
{
start=start->next;
}
free(start->next);
start->next = NULL;
return start;
}

void list_all(struct customer *start)
{
if(start==NULL)
return ;
list_all(start->next);
cout<<start->sequence_number<<" "<<start->name<<" "<<start->day<<"//"<<start->month<<" "<<start->year<<"//"<<start->hour<<"::"<<start->minute<<endl;
}


};
int main()
{
int choice,temp;
unsigned int sequence_number;
int month,day,year,hour,minute;
string name,service_required;
task object1;

while(1)
{ cout<<endl;
cout<<"Enter the choice\n";
cout<<"1) New customer arrival \n2) Serve Customer\n3) Customer left remove from waiting list\n4) List all customers waiting for service\n5) To exit";
cin>>choice;

switch(choice)
{
case 1:
cout<<"enter name\n";
cin>>name;
cout<<"Enter date as day,month,year\n";
cin>>day>>month>>year;
cout<<"Enter time as hour and minute\n";
cin>>hour>>minute;
object1.insert_node(name,day,month,year,hour,minute);
break;
case 2:
object1.serve_customer();
break;
case 3:
cout<<"enter the name of customer\n";
cin>>name;
temp=object1.delete_node(name);
(temp==1)?cout<<"Already Served":cout<<"Deleted";
break;
case 4:
object1.list_all(object1.head);
break;
case 5:
return 0;
break;

}
}
}
The most obvious issue I see is that you are calling free() on objects you allocate by new. You're mixing C and C++ memory management, and it will end in your program exploding.

Call delete on what you new.

Also, use code formatting by adding [code] and [/code] around your code.
Last edited on
@Ganado i am new user kindly will you please do little modifications in this program so i can run it smoothly
Here is your code, indented and with Ganado's suggested fix (basically I just replaced "free" with "delete" globally).

I have added "dmh" to the code that I changed. I also pointed out some bugs. The main problem is in serve_customer().
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<bits/stdc++.h>
using namespace std;
struct customer {
    unsigned int sequence_number;
    int month, day, year, hour, minute;
    string name, service_required;
    struct customer *next;
};

class task {
public:
    struct customer *head, *last;

    task()
    {
	head = last = NULL;
    }
     ~task()
    {
	struct customer *start, *temp;
	start = temp = head;
	while (start != NULL) {
	    temp = start;
	    start = start->next;
	    delete(temp);	// dmh
	}
    }
    void insert_node(string name, int day, int month, int year, int hour, int minute)
    {
	static int counter = 0;
	struct customer *node = NULL;

	node = new struct customer ();
	node->name = name;
	node->day = day;
	node->month = month;
	node->year = year;
	node->hour = hour;
	node->minute = minute;
	node->sequence_number = counter++;
	if (head == NULL && last == NULL) {
	    head = last = node;
	    node->next = NULL;
	} else {
	    node->next = head;
	    head = node;
	}

    }
    int delete_node(string name)
    {
	struct customer *deleted, *start = head;
	deleted = search(name, head);

	if (deleted == NULL) {
	    return 1;
	} else {				 // cout<<deleted->name<<endl;
	    if (deleted != head) {
		while (start->next != deleted) {
		    start = start->next;
		}

		start->next = deleted->next;
		delete (deleted); // dmh
	    } else {
		head = deleted->next;
		delete (deleted); // dmh
	    }

	    return 0;
	}
    }

    // dmh: The "start" parameter is a pointer and it returns a pointer
    struct customer *search(string name, struct customer *start)
    {
	if (start == NULL)
	    return NULL;
	else {
	    if (name.compare(start->name) == 0) {
		return start;
	    } else {
		return search(name, start->next);
	    }
	}
    }

    struct customer *serve_customer()
    {
	struct customer *start;
	start = head;
	// dmh - it looks like you're trying to delete the last node.
	// There are three bugs here. First, it won't work if the list
	// is empty.  Second, it doesn't update last. Third, if it
	// deletes the last item, it needs to update head as well as
	// last.
	while (start->next != last) {
	    start = start->next;
	}
	delete (start->next);	// dmh
	start->next = NULL;
	return start;
    }

    void list_all(struct customer *start)
    {
	if (start == NULL)
	    return;
	list_all(start->next);
	cout << start->sequence_number << " " << start->name << " " << start->
	    day << "//" << start->month << " " << start->year << "//" << start->
	    hour << "::" << start->minute << endl;
    }

};

int
main()
{
    int choice, temp;
    unsigned int sequence_number;
    int month, day, year, hour, minute;
    string name, service_required;
    task object1;

    while (1) {
	cout << endl;
	cout << "Enter the choice\n";
	cout <<
	    "1) New customer arrival \n2) Serve Customer\n3) Customer left remove from waiting list\n4) List all customers waiting for service\n5) To exit";
	cin >> choice;

	switch (choice) {
	case 1:
	    cout << "enter name\n";
	    cin >> name;
	    cout << "Enter date as day,month,year\n";
	    // dmh = the line below only works of the day, month and
	    // year are separated by spaces (or tabs, newlines etc).
	    // But the instructions above indicate that they should be
	    // separated by commas.  So if the user enters
	    // "23,10,2019" then the code will read "23,10,2019" into
	    // "day" and then wait for more input for month and year
	    cin >> day >> month >> year;
	    cout << "Enter time as hour and minute\n";
	    cin >> hour >> minute;
	    object1.insert_node(name, day, month, year, hour, minute);
	    break;
	case 2:
	    object1.serve_customer();
	    break;
	case 3:
	    cout << "enter the name of customer\n";
	    cin >> name;
	    temp = object1.delete_node(name);
	    (temp == 1) ? cout << "Already Served" : cout << "Deleted";
	    break;
	case 4:
	    object1.list_all(object1.head);
	    break;
	case 5:
	    return 0;
	    break;

	}
    }
}

thanks sir really appreciated.
may ALLAH bless you
Topic archived. No new replies allowed.