Linked List Delete won't work

closed account (oj1C54Gy)
I was task to create a linked list and it must be menu driven I already finish the other parts but the delete a record won't work and it just delete the first node and I to add a menu where a node is updated

Here's the code
#include <iostream>
#include <string>
using namespace std;

// Node Class
class Node {
public:
int roll;
string Name;
string Dept;
int Marks;
Node* next;
};

// Stores the head of the Linked List
Node* head = new Node();

// Check Function to check that if
// Record Already Exist or Not
bool check(int x)
{
// Base Case
if (head == NULL)
return false;

Node* t = new Node;
t = head;

// Traverse the Linked List
while (t != NULL) {
if (t->roll == x)
return true;
t = t->next;
}

return false;
}

// Function to insert the record
void Insert_Record(int roll, string Name,
string Dept, int Marks)
{
// if Record Already Exist
if (check(roll)) {
cout << "Student with this "
<< "record Already Exists\n";
return;
}

// Create new Node to Insert Record
Node* t = new Node();
t->roll = roll;
t->Name = Name;
t->Dept = Dept;
t->Marks = Marks;
t->next = NULL;

// Insert at Begin
if (head == NULL
|| (head->roll >= t->roll)) {
t->next = head;
head = t;
}

// Insert at middle or End
else {
Node* c = head;
while (c->next != NULL
&& c->next->roll < t->roll) {
c = c->next;
}
t->next = c->next;
c->next = t;
}

cout << "Record Inserted "
<< "Successfully\n";
}

// Function to search record for any
// students Record with roll number
void Search_Record(int roll)
{
// if head is NULL
if (!head) {
cout << "No such Record "
<< "Available\n";
return;
}

// Otherwise
else {
Node* p = head;
while (p) {
if (p->roll == roll) {
cout << "Roll Nmuber\t"
<< p->roll << endl;
cout << "Name\t\t"
<< p->Name << endl;
cout << "Department\t"
<< p->Dept << endl;
cout << "Marks\t\t"
<< p->Marks << endl;
return;
}
p = p->next;
}

if (p == NULL)
cout << "No such Record "
<< "Available\n";
}
}

// Function to delete record students
// record with given roll number
// if it exist
int Delete_Record(int roll)
{
Node* t = head;
Node* p = NULL;

// Deletion at Begin
if (t != NULL
&& t->roll == roll) {
head = t->next;
delete t;

cout << "Record Deleted "
<< "Successfully\n";
return 0;
}

// Deletion Other than Begin
while (t != NULL && t->roll != roll) {
p = t;
t = t->next;
}
if (t == NULL) {
cout << "Record does not Exist\n";
return -1;
p->next = t->next;

delete t;
cout << "Record Deleted "
<< "Successfully\n";

return 0;
}
}

// Function to display the Student's
// Record
void Show_Record()
{
Node* p = head;
if (p == NULL) {
cout << "No Record "
<< "Available\n";
}
else {
cout << "Index\tName\tCourse"
<< "\tMarks\n";

// Until p is not NULL
while (p != NULL) {
cout << p->roll << " \t"
<< p->Name << "\t"
<< p->Dept << "\t"
<< p->Marks << endl;
p = p->next;
}
}
}

// Driver code
int main()
{
head = NULL;
string Name, Course;
int Roll, Marks;

// Menu-driven program
while (true) {
cout << "\n\t\tWelcome to Student Record "
"Management System\n\n\tPress\n\t1 to "
"create a new Record\n\t2 to delete a "
"student record\n\t3 to Search a Student "
"Record\n\t4 to view all students "
"record\n\t5 to Exit\n";
cout << "\nEnter your Choice\n";
int Choice;

// Enter Choice
cin >> Choice;
if (Choice == 1) {
cout << "Enter Name of Student\n";
cin >> Name;
cout << "Enter Roll Number of Student\n";
cin >> Roll;
cout << "Enter Course of Student \n";
cin >> Course;
cout << "Enter Total Marks of Student\n";
cin >> Marks;
Insert_Record(Roll, Name, Course, Marks);
}
else if (Choice == 2) {
cout << "Enter Roll Number of Student whose "
"record is to be deleted\n";
cin >> Roll;
Delete_Record(Roll);
}
else if (Choice == 3) {
cout << "Enter Roll Number of Student whose "
"record you want to Search\n";
cin >> Roll;
Search_Record(Roll);
}
else if (Choice == 4) {
Show_Record();
}
else if (Choice == 5) {
exit(0);
}
else {
cout << "Invalid Choice "
<< "Try Again\n";
}
}
return 0;
}
Last edited on
You really should format your code. I've done that so I can see it, so I may as well post it.
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <string>
using namespace std;

// Node Class
class Node {
public:
    int roll;
    string Name;
    string Dept;
    int Marks;
    Node* next;
};

// Stores the head of the Linked List
Node* head = new Node();

// Check Function to check that if
// Record Already Exist or Not
bool check(int x)
{
    // Base Case
    if (head == NULL)
        return false;

    Node* t = new Node;
    t = head;

    // Traverse the Linked List
    while (t != NULL) {
        if (t->roll == x)
            return true;
        t = t->next;
    }

    return false;
}

// Function to insert the record
void Insert_Record(int roll, string Name, string Dept, int Marks)
{
    // if Record Already Exist
    if (check(roll)) {
        cout << "Student with this "
        << "record Already Exists\n";
        return;
    }

    // Create new Node to Insert Record
    Node* t = new Node();
    t->roll = roll;
    t->Name = Name;
    t->Dept = Dept;
    t->Marks = Marks;
    t->next = NULL;

    // Insert at Begin
    if (head == NULL || (head->roll >= t->roll)) {
        t->next = head;
        head = t;
    }

    // Insert at middle or End
    else {
        Node* c = head;
        while (c->next != NULL && c->next->roll < t->roll) {
            c = c->next;
        }
        t->next = c->next;
        c->next = t;
    }

    cout << "Record Inserted " << "Successfully\n";
}

// Function to search record for any
// students Record with roll number
void Search_Record(int roll)
{
    // if head is NULL
    if (!head) {
        cout << "No such Record " << "Available\n";
        return;
    }

    // Otherwise
    else {
        Node* p = head;
        while (p) {
            if (p->roll == roll) {
                cout << "Roll Nmuber\t" << p->roll << endl;
                cout << "Name\t\t" << p->Name << endl;
                cout << "Department\t" << p->Dept << endl;
                cout << "Marks\t\t" << p->Marks << endl;
                return;
            }
            p = p->next;
        }

        if (p == NULL)
            cout << "No such Record " << "Available\n";
    }
}

// Function to delete record students
// record with given roll number
// if it exist
int Delete_Record(int roll)
{
    Node* t = head;
    Node* p = NULL;

    // Deletion at Begin
    if (t != NULL && t->roll == roll) {
        head = t->next;
        delete t;

        cout << "Record Deleted " << "Successfully\n";
        return 0;
    }

    // Deletion Other than Begin
    while (t != NULL && t->roll != roll) {
        p = t;
        t = t->next;
    }
    if (t == NULL) {
        cout << "Record does not Exist\n";
        return -1;
        p->next = t->next;

        delete t;
        cout << "Record Deleted " << "Successfully\n";

        return 0;
    }
}

// Function to display the Student's
// Record
void Show_Record()
{
    Node* p = head;
    if (p == NULL) {
        cout << "No Record " << "Available\n";
    }
    else {
        cout << "Index\tName\tCourse" << "\tMarks\n";

        // Until p is not NULL
        while (p != NULL) {
            cout << p->roll << " \t"  p->Name << "\t" << p->Dept << "\t" << p->Marks << endl;
            p = p->next;
        }
    }
}

// Driver code
int main()
{
    head = NULL;
    string Name, Course;
    int Roll, Marks;

    // Menu-driven program
    while (true) {
        cout << "\n\t\tWelcome to Student Record "
            "Management System\n\n\tPress\n\t1 to "
            "create a new Record\n\t2 to delete a "
            "student record\n\t3 to Search a Student "
            "Record\n\t4 to view all students "
            "record\n\t5 to Exit\n";
        cout << "\nEnter your Choice\n";
        int Choice;

        // Enter Choice
        cin >> Choice;
        if (Choice == 1) {
            cout << "Enter Name of Student\n";
            cin >> Name;
            cout << "Enter Roll Number of Student\n";
            cin >> Roll;
            cout << "Enter Course of Student \n";
            cin >> Course;
            cout << "Enter Total Marks of Student\n";
            cin >> Marks;
            Insert_Record(Roll, Name, Course, Marks);
        }
        else if (Choice == 2) {
            cout << "Enter Roll Number of Student whose " "record is to be deleted\n";
            cin >> Roll;
            Delete_Record(Roll);
        }
        else if (Choice == 3) {
            cout << "Enter Roll Number of Student whose " "record you want to Search\n";
            cin >> Roll;
            Search_Record(Roll);
        }
        else if (Choice == 4) {
            Show_Record();
        }
        else if (Choice == 5) {
            exit(0);
        }
        else {
            cout << "Invalid Choice " << "Try Again\n";
        }
    }
    return 0;
}

Your code:
1
2
3
4
Node* head = new Node();
//
int main() {
    head = NULL;
leaks that memory. It should just be:
 
Node* head = nullptr;

Your Delete_Record seems to be missing a few lines. I've highlighted the missing lines in bold on lines 23/24. I haven't checked if it's correct, but looks right at first sight:
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
int Delete_Record(int roll)
{
    Node* t = head;
    Node* p = NULL;

    // Deletion at Begin
    if (t != NULL && t->roll == roll) {
        head = t->next;
        delete t;

        cout << "Record Deleted " << "Successfully\n";
        return 0;
    }

    // Deletion Other than Begin
    while (t != NULL && t->roll != roll) {
        p = t;
        t = t->next;
    }
    if (t == NULL) {
        cout << "Record does not Exist\n";
        return -1;
    }
    else {
        p->next = t->next;

        delete t;
        cout << "Record Deleted " << "Successfully\n";

        return 0;
    }
}
Last edited on
Consider:

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <string>
using namespace std;

// Node Class
class Node {
public:
	int roll {};
	string Name;
	string Dept;
	int Marks {};
	Node* next {};

	Node() {}
	Node(int r, const std::string& n, const std::string& d, int m) : roll(r), Name(n), Dept(d), Marks(m) {}
};

// Stores the head of the Linked List
Node* head {};

// Check Function to check that if
// Record Already Exist or Not
bool check(int x) {
	// Traverse the Linked List
	for (auto t {head}; t; t = t->next)
		if (t->roll == x)
			return true;

	return false;
}

// Function to insert the record
void Insert_Record(int roll, const string& Name, const string& Dept, int Marks) {
	// if Record Already Exist
	if (check(roll)) {
		cout << "Student with this record already exists\n";
		return;
	}

	// Create new Node to Insert Record
	auto t {new Node(roll, Name, Dept, Marks)};

	if (!head || (head->roll >= t->roll)) {
		// Insert at Begin
		t->next = head;
		head = t;
	} else {
		// Insert at middle or End
		auto c {head};

		for (; c->next && c->next->roll < t->roll; c = c->next);

		t->next = c->next;
		c->next = t;
	}

	cout << "Record Inserted Successfully\n";
}

// Function to search record for any
// students Record with roll number
void Search_Record(int roll)
{
	for (auto p {head}; p; p = p->next)
		if (p->roll == roll) {
			cout << "Roll Nmuber\t" << p->roll << '\n';
			cout << "Name\t\t" << p->Name << '\n';
			cout << "Department\t" << p->Dept << '\n';
			cout << "Marks\t\t" << p->Marks << '\n';
			return;
		}

	cout << "No such Record Available\n";
	return;
}

// Function to delete record students
// record with given roll number
// if it exist
bool Delete_Record(int roll)
{
	auto t {head};

	// Deletion at Begin
	if (t && t->roll == roll) {
		head = t->next;
		delete t;
		return (cout << "Record Deleted Successfully\n"), true;
	}

	// Deletion Other than Begin
	Node* p {};

	for (; t && t->roll != roll; t = t->next)
		p = t;

	if (!t)
		return (cout << "Record does not Exist\n"), false;

	p->next = t->next;
	delete t;
	return (cout << "Record Deleted Successfully\n"), true;
}

// Function to display the Student's
// Record
void Show_Record()
{
	auto p {head};

	if (!p)
		cout << "No Record Available\n";
	else {
		cout << "Index\tName\tCourse" << "\tMarks\n";

		// Until p is not NULL
		for (; p; p = p->next)
			cout << p->roll << " \t" << p->Name << "\t" << p->Dept << "\t" << p->Marks << '\n';
	}
}

// Driver code
int main()
{
	// Menu-driven program

	for (bool quit {false}; !quit; ) {
		cout << "\n\t\tWelcome to Student Record "
			"Management System\n\n\tPress\n\t1 to "
			"create a new Record\n\t2 to delete a "
			"student record\n\t3 to Search a Student "
			"Record\n\t4 to view all students "
			"record\n\t5 to Exit\n";

		cout << "\nEnter your Choice: ";
		int Choice {};

		// Enter Choice
		cin >> Choice;

		switch (Choice) {
			case 1:
			{
				std::string Name, Course;
				int Roll {}, Marks {};

				cout << "Enter Name of Student: ";
				cin >> Name;

				cout << "Enter Roll Number of Student: ";
				cin >> Roll;

				cout << "Enter Course of Student: ";
				cin >> Course;

				cout << "Enter Total Marks of Student: ";
				cin >> Marks;

				Insert_Record(Roll, Name, Course, Marks);
			}
			break;

			case 2:
			{
				int Roll {};

				cout << "Enter Roll Number of Student whose record is to be deleted: ";
				cin >> Roll;

				Delete_Record(Roll);
			}
			break;

			case 3:
			{
				int Roll {};

				cout << "Enter Roll Number of Student whose record you want to Search: ";
				cin >> Roll;

				Search_Record(Roll);
			}
			break;

			case 4:
				Show_Record();
				break;

			case 5:
				quit = true;
				break;

			default:
				cout << "Invalid Choice Try Again\n";
				break;
		}
	}
}

The OP has already closed his account. 🤷‍♂️
Well someone else might get some benefit from the code......
... at least he didn't delete his post. I was half expecting it, that's why I copied it.
In case anyone in the same class is following along...

Notice that check(), Insert_Record(), Delete_Record(), and Search_Record() all look for the record. Let's factor out that code into:
1
2
3
4
5
// Find the node whose roll is the given one.
// Return a reference to the pointer to the node,
// or a reference to the pointer to where the node should
// go.
Node * &find(int roll)

This returns a reference to to node pointer: either head, or some node's next pointer. It has the advantage of stopping when it knows it knows it has passed where the record should be. Building on seeplus's code:

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <string>
using namespace std;

// Node Class
class Node {
  public:
    int roll {};
    string Name;
    string Dept;
    int Marks {};
    Node *next {};

    Node() {}
    Node(int r, const std::string & n, const std::string & d, int m):roll(r), Name(n),
	Dept(d), Marks(m) {}
};

// Stores the head of the Linked List
Node *head {};

// Find the node whose roll is the given one.
// Return a reference to the pointer to the node,
// or a reference to the pointer to where the node should
// go.
Node * &
find(int roll)
{
    Node **pp, *p;
    for (pp = &head; (p = *pp); pp = &(p->next)) {
	if (p->roll >= roll) break;
    }
    return *pp;
}

// Check Function to check that if
// Record Already Exist or Not
bool
check(int x)
{
    // Traverse the Linked List
    Node *p = find(x);
    return p && p->roll == x;
}

// Function to insert the record
void
Insert_Record(int roll, const string & Name, const string & Dept, int Marks)
{
    // if Record Already Exist
    Node * &p {find(roll)};
    if (p && p->roll == roll) {
	cout << "Student with this record already exists\n";
	return;
    }
    // Create new Node to Insert Record
    auto t {new Node(roll, Name, Dept, Marks)};

    // Link it in
    t->next = p;
    p = t;
    cout << "Record Inserted Successfully\n";
}

// Function to search record for any
// students Record with roll number
void
Search_Record(int roll)
{
    Node *p = find(roll);
    if (p && p->roll == roll) {
	cout << "Roll Nmuber\t" << p->roll << '\n';
	cout << "Name\t\t" << p->Name << '\n';
	cout << "Department\t" << p->Dept << '\n';
	cout << "Marks\t\t" << p->Marks << '\n';
	return;
    }

    cout << "No such Record Available\n";
}

// Function to delete record students
// record with given roll number
// if it exist
bool
Delete_Record(int roll)
{
    Node * &p = find(roll);
    if (p && p->roll == roll) {
	Node *old = p;
	p = p->next;
	delete old;
	cout << "Record Deleted Successfully\n";
	return true;
    }
    
    cout << "Record does not Exist\n";
    return  false;
}

// Function to display the Student's
// Record
void
Show_Record()
{
    auto p {head};

    if (!p)
	cout << "No Record Available\n";
    else {
	cout << "Index\tName\tCourse" << "\tMarks\n";

	// Until p is not NULL
	for (; p; p = p->next)
	    cout << p->roll << " \t" << p->Name << "\t" << p->Dept
		 << "\t" << p->Marks << '\n';
    }
}

// Driver code
int
main()
{
    // Menu-driven program

    for (bool quit {false}; !quit;) {
	cout << "\n\t\tWelcome to Student Record "
	    "Management System\n\n\tPress\n\t1 to "
	    "create a new Record\n\t2 to delete a "
	    "student record\n\t3 to Search a Student "
	    "Record\n\t4 to view all students " "record\n\t5 to Exit\n";

	cout << "\nEnter your Choice: ";
	int Choice {};

	// Enter Choice
	cin >> Choice;

	switch (Choice) {
	case 1:
	    {
		std::string Name, Course;
		int Roll {}, Marks {};

		cout << "Enter Name of Student: ";
		cin >> Name;

		cout << "Enter Roll Number of Student: ";
		cin >> Roll;

		cout << "Enter Course of Student: ";
		cin >> Course;

		cout << "Enter Total Marks of Student: ";
		cin >> Marks;

		Insert_Record(Roll, Name, Course, Marks);
	    }
	    break;

	case 2:
	    {
		int Roll {};

		cout << "Enter Roll Number of Student whose record is to be deleted: ";
		cin >> Roll;

		Delete_Record(Roll);
	    }
	    break;

	case 3:
	    {
		int Roll {};

		cout << "Enter Roll Number of Student whose record you want to Search: ";
		cin >> Roll;

		Search_Record(Roll);
	    }
	    break;

	case 4:
	    Show_Record();
	    break;

	case 5:
	    quit = true;
	    break;

	default:
	    cout << "Invalid Choice Try Again\n";
	    break;
	}
    }
}

Topic archived. No new replies allowed.