Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I make the delete function work properly where it deletes a node based on its ID #include #include using namespace std; struct Node

How can I make the delete function work properly where it deletes a node based on its ID
#include
#include
using namespace std;
struct Node {
int id;
string name;
float gpa;
Node* next;
};
Node* createNode(int id, const string& name, float gpa){
Node* newNode = new Node;
newNode->id = id;
newNode->name = name;
newNode->gpa = gpa;
newNode->next = nullptr;
return newNode;
}
void addNode(Node*& head, int id, const string& name, float gpa){
Node* newNode = createNode(id, name, gpa);
if (head == nullptr){
head = newNode;
return;
}
if (head->id > id){
newNode->next = head;
head = newNode;
return;
}
Node* current = head;
while (current->next != nullptr && current->next->id < id){
current = current->next;
}
if (current->next != nullptr && current->next->id == id){
cout << "Duplicate ID found. Please enter other ID."<< endl;
delete newNode;
return;
}
newNode->next = current->next;
current->next = newNode;
}
void deleteNode(Node*& head, int id){
if (head == nullptr){
cout << "List is empty. Cannot delete." << endl;
return;
}
if (head->id == id){
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
while (current->next != nullptr && current->next->id != id){
current = current->next;
}
if (current->next == nullptr){
cout <<"ID not found. Cannot delete." << endl;
return;
}
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
void displayList(const Node* head){
if (head == nullptr){
cout << "List is empty." << endl;
return;
}
const Node* current = head;
while (current != nullptr){
cout <<"ID: "<< current->id <<", Name: "<< current->name <<", GPA: "<< current->gpa << endl;
current = current->next;
}
}
// Other functions like Modify, Purge, Search can be implemented similarly
int main(){
Node* head = nullptr;
bool created = false;
while (true){
cout << "Enter command" << endl;
cout <<"1. Create
2. Add
3. Delete
4. Display
5. Modify
6. Purge entire list
7. Search for node
8. Exit the program" << endl;
string command;
cin >> command;
if (command =="1"){
created = true;
cout << "Linked list created." << endl;
} else if (command =="2"){
if (!created){
cout << "Create Linked list first." << endl;
continue;
}
int id;
string name;
float gpa;
cout << "Enter ID: ";
cin >> id;
cout << "Enter Name: ";
cin >> name;
cout << "Enter GPA: ";
cin >> gpa;
cout << "Node added." << endl;
addNode(head, id, name, gpa);
} else if (command =="3"){
if (!created){
cout << "Create Linked list first." << endl;
continue;
}
// Implement delete functionality
} else if (command =="4"){
if (!created){
cout << "Create Linked list first." << endl;
continue;
}
displayList(head);
} else if (command =="5"){
if (!created){
cout << "Create Linked list first." << endl;
continue;
}
// Implement modify functionality
} else if (command =="6"){
if (!created){
cout << "Create Linked list first." << endl;
continue;
}
// Implement purge functionality
} else if (command =="7"){
if (!created){
cout << "Create Linked list first." << endl;
continue;
}
// Implement search functionality
} else if (command =="8"){
break;
} else {
cout << "Invalid command. Please try again." << endl;
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions