I have a program that allows a user to add and delete (amongst other things) an account to a binary search tree. I have tested/debugged this code and have found the area causing the issue, I'm just not quite sure how to fix it. I can add accounts fine, and I can remove all but the root node without error. Here is my function:
if((current->llink==NULL && current->rlink != NULL) || (current->llink != NULL && current->rlink==NULL)){
if(current->llink==NULL && current->rlink != NULL){
if(trailcurrent->llink==current){
trailcurrent->llink=current->rlink;
delete current;
current=NULL;
cout << "Account #:" << acctNum << " has been removed from the list." << endl;
}
else{
trailcurrent->rlink=current->rlink;
delete current;
current=NULL;
cout << "Account #:" << acctNum << " has been removed from the list." << endl;
}
}
else{
if(trailcurrent->llink==current){
trailcurrent->llink=current->llink;
delete current;
current=NULL;
cout << "Account #:" << acctNum << " has been removed from the list." << endl;
}
else{
trailcurrent->rlink=current->llink;
delete current;
current=NULL;
cout << "Account #:" << acctNum << " has been removed from the list." << endl;
}
}
return;
}
if(current->llink==NULL && current->rlink==NULL){
if(trailcurrent->llink==current) // This 'if' statement crashes the program with a read violation when deleting the root.
trailcurrent->llink=NULL;
else
trailcurrent->rlink=NULL;
delete current;
cout << "Account #:" << acctNum << " has been removed from the list." << endl;
return;
}
if(current->llink != NULL && current->rlink != NULL){
nodeType<accountType>* check=current->rlink;
if((current->llink==NULL)&&(current->rlink==NULL)){
current=check;
delete check;
current->rlink=NULL;
cout << "Account #:" << acctNum << " has been removed from the list." << endl;
}
else{
if((current->rlink)->llink!=NULL){
nodeType<accountType>* leftCurrent;
nodeType<accountType>* leftTrailCurrent;
leftTrailCurrent=current->rlink;
leftCurrent=(current->rlink)->llink;
while(leftCurrent->llink != NULL){
leftTrailCurrent=leftCurrent;
leftCurrent=leftCurrent->llink;
}
current->info=leftCurrent->info;
delete leftCurrent;
leftTrailCurrent->llink=NULL;
cout << "Account #:" << acctNum << " has been removed from the list." << endl;
}
else{
nodeType<accountType>* temp=current->rlink;
current->info=temp->info;
current->rlink=temp->rlink;
delete temp;
cout << "Account #:" << acctNum << " has been removed from the list." << endl;
}
}
return;
}
}
With the line that is causing me issues, do I have to have an 'if' statement before it, where trailcurrent == NULL;? If so, inside of that 'if' statement would I do something similar to
if(current->rlink>current->llink){}
Related
This program is a binary search tree of string to store information of students with the following details such as id, name and CGPA. And using unique id of a student to search, delete and insert different data. But in deletion when a subtree is involved the tree gets separated, I don't know what I'm doing wrong.
#include <iostream>
using namespace std;
struct BstNode
{
string iD;
string name;
float cgpa;
BstNode *left;
BstNode *right;
};
BstNode *root;
BstNode *GetNewNode(string iD0, string n0, float cg0)
{
BstNode *NewNode = new BstNode();
NewNode->iD = iD0;
NewNode->name = n0;
NewNode->cgpa = cg0;
NewNode->left = NULL;
NewNode->right = NULL;
return NewNode;
}
void PreOrder(BstNode *root)
{
if (root == NULL)
{
return;
}
cout << "ID: " << root->iD << "\nNAME: " << root->name << "\nCGPA: " << root->cgpa << endl;
PreOrder(root->left);
PreOrder(root->right);
}
void InOrder(BstNode *root)
{
if (root == NULL)
{
return;
}
InOrder(root->left);
cout << "ID: " << root->iD << "\nNAME: " << root->name << "\nCGPA: " << root->cgpa << endl;
InOrder(root->right);
}
void PostOrder(BstNode *root)
{
if (root == NULL)
{
return;
}
PostOrder(root->left);
PostOrder(root->right);
cout << "ID: " << root->iD << "\nNAME: " << root->name << "\nCGPA: " << root->cgpa << endl;
cout << endl;
}
BstNode *Insert(BstNode *root, string iD2, string n2, float cg2)
{
if (root == NULL)
{
root = GetNewNode(iD2, n2, cg2);
}
else if (iD2 <= root->iD)
{
root->left = Insert(root->left, iD2, n2, cg2);
}
else
{
root->right = Insert(root->right, iD2, n2, cg2);
}
return root;
}
BstNode *Rectify(BstNode *root, string id1, string n, float cg)
{
root->iD = id1;
root->name = n;
root->cgpa = cg;
return root;
}
struct BstNode *FindMin(BstNode *root)
{
struct BstNode* current = root;
/* loop down to find the leftmost leaf */
while (current && current->left != NULL)
current = current->left;
return current;
}
BstNode *Delete(BstNode *root, string data)
{
if (root == NULL)
return root;
else
{
if (data < root->iD)
root->left = Delete(root->left, data);
else if (data > root->iD)
root->right = Delete(root->right, data);
else
{
if (root->left == NULL and root->right == NULL)
return NULL;
else if (root->left == NULL)
{
struct BstNode *temp = root->right;
delete(root);
return temp;
}
else if (root->right == NULL)
{
struct BstNode *temp = root->left;
delete(root);
return temp;
}
else
{
struct BstNode *temp = FindMin(root->right);
root->iD = temp->iD;
root->name = temp->name;
root->cgpa = temp->cgpa;
root->right = Delete(root->right, temp->iD);
}
}
return root;
}
}
BstNode *Search(BstNode * root, string iDs)
{
cout << "Finding " << endl;
if (root == NULL)
{
cout << "No such info!" << endl;
return root;
}
else if (root->iD == iDs)
{
cout << "Found 0 " << endl;
cout << "ID: " << root->iD << endl;
cout << "NAME: " << root->name << endl;
cout << "CGPA: " << root->cgpa << endl;
bool done = false;
while (!done)
{
cout << "Would you like to update this student's information? [y/n]\n";
string x;
cin >> x;
if (x == "y")
{
cout << "The following fields are updatable:\n";
cout << "1. Student ID\n";
cout << "2. Student Name\n";
cout << "3. Student CGPA\n";
cout << "4. Remove Student\n";
cout << "Enter your choice:\n";
bool done1 = false;
while (!done1)
{
int ch;
cin >> ch;
if (ch == 1)
{
cout << "Please enter new ID: \n";
string nwid;
cin >> nwid;
cout << "============================" << endl;
cout << "Updating \n";
cout << "============================" << endl;
root = Rectify(root, nwid, root->name, root->cgpa);
cout << "============================" << endl;
cout << "Updated Successfully! \n";
cout << "============================" << endl;
cout << "============================" << endl;
cout << root->iD << endl;
cout << root->name << endl;
cout << root->cgpa << endl;
cout << "============================" << endl;
done1 = true;
}
else if (ch == 2)
{
cout << "Please enter new name: \n";
string nwname;
cin >> nwname;
cout << "============================" << endl;
cout << "Updating \n";
cout << "============================" << endl;
root = Rectify(root, root->iD, nwname, root->cgpa);
cout << "============================" << endl;
cout << "Updated Successfully! \n";
cout << "============================" << endl;
cout << "============================" << endl;
cout << root->iD << endl;
cout << root->name << endl;
cout << root->cgpa << endl;
cout << "============================" << endl;
done1 = true;
}
else if (ch == 3)
{
cout << "Please enter new CGPA: \n";
float nwcg;
cin >> nwcg;
cout << "============================" << endl;
cout << "Updating \n";
cout << "============================" << endl;
root = Rectify(root, root->iD, root->name, nwcg);
cout << "============================" << endl;
cout << "Updated Successfully! \n";
cout << "============================" << endl;
cout << "============================" << endl;
cout << root->iD << endl;
cout << root->name << endl;
cout << root->cgpa << endl;
cout << "============================" << endl;
done1 = true;
}
else if (ch == 4)
{
cout << "Removing\n";
root = Delete(root, root->iD);
cout << "Removed!\n";
return root;
done1 = true;
}
else
{
cout << "Wrong input try again! \n";
}
}
done = true;
}
else if (x == "n")
{
done = true;
}
else
{
cout << "Wrong input try again! \n";
}
}
return root;
}
else if (iDs <= root->iD)
{
return Search(root->left, iDs);
}
else
{
return Search(root->right, iDs);
}
}
int main()
{
root = NULL;
root = Insert(root, "21-44631-1", "Rafiul", 3.75);
root = Insert(root, "21-44531-1", "Haque", 4.0);
root = Insert(root, "21-44431-1", "Rafsun", 3.78);
root = Insert(root, "21-44331-1", "Rafi", 3.5);
root = Insert(root, "21-44231-1", "ABC", 4.0);
root = Insert(root, "21-44611-1", "AC", 4.0);
cout << "Please enter the ID you want to search: ";
string s;
cin >> s;
cout << endl;
Search(root, s);
cout << "Reading tree in preorder : \n";
PreOrder(root);
cout<<"============================"<< endl;
cout << "\nReading tree in inorder : \n";
InOrder(root);
cout<<"============================"<< endl;
cout << "\nReading tree in postorder : \n";
PostOrder(root);
cout << "============================" << endl;
}
Deleting a BST node from a simple (e.g. non-self-balancing) implementation involves three things:
Find the node you're deleting. More specifically, find the pointer in the tree (which may be the root pointer) that points to the node you want to delete.
Once found, promote one of the children to occupy the vacancy about to be left by the node being deleted.
Hang the opposite child (if there even is one) in the left (or right, depending on which child you promoted in (2), position possible of the promoted child.
Note that it is possible to cover all four node+children cases (no children, only a left child, only a right child, or both), using a single algorithm, especially when using a pointer-to-pointer to iterate the tree. It is done like this (liberally commented to try and explain what is going on):
BstNode *Delete(BstNode *root, string data)
{
BstNode **pp = &root;
// find the node we want to delete by id
while (*pp)
{
if (data < (*pp)->iD)
pp = &(*pp)->left;
else if ((*pp)->iD < data)
pp = &(*pp)->right;
else // equivalent. fount it
break;
}
// if this is non-null, we found the pointer in the
// tree that points to the node we want to delete.
// note that this could even be the root pointer
// where we started.
if (*pp)
{
// remember the node. we're about to overwrite
// the tree pointer that is referring to it.
BstNode *victim = *pp;
// always promote the right node.
*pp = victim->right;
// now iterate down the opposite side of the
// note we just promoted, looking for the first
// null slot on which we can hang our sibling
// (if there even is one; it's ok if there isn't)
while (*pp)
pp = &(*pp)->left;
// this is where we hang the sibling node
*pp = victim->left;
// the tree is fixed up and the node excised. we
// can delete it now.
delete victim;
}
// return this. remember, root may have been pointing
// to the node we deleted, and if so, it has changed
// so we need to inform the caller by return result.
return root;
}
Sans the overt comments explaining what is happening, it is a surprisingly small amount of code, especially when you consider what it is accomplishing.
My code is working but there is still a problem so, technically it is not working. The problem that I encounter is that whenever I tried to search for my 2nd, 3rd and so on ID that I inputted command prompt is not working and then it exit.
Here is my code
void queue::qdisplay() // 7.2 ID CUSTOMER DETAILS
{
int exist = 0;
int C_id;
node *temp = front;
cout << "~~~~~~~~~~~~~~~ Customer Details ~~~~~~~~~~~~~~~" << endl << endl;
cout << "Enter customer ID: ";
cin >> C_id;
if (front == NULL)
{
cout << "No Information";
}
else
{
while(temp!=NULL)
{
if (temp -> C_ID == C_id)
{
cout << setw(5) << "ID" << setw(20) << "Name: " << setw(10) << "Address" <<endl;
cout << setw(5) << temp -> C_ID << setw(20) << temp -> C_name << setw(10) << temp -> C_address << endl;
temp = temp->next;
cout<< endl;
exist = 1;
break;
}
}
if(exist==0)
{
cout<<"MOVIE DOES NOT EXIST!!!"<<endl;
}
}
}
food is ok, please eat,
饭好了,请吃,
call once, searce once
调用一次,查询一次
if you want seach 2nd, 3rd
如果你想查第二个,第三个
do this,
你需要这样做
if you understand how to eated the food, please do accept
如果你懂我的意思,请点accept。
yourQueue.qdisplay();
yourQueue.qdisplay();
yourQueue.qdisplay();
void queue::qdisplay() // 7.2 ID CUSTOMER DETAILS
{
int exist = 0;
int C_id;
node *temp = front;
cout << "~~~~~~~~~~~~~~~ Customer Details ~~~~~~~~~~~~~~~" << endl << endl;
cout << "Enter customer ID: ";
cin >> C_id;
if (front == NULL)
{
cout << "No Information";
}
else
{
while(temp!=NULL)
{
if (temp -> C_ID == C_id)
{
cout << setw(5) << "ID" << setw(20) << "Name: " << setw(10) << "Address" <<endl;
cout << setw(5) << temp -> C_ID << setw(20) << temp -> C_name << setw(10) << temp -> C_address << endl;
cout<< endl;
exist = 1;
break;
}
temp = temp->next;
}
if(exist==0)
{
cout<<"MOVIE DOES NOT EXIST!!!"<<endl;
}
}
}
I am making this function that deletes an element inside of a list. The problem is when I delete the last remaining node of the list it gives me back nothing. To be precise, let's say we have a link list
head -> node_1 -> node_2 -> NULL
I want to delete all of the items inside the list hence " head -> NULL; "
void delete_item(NODE **start,int *last){
NODE *before, *discard;
int delete_value;
if(is_empty(*start) == 1){
cout << endl << "THE LIST IS EMPTY";
}else{
discard = *start;
print_items(*start);
cout << endl << "What item would you like to delete?: ";
cin >> delete_value;
if((*start)->next == NULL){
cout << endl <<"entered" << endl;
delete *start;
*start = NULL;
--*last;
}
//if delete value is in the first node
if((*start)->value == delete_value){
*start = (*start)->next;
(*start)->prev = NULL;
free(discard);
--*last;
cout << endl << "ITEM SUCCESSFULLY DELETED!";
}else{
int index{1};
//searches for the value
while(discard != NULL && discard->value != delete_value){
before = discard;
discard = discard->next;
++index;
}
if(discard == NULL){
cout << endl<< "ITEM DOES NOT EXIST TRY AGAIN!" << endl;
}else{
//if item is in the last node of the list
if(index == *last){
before->next = NULL;
free(discard);
--*last;
cout << endl << "ITEM SUCCESSFULLY DELETED!" << endl;
}else{
//if item is in between the list
discard->next->prev = before->next;
before->next = discard->next;
free(discard);
--*last;
cout << endl << "ITEM SUCCESSFULLY DELETED!" << endl;
}
}
}
}
}
It is always best to delete a given node in a seperate function and then call that function according to your requirement. You can find that kind of code in any coding website.
However, I did not want to discourage your method. So I am editing your code only.
void delete_item(NODE **start,int *last){
NODE *before, *discard;
int delete_value;
if(is_empty(*start) == 1){
cout << endl << "THE LIST IS EMPTY";
}else{
discard = *start;
print_items(*start);
cout << endl << "What item would you like to delete?: ";
cin >> delete_value;
//if delete value is in the first node
if((*start)->value == delete_value){
*start = (*start)->next;
(*start)->prev = NULL;
free(discard);
--*last;
cout << endl << "ITEM SUCCESSFULLY DELETED!";
}else{
int index{1};
//searches for the value
while(discard != NULL && discard->value != delete_value){
before = discard;
discard = discard->next;
++index;
}
if(discard == NULL){
cout << endl<< "ITEM DOES NOT EXIST TRY AGAIN!" << endl;
}else{
//if item is in the last node of the list
if(index == *last){
before->next = NULL;
free(discard);
--*last;
cout << endl << "ITEM SUCCESSFULLY DELETED!" << endl;
}else{
//if item is in between the list
discard->next->prev = before;
before->next = discard->next;
free(discard);
--*last;
cout << endl << "ITEM SUCCESSFULLY DELETED!" << endl;
}
}
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i have some code here. I'm using linked list in this code. We can add note, displas it and also delete. The problem occur when i want to delete something.
1. create one node, then try to delete it. It can detect and delete the node.
2. create two node, then i try to delete the 1st one. but it delete the second.
I really run out of idea right now. Hopefully anyone can help me. Thank you
#include <iostream>
#include <string.h>
using namespace std;
const int SIZE = 10;
//1st class
class SportShoe {
private:
struct nodeSport {
int ShoeID;
char BrandShoe[SIZE];
char TypeShoe[SIZE];
char ColourShoe[SIZE];
int SizeShoe;
float PriceShoe;
nodeSport *last;
};
nodeSport *first = NULL;
public:
int MenuSportShoe();
void AddSportShoe();
void DisplaySportShoe();
void DeleteSportShoe();
static void ExitSportShoe();
};
//2nd class
class HighHeel {
private:
struct nodeHeel {
int ProductCode;
char BrandHeel[SIZE];
char MaterialHeel[SIZE];
char ColourHeel[SIZE];
int HeightHeel;
float PriceHeel;
nodeHeel *next;
};
nodeHeel *start = NULL;
public:
int MenuHighHeel();
void AddHighHeel();
void DisplayHighHeel();
void DeleteHighHeel();
static void ExitHighHeel()
{
SportShoe::ExitSportShoe();
}
};
int SportShoe::MenuSportShoe() {
int OptionSportShoe = 0;
cout << endl;
cout << ">> Please select from the menu below <<" << endl;
cout << ":: 1 :: Add item to shoe list" << endl;
cout << ":: 2 :: Display shoes list" << endl;
cout << ":: 3 :: Delete item from the list" << endl;
cout << ":: 4 :: Back" << endl;
cout << "=>> ";
cin >> OptionSportShoe;
while (OptionSportShoe == 1){
AddSportShoe();
}
while (OptionSportShoe == 2){
DisplaySportShoe();
}
while (OptionSportShoe == 3){
DeleteSportShoe();
}
while (OptionSportShoe == 4){
ExitSportShoe();
}
return 0;
}
int HighHeel::MenuHighHeel() {
int OptionHighHeel = 0;
cout << endl;
cout << ">> Please select from the menu below <<" << endl;
cout << ":: 1 :: Add item to the Heel List" << endl;
cout << ":: 2 :: Display the Heel List" << endl;
cout << ":: 3 :: Delete item from the list" << endl;
cout << ":: 4 :: Back" << endl;
cout << "=>> ";
cin >> OptionHighHeel;
while (OptionHighHeel == 1){
AddHighHeel();
}
while (OptionHighHeel == 2){
DisplayHighHeel();
}
while (OptionHighHeel == 3){
DeleteHighHeel();
}
while (OptionHighHeel == 4){
SportShoe::ExitSportShoe();
}
return 0;
}
void SportShoe::AddSportShoe() {
nodeSport *tempShoe1, *tempShoe2;
tempShoe1 = new nodeSport;
cout << "Sport Shoe Section." << endl;
cout << "Please enter the Shoe ID : (eg. 43210) " << endl;
cout << "=>> ";
cin >> tempShoe1->ShoeID;
cout << "Please enter the Shoe Brand: (eg. Adidas) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempShoe1->BrandShoe,SIZE);
cout << "Please enter the Shoe Type : (eg. Running) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempShoe1->TypeShoe,SIZE);
cout << "What is the Shoe Colour : (eg. Grey) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempShoe1->ColourShoe,SIZE);
cout << "Please enter Shoe Size : (eg. 9) " << endl;
cout << "=>> ";
cin >> tempShoe1->SizeShoe;
cout << "Please enter the price of the Shoe : (eg. RM123.45) " << endl;
cout << "=>> RM ";
cin >> tempShoe1->PriceShoe;
tempShoe1->last = NULL;
if (first == NULL)
first = tempShoe1;
else
{
tempShoe2 = first;
while (tempShoe2->last != NULL)
tempShoe2 = tempShoe2->last;
tempShoe2->last = tempShoe1;
}
system("PAUSE");
MenuSportShoe();
}
void HighHeel::AddHighHeel() {
nodeHeel *tempHeel1, *tempHeel2;
tempHeel1 = new nodeHeel;
cout << "Heel Section." << endl;
cout << "Please enter Heel Code : (eg. 98765) " << endl;
cout << "=>> ";
cin >> tempHeel1->ProductCode;
cout << "Please enter Heel Brand: (eg. Gucci) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempHeel1->BrandHeel,SIZE);
cout << "Please enter Heel Material : (eg. Leather) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempHeel1->MaterialHeel,SIZE);
cout << "What is the Heel Colour : (eg. Red) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempHeel1->ColourHeel,SIZE);
cout << "Please enter Heel Height (cm) : (eg. 2.25) " << endl;
cout << "=>> ";
cin >> tempHeel1->HeightHeel;
cout << "Please enter the Heel Price : (eg. RM123.45) " << endl;
cout << "=>> RM ";
cin >> tempHeel1->PriceHeel;
tempHeel1->next = NULL;
if (start == NULL)
start = tempHeel1;
else
{
tempHeel2 = start;
while (tempHeel2->next != NULL)
tempHeel2 = tempHeel2->next;
tempHeel2->next = tempHeel1;
}
system("PAUSE");
MenuHighHeel();
}
void SportShoe::DisplaySportShoe() {
nodeSport *tempShoe1;
tempShoe1 = first;
if (tempShoe1 == NULL){
cout << "List empty." << endl;
cout << endl;
system("PAUSE");
MenuSportShoe();
}
else{
while(tempShoe1){
cout << "Sport Shoe Section." << endl;
cout << "ID =>> " << tempShoe1->ShoeID << endl;
cout << "Brand =>> " << tempShoe1->BrandShoe << endl;
cout << "Type =>> " << tempShoe1->TypeShoe << endl;
cout << "Colour =>> " << tempShoe1->ColourShoe << endl;
cout << "Size =>> " << tempShoe1->SizeShoe << endl;
cout << "Price =>> " << tempShoe1->PriceShoe << endl;
cout << endl;
tempShoe1 = tempShoe1->last;
}
system("PAUSE");
MenuSportShoe();
}
}
void HighHeel::DisplayHighHeel() {
nodeHeel *tempHeel1;
tempHeel1 = start;
if (tempHeel1 == NULL){
cout << " List empty." << endl;
cout << endl;
system("PAUSE");
MenuHighHeel();
}
else{
while(tempHeel1){
cout << "Heel Section." << endl;
cout << "Heel Code =>> " << tempHeel1->ProductCode << endl;
cout << "Brand =>> " << tempHeel1->BrandHeel << endl;
cout << "Material =>> " << tempHeel1->MaterialHeel << endl;
cout << "Colour =>> " << tempHeel1->ColourHeel << endl;
cout << "Height (cm) =>> " << tempHeel1->HeightHeel << endl;
cout << "Price =>> " << tempHeel1->PriceHeel << endl;
cout << endl;
tempHeel1 = tempHeel1->next;
}
system("PAUSE");
MenuHighHeel();
}
}
void SportShoe::DeleteSportShoe(){
nodeSport *tempShoe1, *tempShoe2;
int DataShoe;
cout << "Sport Shoe Section." << endl;
cout << "\nEnter the Shoes ID to be deleted: (eg. 123) "<< endl;
cout << "=>> ";
cin >> DataShoe;
tempShoe2 = tempShoe1 = first;
while((tempShoe1 != NULL) && (DataShoe == tempShoe1-> ShoeID))
{
tempShoe2 = tempShoe1;
tempShoe1 = tempShoe1->last;
}
if(tempShoe1 == NULL)
{
cout << "\nRecord not Found!!!" << endl;
system("PAUSE");
MenuSportShoe();
}
if((tempShoe1 == first) && (DataShoe == tempShoe1-> ShoeID))
{
first = first->last;
cout << "\nData found " << endl;
}
else{
tempShoe2->last = tempShoe1->last;
if(tempShoe1->last == NULL){
tempShoe2 = tempShoe2;
}
cout << "\nData deleted "<< endl;
}
delete(tempShoe1);
cout << endl;
system("PAUSE");
MenuSportShoe();
}
void HighHeel::DeleteHighHeel(){
nodeHeel *tempHeel1, *tempHeel2;
int DataHeel;
cout << "Heel Section." << endl;
cout << "\nEnter the Heel Code to be deleted: (eg. 123) "<< endl;
cout << "=>> ";
cin >> DataHeel;
tempHeel2 = tempHeel1 = start;
while((tempHeel1 != NULL) && (DataHeel == tempHeel1->ProductCode))
{
tempHeel2 = tempHeel1;
tempHeel1 = tempHeel1->next;
}
if(tempHeel1 == NULL)
{
cout << "\nRecord not Found!!!" << endl;
system("PAUSE");
MenuHighHeel();
}
if(tempHeel1 == start)
{
start = start->next;
cout << "\nData deleted "<< endl;
}
else{
tempHeel2->next = tempHeel1->next;
if(tempHeel1->next == NULL){
tempHeel2 = tempHeel2;
}
cout << "\nData deleted "<< endl;
}
delete(tempHeel1);
cout << endl;
system("PAUSE");
MenuHighHeel();
}
void SportShoe::ExitSportShoe(){
int sepatu;
cout << endl;
cout << ">> Please choose the option below <<"<<endl;
cout << ":: 1 :: Sport Shoe." << endl;
cout << ":: 2 :: Ladies High Heel." << endl;
cout << ":: 3 :: Exit" << endl;
cout << "=>> ";
cin >> sepatu;
while(sepatu == 1){
SportShoe listShoe;
listShoe.MenuSportShoe();
}
while(sepatu == 2){
HighHeel listShoe;
listShoe.MenuHighHeel();
}
while(sepatu == 3){
cout << ">> Have a nice day. See you soon! <<"<< endl;
exit(1);
}
}
main() {
cout << ">> Hello! Welcome to MySepatu Online (Administrator Site) <<";
cout << endl;
SportShoe::ExitSportShoe();
return 0;
}
This block of code, in your SportShoe::DeleteSportShoe function
while ((tempShoe1 != NULL) && (DataShoe == tempShoe1->ShoeID))
{
tempShoe2 = tempShoe1;
tempShoe1 = tempShoe1->last;
}
The while loop stops as soon as it encounters the first node in the list that does not have the matching id. Which means the subsequent code it will either delete the wrong node or nothing at all.
Probably should be:
while ((tempShoe1 != NULL) && (DataShoe != tempShoe1->ShoeID))
{
tempShoe2 = tempShoe1;
tempShoe1 = tempShoe1->last;
}
Some suggestions for improving your code:
Your HighHeel and SportShoe class are 99% the same. They should have a common base class - especially for linked list management.
Completely separate out the user interface (menu printing and input) code form the code that maintains your data model (linked list).
Don't use char BrandShoe[SIZE] to store a string in C++. That breaks as soon as I typed in more than SIZE (10) characters. Use the std::string class. You get that by #include <string> - not to be confused with #include <string.h>
Meet multiple problems while coding, I want to build a room reservation system with linked list,
I want the user can see the room number in the menu and when the room number is taken, the room number will be deleted from the menu.
Is there anyways to simplified the codes, my codes is very messy, please help thanks
1.First I used
struct nodes{
int single_room[5] = {101,102,103,104,105};
int double_room[4] = {201,202,203,204,205};
};
But when I want to compare the Linked List with user input it came out errors
void reservation::UpdateReservation(){
int x;
node *current;
current = head;
bool found;
found = false;
cout << "Please enter your room number to update your reservation:
";
cin >> x;
while ((current != NULL) && (!found))
{
if (current -> single_room == x)
found = true;
else
current = current -> link;
}
while(found)
{
cout <<"The number is exist";
cout << endl;
return found;
cout << value;
}
cout << "The number is not found";
cout << endl;
}
}
It came up an error with ISO C++ forbids comparison between pointer and integer.
After I put a for loop, for the single_room[i], but the current -> single_room[i] is empty..
I've stackoverflowed a lot of questions and found out I can't initialize a linked list, so I used one by one initialize value inside the notes
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
using namespace std;
class reservation{
private:
struct node
{
int single_room;
int double_room;
int deluxe_suite;
int president_suite;
int room_number;
string name;
int phone_number;
int date;
int number_of_nights;
int number_of_pax;
int time;
node *link;
}*head;
public:
reservation();
void InitializeValue_single_room(int a);
void InitializeValue_double_room(int b);
void InitializeValue_deluxe_suite(int c);
void InitializeValue_president_suite(int d);
void menu();
void Room_size();
void InsertReservation();
void UpdateReservation();
// void DeleteReservation();
void DisplayReservation();
~reservation();
};
reservation::reservation()
{
head = NULL;
}
reservation::~reservation()
{
node *q;
if (head == NULL)
{
return;
}
while (head != NULL)
{
q = head->link;
delete head;
head = q;
}
}
void reservation::InitializeValue_single_room(int a)
{
node *newNode;
newNode = new node;
newNode -> single_room = a;
newNode -> link = head;
head = newNode;
}
void reservation::InitializeValue_double_room(int b)
{
node *newNode;
newNode = new node;
newNode -> double_room = b;
newNode -> link = head;
head = newNode;
}
void reservation::InitializeValue_deluxe_suite(int c)
{
node *newNode;
newNode = new node;
newNode -> deluxe_suite= c;
newNode -> link = head;
head = newNode;
}
void reservation::InitializeValue_president_suite(int d)
{
node *newNode;
newNode = new node;
newNode -> president_suite = d;
newNode -> link = head;
head = newNode;
}
void reservation::menu()
{
cout <<" 1. Book a reservation" << endl;
cout <<" 2. Update a reservation" << endl;
cout <<" 3. Delete a resevation" << endl;
cout <<" 4. Display the reservation" << endl;
cout <<" 5. Help" << endl;
cout <<" 6. Exit" << endl << endl;
cout << "Please enter here: ";
}
But I want to print out the value in the Room_size(), it would print out this
Sorry not enough reputation to upload an image, I just describe here, it would came out|01704272||1704272||1704272||1704272||
1704272||1704272||1704272||1704272||1728736||101||102||103||104||105|
It would print out the numbers which I don't want, I only want 101,102,103,104,105 to view
void reservation::Room_size()
{
node guest;
node *current, *current1;
current = head;
current1 = head;
cout << " Single Room: " << endl;
cout << " ";
while (current != NULL)
{
cout << current -> single_room;
current = current -> link;
if(current != NULL)
cout << "|" << "|";
}
cout << "|" << endl;
cout << " Double Room: " << endl;
cout << " ";
while (current1 != NULL)
{
cout << current1 -> double_room;
current1 = current1 -> link;
if(current1 != NULL)
cout << "|" << "|";
}
cout << "|" << endl;
}
void reservation::InsertReservation()
{
node guest;
node *current;
current = head;
bool found;
found = false;
char b;
int c;
bool d = true;
bool f = false;
do{
ofstream file;
file.open("Guest info.txt", ios::out|ios::app);
if(!file)
{
cout << "ERROR: File can't open";
system("pause");
}
cout<<"Please enter your Name: ";
getline(cin,guest.name);
cout << endl;
while(!f){
cout << "Please enter your phone number: +";
cin >> guest.phone_number;
if(guest.phone_number < 10)//|| (cin >> guest.phone_number) < 11)
{
cout << "Wrong input! ";
f = false;
}
else{
f = true;
cin.ignore();
}
cout << endl;
}
cout << "Please enter the date you want to book: (20/04): ";
cin >> guest.date;
cout << endl;
cout << "Please enter the number of nights you want to stay: ";
cin >> guest.number_of_nights ;
cout << endl;
cout << "Please enter the time you want to check in (24:00 format): ";
cin >> guest.time;
cout << endl;
cout << "Please enter the number of people you have: ";
cin >> guest.number_of_pax;
while(d){
cout << "1.Single Room\n2.Double Room \n3.Luxury Suite \n4.President Suite\n\n";
cout << "Please select the room you want: ";
cin >> c;
if(c==1)
{
cout << " Rooms that are available: " << endl;
cout << " Single rooms: " << endl;
cout << " |" << guest.single_room << "|";
cout << endl << endl;
cout << "Please enter the room number you want to book: ";
cin >> guest.room_number;
d = false;
}else if(c==2)
{
cout << " Rooms that are available: " << endl;
cout << " Double rooms: " << endl;
cout << " |" << guest.double_room << "|";
cout << endl << endl;
cout << "Please enter the room number you want to book: ";
cin >> guest.room_number;
d = false;
}else if(c==3)
{
cout << " Rooms that are available: " << endl;
cout << " Luxury Suite: " << endl;
cout << " |" << guest.deluxe_suite << "|";
cout << endl << endl;
cout << "Please enter the room number you want to book: ";
cin >> guest.room_number;
d = false;
}else if (c==4)
{
cout << " Rooms that are available: " << endl;
cout << " President Suite" << endl;
cout << " |" << guest.president_suite << "|";
cout << endl << endl;
cout << "Please enter the room number you want to book: ";
cin >> guest.room_number;
d = false;
}else
{
cout << "Wrong input. Please try again";
d = true;
}
}
int e;
cout << "Press 1 to Save or 2 to cancel:";
cout << endl;
cin >> e;
if(e==1)
{
cout << endl;
file << "Guests info" << endl;
cout << endl;
file << "=================================================="<< endl;
file << endl;
file << "Room Number: " << guest.room_number;
file << endl;
file << "Name: " << guest.name << endl;
file << endl;
file << "Phone Number: " << guest.phone_number << endl;
file << endl;
file << "Date: " << guest.date << endl;
file << endl;
file << "Number of nights: " << guest.number_of_nights << endl;
file << endl;
file << "Time: " << guest.time << endl;
file << endl;
file << "Number of pax: " << guest.number_of_pax << endl;
file << endl;
file << endl;
file << "====================================================" << endl;
file.close();
cout << "Record Saved " << endl;
cout << "======================================================" << endl;
}else
{
cout << "Record was not saved " << endl;
cout << "====================================================" << endl;
cout << endl;
}
/* while ((current != NULL) && (!found))
{
if (current -> single_room == (guest.room_number))
found = true;
else
current = current -> link;
while(found)
{
cout << "ROOM NUMBER FOUND";
cout << endl;
}
cout << "NO";
cout << endl;
}*/
cout << "Enter (Y/y) to input another data or enter (N/n) to Exit";
cin >> b;
cout << "======================================================" <<
endl;
cout << endl;
}while(b == 'y' || b == 'Y');
if(b =='y'|| b=='Y')
{
cin.ignore();
}
}
void reservation::DisplayReservation()
{
node *current;
current = head;
cout << " |";
while (current != NULL)//) && current-> deluxe_suite <400))
{
cout << current -> deluxe_suite;
current = current -> link;
if(current != NULL)
cout << "| "<< "|";
}
cout << "|" << endl;
}
int main()
{
reservation r;
r.InitializeValue_single_room(105);
r.InitializeValue_single_room(104);
r.InitializeValue_single_room(103);
r.InitializeValue_single_room(102);
r.InitializeValue_single_room(101);
r.InitializeValue_double_room(204);
r.InitializeValue_double_room(203);
r.InitializeValue_double_room(202);
r.InitializeValue_double_room(201);
r.InitializeValue_deluxe_suite(303);
r.InitializeValue_deluxe_suite(302);
r.InitializeValue_deluxe_suite(301);
r.InitializeValue_president_suite(888);
r.InitializeValue_president_suite(666);
cout <<"================================================" << endl << endl;
cout <<" Welcome to the Hotel Reservation Application" << endl << endl;
cout <<"================================================" << endl;
int a;
cout << "Please wait while the system is analyzing hte data\n";
cout << endl;
cout<<"===================================" << endl;
cout <<" Main Menu " << endl << endl;
cout << " Rooms that are available: " << endl;
r.Room_size();
cout << endl << endl;
cout << "===================================" << endl;
r.menu();
cin >> a;
if(a == 1)
{
system("CLS");
cout << "===================================" << endl;
cout << " BOOK A RESERVATION" << endl;
cout << "===================================" << endl;
cin.ignore();
r.InsertReservation();
}else if(a == 2)
{
system("CLS");
cout << "===================================" << endl;
cout << " UPDATE YOUR RESERVATION" << endl;
cout << "===================================" << endl;
cin.ignore();
// r.UpdateReservation();
}else if(a == 4)
{
system("CLS");
cout <<"===================================" << endl;
cout << " VIEW ALL RESERVATION" << endl;
cout << "===================================" << endl;
cin.ignore();
r.DisplayReservation();
}
return 0;
}
I just ran your code.
What is happening is that you have single rooms, double, delux and president suite rooms in the same class, with the same list. It's not the best way to go, but it is a way to go.
This is the result that I got
Rooms that are available:
Single Room:
0||0||0||0||0||0||0||0||0||101||102||103||104||105|
Double Room:
0||0||0||0||0||201||202||203||204||0||0||-805306368||-805306368||-805306368|
You initialised the list as
r.InitializeValue_single_room(105);
r.InitializeValue_single_room(104);
r.InitializeValue_single_room(103);
r.InitializeValue_single_room(102);
r.InitializeValue_single_room(101);
r.InitializeValue_double_room(204);
r.InitializeValue_double_room(203);
r.InitializeValue_double_room(202);
r.InitializeValue_double_room(201);
r.InitializeValue_deluxe_suite(303);
r.InitializeValue_deluxe_suite(302);
r.InitializeValue_deluxe_suite(301);
r.InitializeValue_president_suite(888);
r.InitializeValue_president_suite(666);
Since the list is last in first out type, when you iterate through the list in the Room_size() function your function sees the following in the list
The first 2 rooms are president suites, where only president_suite attribute is set
The next 3 rooms are delux, where only deluxe_suite attribute is set
the next 4 rooms are double, where only double_room attribute is set
and the last 5 rooms are, where only single room attribute is set
So when it is printing single rooms, the first 9 nodes do not have the single room attribute set, as such it prints some garbage value, 5 times and then prints the correct single room number, and
Similarly for double room, it prints 5 garbage values and then prints the right numbers, and then 5 more garbage values.
to correct this problem you need to make the following changes.
1) In the class definition, use a single variable for room number, and another variable for room type.
2) Set room type constants using enum
enum room_types={single,double,delux,president_suite}
3) When a room is initialised via the InitializeValue_**, the function should set the room number from the arguments to the room number in the node, but should set room type attribute as per the enum. for example in the InitializeValue_single_room function
void reservation::InitializeValue_single_room(int a)
{
node *newNode;
newNode = new node;
newNode -> room_number = a;
newNode -> room_type = single;
newNode -> link = head;
head = newNode;
}
4) When you call the room_size() function, while printing the room types check for the room type. Example :
cout << " Single Room: " << endl;
cout << " ";
while (current != NULL)
{
if(current -> room_type !=single){
current = current -> link;
continue;
}
cout << current -> room_number;
current = current -> link;
if(current != NULL)
cout << "|" << "|";
}
I hope this helps.
There are much better ways to implement this, but I believe this is the best way for you for now.