Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need the search function to not just work with the first node and to display id not found if the id does not exist

I need the search function to not just work with the first node and to display id not found if the id does not exist
#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;
}
}
void searchList(const Node* head, int id){
if (head == nullptr){
cout << "List is empty." << endl;
return;
}
if (head->id == id){
cout <<"ID: "<< head->id <<", Name: "<< head->name <<", GPA: "<< head->gpa << endl;
return;
}
}
void modifyNode(Node* head, int id, const string& name, float gpa){
if (head == nullptr){
cout << "List is empty. Cannot modify." << endl;
return;
}
if (head->id == id){
head->name = name;
head->gpa = gpa;
return;
}
deleteNode(head, id);
addNode(head, id, name, gpa);
cout << "The number is modified." << endl;
}
// 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;
}
int id;
cout << "Enter ID to delete: ";
cin >> id;
deleteNode(head, id);
cout << "Node deleted." << endl;
} 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;
}
int id;
string name;
float gpa;
cout << "Enter ID to modify: ";
cin >> id;
cout << "Enter new Name: ";
cin >> name;
cout << "Enter new GPA: ";
cin >> gpa;
modifyNode(head, id, name, gpa);
} else if (command =="6"){
if (!created){
cout << "Create Linked list first." << endl;
continue;
}
// Implement purge functionality
} else if (command =="7"){
if (

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Solve. 5 - 2x = -2x + 3

Answered: 1 week ago