Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help making the program not allow duplicate id's like in the picture #include #include using namespace std; struct Node { int id; string

I need help making the program not allow duplicate id's like in the picture
#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." 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;
}
const Node* current = head;
while (current != nullptr){
if (current->id == id){
cout "ID: " current->id ", Name: " current->name ", GPA: " current->gpa endl;
return;
}
current = current->next;
}
cout "ID not found." endl;
}
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;
cout "The number is modified." endl;
return;
}
deleteNode(head, id);
addNode(head, id, name, gpa);
}
void purgeList(Node*& head){
while (head != nullptr){
Node* temp = head;
head = head->next;
delete temp;
}
}
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: ";
image text in transcribed

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

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions