greyfacemagilicuty
asked on
Linked List bug
A bug happens after deleting a record. I can't find where the problem is. Please help.
// Include files
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstdlib>
// Function prototypes
void PrintMenu (void);
// Classes
class LinkList
{
private:
class AppointmentNode
{
public:
string subject;
string location;
int dateYear;
int dateMonth;
int dateDay;
string startTime;
string finishTime;
string description;
AppointmentNode *next;
AppointmentNode (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc, AppointmentNode *n=NULL);
AppointmentNode();
~AppointmentNode();
};
AppointmentNode *head;
public:
LinkList();
~LinkList();
void AddAppointment (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc);
void AddToFront (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc);
void AddToBack (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc);
void AddToMiddle (AppointmentNode *previous, string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc);
void DeleteAppointment (string sub);
void PrintAppointment (int dm, int dd, int dy);
void PrintAllAppointments (void);
// Local variables
int choice, dm, dd, dy;
string temp, sub, loc, st, ft, desc;
// Main program
int main (void)
{
LinkList Appointments; // creates linked list
do
{
PrintMenu(); // prints menu to screen
cin >> choice; // input from user
if (choice == 1) // if choice is to add appointment
{
cout << endl << “ Enter the Subject: “;
getline (cin, temp);
getline (cin, sub);
cout << endl << “ Enter the Location: “;
getline (cin, loc);
cout << endl << “ Enter the Date (Month Day Year) with spaces between each: “;
cin >> dm >> dd >> dy;
getline (cin, temp);
cout << endl << “ Enter the Start Time: “;
getline (cin, st);
cout << endl << “ Enter the Finish Time: “;
getline (cin, ft);
cout << endl << “ Enter the Description: “;
getline (cin, desc);
Appointments.AddA ppointment (sub, loc, dm, dd, dy, st, ft, desc);
}
else if (choice == 2) // if choice is to delete an appointment
{
cout << endl << “ Enter the Subject of the Appointment to Delete: “;
getline (cin, temp);
getline (cin, sub);
Appointments.Dele teAppointm ent (sub);
}
else if (choice ==3) // if choice is to print a date
{
cout << endl << “ Enter the Date (Month Day Year) with space between each: “;
cin >> dm >> dd >> dy;
Appointments.Prin tAppointme nt (dm, dd, dy);
}
else if (choice == 4) // if choice is to print all
{
Appointments.Prin tAllAppoin tments();
}
}
while (choice != 5); // if choice is to exit
return(0);
}
// Functions
LinkList::LinkList()
{
head = NULL;
}
// LinkList class default constructor that creates a LinkList type class.
LinkList::~LinkList()
{
}
// LinkList class destructor that destroys the LinkList class specified.
LinkList::AppointmentNode: :Appointme ntNode()
{
subject = “0”;
location = “0”;
dateMonth = 1;
dateDay = 1;
dateYear = 1900;
startTime = “12:00 am”;
finishTime = “12:01 am”;
description = “0”;
next = NULL;
}
// AppointmentNode class default constructor that creates a new AppointmentNode.
LinkList::AppointmentNode: :~Appointm entNode()
{
}
// LinkList class destructor that destroys the AppointmentNode class specified.
LinkList::ApointmentNode:: Apointment Node (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc, AppointmentNode *r)
{
subject = sub;
location = loc;
dateMonth = dm;
dateDay = dd;
dateYear = dy;
startTime = st;
finishTime = ft;
description = desc;
next = r;
}
// AppointmentNode constructor that creates a new class of type AppointmentNode and fills it with the passed in values.
void LinkList::AddAppointment (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc)
{
AppointmentNode *current, *previous;
current = head;
previous = head;
while (current != NULL && ((dy*10000)+(dm*100)+dd)) > ((current->dateYear * 10000)+(current->dateMonth * 100)+(current->dateDay)))
{
previous = current;
current = current->next;
}
if (current == head)
{
AddToFront (sub, loc, dm, dd, dy, st, ft, desc);
}
else if (current == NULL)
{
AddToBack (sub, loc, dm, dd, dy, st, ft, desc);
}
else
{
AddToMiddle (previoud, sub, loc, dm, dd, dy, st, ft, desc);
}
cout << endl << “ -> Appointment Added <- “ << endl << endl;
}
// Determines where a new set of appointment data should be entered into the linked list by finding where it goes in chronological order. Then it calls the correct modifier function to enter the data into the linked list.
void LinkList::DeleteAppointmen t (string sub)
{
int flag = 0;
AppointmentNode *current, *previous, *currentfound, *previousfound;
currentfound = head;
previoudfound = head;
current = head;
previoud = head;
while (current != NULL)
{
if (sub == current->subject)
{
flag = 1;
previousfound = previous;
currentfound = current;
}
previous = current;
current = current->next;
}
if (flag == 0)
{
cout << “ Error. Appointment not found” << endl << endl;
{
else
{
previousfound->next = currentfound ->next;
delete currentfound;
cout << endl << “ -> Appointment Deleted <- “ << endl << endl;
}
}
// Searches for any appointments with the passed in subject and deletes the appointment. An error is displayed occurs if no appointment is found.
void LinkList::AddToFront (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc)
{
AppointmentNode *current;
current = head;
head = new AppointmentNode (sub, loc, dm, dd, dy, st, ft, desc, current);
if (head == NULL)
{
cout << “ Memory full “;
exit(1);
}
}
// Adds a new node to the linked list at the beginning of the linked list.
void LinkList::AddToBack (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc)
{
AppointmetnNode *current;
current = head;
while (current->next !=NULL)
{
current = current->next;
}
current->next = new AppointmentNode (sub, loc, dm, dd, dy, st, ft, desc, NULL);
if (current->next == NULL)
{
cout << “ Memory full “;
exit(1);
}
}
// Adds a new node to the linked list at the end of the linked list.
void LinkList::AddToMiddle(Appo intmentNod e *previoud, string sub, string, loc, int dm, int dd, int dy, string st, string ft, string desc)
{
AppointmentNode *newappointment;
Newappointment = new AppointmentNode (sub, loc, dm, dd, dy, st, ft, desc, previous->next);
if (newappointment == NULL)
{
cout << “ Memory full “;
}
else
{
previous->next = new appointment;
}
}
// Adds a new node to the linked list in the middle of the linked list.
void LinkList::PrintAppointment (int dm, int dd, int dy)
{
int flag = 0;
AppointmentNode *current;
current = head;
while (current !=NULL)
{
if(((dy*10000)+(dm*100 )+(dd)) == ((current->dateYear * 10000)+(current->dateMonth * 100)+(current->dateDay)))
{
flag++;
cout << endl << “Subject: “ << current->subject << endl;
cout << “Location: “ << current->location << endl;
cout << “Date: “ << current->dateMonth << “/” << current->dateDay << “/” << current->dateYear << endl;
cout << “StartTime: “ << current->startTime << endl;
cout << “FinishTime: “ << current->finishTime << endl;
cout << “Description: “ << current->description << endl;
}
current = current->next;
}
if (flag == 0)
{
cout << “Error. No Appointments found on that Date.” << endl << endl;
}
}
// Prints out to the screen any appointments that are on the passed in date. An error message is displayed if no appointments are found for the passed in date.
void LinkList:: PrintAllAppointments (void)
{
AppointmentNode *current;
current = head;
while (current != NULL)
{
cout << endl << “Subject: “ << current->subject << endl;
cout << “Location: “ << current->location << endl;
cout << “Date: “ << current->dateMonth << “/” << current->dateDay << “/” << current->dateYear << endl;
cout << “StartTime: “ << current->startTime << endl;
cout << “FinishTime: “ << current->finishTime << endl; cout << “Description: “ << current->description << endl;
current = current->>next;
}
}
// Prints out all the appointments and their information.
void PrintMenu (void)
{
cout << endl << 1. Add an Appointment” << endl << endl;
cout << “2. Delete an Appointment” << endl << endl;
cout << “3. Print Appointment(s)” << endl << endl;
cout << “4. Print all Appointments” << endl << endl;
cout << “5. Exit” << endl << endl;
cout << “Please Choose an Option: “;
}
// Prints the menu driven system to the screen.
// Include files
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstdlib>
// Function prototypes
void PrintMenu (void);
// Classes
class LinkList
{
private:
class AppointmentNode
{
public:
string subject;
string location;
int dateYear;
int dateMonth;
int dateDay;
string startTime;
string finishTime;
string description;
AppointmentNode *next;
AppointmentNode (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc, AppointmentNode *n=NULL);
AppointmentNode();
~AppointmentNode();
};
AppointmentNode *head;
public:
LinkList();
~LinkList();
void AddAppointment (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc);
void AddToFront (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc);
void AddToBack (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc);
void AddToMiddle (AppointmentNode *previous, string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc);
void DeleteAppointment (string sub);
void PrintAppointment (int dm, int dd, int dy);
void PrintAllAppointments (void);
// Local variables
int choice, dm, dd, dy;
string temp, sub, loc, st, ft, desc;
// Main program
int main (void)
{
LinkList Appointments; // creates linked list
do
{
PrintMenu(); // prints menu to screen
cin >> choice; // input from user
if (choice == 1) // if choice is to add appointment
{
cout << endl << “ Enter the Subject: “;
getline (cin, temp);
getline (cin, sub);
cout << endl << “ Enter the Location: “;
getline (cin, loc);
cout << endl << “ Enter the Date (Month Day Year) with spaces between each: “;
cin >> dm >> dd >> dy;
getline (cin, temp);
cout << endl << “ Enter the Start Time: “;
getline (cin, st);
cout << endl << “ Enter the Finish Time: “;
getline (cin, ft);
cout << endl << “ Enter the Description: “;
getline (cin, desc);
Appointments.AddA
}
else if (choice == 2) // if choice is to delete an appointment
{
cout << endl << “ Enter the Subject of the Appointment to Delete: “;
getline (cin, temp);
getline (cin, sub);
Appointments.Dele
}
else if (choice ==3) // if choice is to print a date
{
cout << endl << “ Enter the Date (Month Day Year) with space between each: “;
cin >> dm >> dd >> dy;
Appointments.Prin
}
else if (choice == 4) // if choice is to print all
{
Appointments.Prin
}
}
while (choice != 5); // if choice is to exit
return(0);
}
// Functions
LinkList::LinkList()
{
head = NULL;
}
// LinkList class default constructor that creates a LinkList type class.
LinkList::~LinkList()
{
}
// LinkList class destructor that destroys the LinkList class specified.
LinkList::AppointmentNode:
{
subject = “0”;
location = “0”;
dateMonth = 1;
dateDay = 1;
dateYear = 1900;
startTime = “12:00 am”;
finishTime = “12:01 am”;
description = “0”;
next = NULL;
}
// AppointmentNode class default constructor that creates a new AppointmentNode.
LinkList::AppointmentNode:
{
}
// LinkList class destructor that destroys the AppointmentNode class specified.
LinkList::ApointmentNode::
{
subject = sub;
location = loc;
dateMonth = dm;
dateDay = dd;
dateYear = dy;
startTime = st;
finishTime = ft;
description = desc;
next = r;
}
// AppointmentNode constructor that creates a new class of type AppointmentNode and fills it with the passed in values.
void LinkList::AddAppointment (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc)
{
AppointmentNode *current, *previous;
current = head;
previous = head;
while (current != NULL && ((dy*10000)+(dm*100)+dd)) > ((current->dateYear * 10000)+(current->dateMonth
{
previous = current;
current = current->next;
}
if (current == head)
{
AddToFront (sub, loc, dm, dd, dy, st, ft, desc);
}
else if (current == NULL)
{
AddToBack (sub, loc, dm, dd, dy, st, ft, desc);
}
else
{
AddToMiddle (previoud, sub, loc, dm, dd, dy, st, ft, desc);
}
cout << endl << “ -> Appointment Added <- “ << endl << endl;
}
// Determines where a new set of appointment data should be entered into the linked list by finding where it goes in chronological order. Then it calls the correct modifier function to enter the data into the linked list.
void LinkList::DeleteAppointmen
{
int flag = 0;
AppointmentNode *current, *previous, *currentfound, *previousfound;
currentfound = head;
previoudfound = head;
current = head;
previoud = head;
while (current != NULL)
{
if (sub == current->subject)
{
flag = 1;
previousfound = previous;
currentfound = current;
}
previous = current;
current = current->next;
}
if (flag == 0)
{
cout << “ Error. Appointment not found” << endl << endl;
{
else
{
previousfound->next = currentfound ->next;
delete currentfound;
cout << endl << “ -> Appointment Deleted <- “ << endl << endl;
}
}
// Searches for any appointments with the passed in subject and deletes the appointment. An error is displayed occurs if no appointment is found.
void LinkList::AddToFront (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc)
{
AppointmentNode *current;
current = head;
head = new AppointmentNode (sub, loc, dm, dd, dy, st, ft, desc, current);
if (head == NULL)
{
cout << “ Memory full “;
exit(1);
}
}
// Adds a new node to the linked list at the beginning of the linked list.
void LinkList::AddToBack (string sub, string loc, int dm, int dd, int dy, string st, string ft, string desc)
{
AppointmetnNode *current;
current = head;
while (current->next !=NULL)
{
current = current->next;
}
current->next = new AppointmentNode (sub, loc, dm, dd, dy, st, ft, desc, NULL);
if (current->next == NULL)
{
cout << “ Memory full “;
exit(1);
}
}
// Adds a new node to the linked list at the end of the linked list.
void LinkList::AddToMiddle(Appo
{
AppointmentNode *newappointment;
Newappointment = new AppointmentNode (sub, loc, dm, dd, dy, st, ft, desc, previous->next);
if (newappointment == NULL)
{
cout << “ Memory full “;
}
else
{
previous->next = new appointment;
}
}
// Adds a new node to the linked list in the middle of the linked list.
void LinkList::PrintAppointment
{
int flag = 0;
AppointmentNode *current;
current = head;
while (current !=NULL)
{
if(((dy*10000)+(dm*100
{
flag++;
cout << endl << “Subject: “ << current->subject << endl;
cout << “Location: “ << current->location << endl;
cout << “Date: “ << current->dateMonth << “/” << current->dateDay << “/” << current->dateYear << endl;
cout << “StartTime: “ << current->startTime << endl;
cout << “FinishTime: “ << current->finishTime << endl;
cout << “Description: “ << current->description << endl;
}
current = current->next;
}
if (flag == 0)
{
cout << “Error. No Appointments found on that Date.” << endl << endl;
}
}
// Prints out to the screen any appointments that are on the passed in date. An error message is displayed if no appointments are found for the passed in date.
void LinkList:: PrintAllAppointments (void)
{
AppointmentNode *current;
current = head;
while (current != NULL)
{
cout << endl << “Subject: “ << current->subject << endl;
cout << “Location: “ << current->location << endl;
cout << “Date: “ << current->dateMonth << “/” << current->dateDay << “/” << current->dateYear << endl;
cout << “StartTime: “ << current->startTime << endl;
cout << “FinishTime: “ << current->finishTime << endl; cout << “Description: “ << current->description << endl;
current = current->>next;
}
}
// Prints out all the appointments and their information.
void PrintMenu (void)
{
cout << endl << 1. Add an Appointment” << endl << endl;
cout << “2. Delete an Appointment” << endl << endl;
cout << “3. Print Appointment(s)” << endl << endl;
cout << “4. Print all Appointments” << endl << endl;
cout << “5. Exit” << endl << endl;
cout << “Please Choose an Option: “;
}
// Prints the menu driven system to the screen.
When deleting the first thing in the list, you need to change the head pointer. As it is, it will delete the node and leave the head pointer pointing to it.
It might be helpful if you could provide more information about how it fails.
It might be helpful if you could provide more information about how it fails.
yeh I agree with efn, you need to mark your place when deleting the nodes
if( currentnode == Head)
head = currentNode->NextItem
delete currentnode
if your deleting form the mddle do something like:
Temp = currentNode
if your doubly linked do:
CurrentNode->PrevNode->Nex tNode = CurrentNode->NextNode->Pre vNode;
CurrentNode = CurrentNode->NextNode;
delete temp;
CurrentNode->PrevNode->Nex tNode = CurrentNode->NextNode->Pre vNode;
the above line relinks the nodes
<Node A > <current Node> <Node C>
so the Next pointer in Node a will now link to Node C and The previous pointer in Node C will link to Node A
now Temp(which is CurentNode) can be delete AFTER you have sent current to a valid node
if( currentnode == Head)
head = currentNode->NextItem
delete currentnode
if your deleting form the mddle do something like:
Temp = currentNode
if your doubly linked do:
CurrentNode->PrevNode->Nex
CurrentNode = CurrentNode->NextNode;
delete temp;
CurrentNode->PrevNode->Nex
the above line relinks the nodes
<Node A > <current Node> <Node C>
so the Next pointer in Node a will now link to Node C and The previous pointer in Node C will link to Node A
now Temp(which is CurentNode) can be delete AFTER you have sent current to a valid node
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
though of something else..
void LinkList::DeleteAppointmen t (string sub)
{
int flag = 0;
AppointmentNode *current, *previous, *currentfound, *previousfound;
currentfound = NULL;
previoudfound = NULL;
current = head;
previous = head;
while (current != NULL)
{
if (sub == current->subject)
{
flag = 1;
previousfound = previous;
currentfound = current;
break; // found - no need to waste cpu time
}
previous = current;
current = current->next;
}
if (flag == 0)
{
cout << “ Error. Appointment not found” << endl << endl;
{
else
{
previousfound->next = currentfound ->next;
delete currentfound;
if( previousfound == currentfound) // if we deleted the only record in the list and the list is now empty
head = NULL;
else
{
if( head == currentfound)
head = previousfound; // if the head was deleted
}
cout << endl << “ -> Appointment Deleted <- “ << endl << endl;
}
}
void LinkList::DeleteAppointmen
{
int flag = 0;
AppointmentNode *current, *previous, *currentfound, *previousfound;
currentfound = NULL;
previoudfound = NULL;
current = head;
previous = head;
while (current != NULL)
{
if (sub == current->subject)
{
flag = 1;
previousfound = previous;
currentfound = current;
break; // found - no need to waste cpu time
}
previous = current;
current = current->next;
}
if (flag == 0)
{
cout << “ Error. Appointment not found” << endl << endl;
{
else
{
previousfound->next = currentfound ->next;
delete currentfound;
if( previousfound == currentfound) // if we deleted the only record in the list and the list is now empty
head = NULL;
else
{
if( head == currentfound)
head = previousfound; // if the head was deleted
}
cout << endl << “ -> Appointment Deleted <- “ << endl << endl;
}
}
ASKER