I have written a function to relink and delete a node from a doubly linked list data structure. I keep getting exceptions thrown in relation to failing to access memory.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
//const int MAXRECS = 3; // maximum number of records
struct TeleType
{
string name;
string phoneNo;
TeleType *nextaddr;
TeleType *prevaddr;
};
void populate(TeleType *); // function prototype needed by main()
void display(TeleType *); // function prototype needed by main()
void remove_head(TeleType *); //function prototype for main()
void remove_current(TeleType *); //function prototype for main()
void remove_tail(TeleType *); //function prototype for main()
void make_list();
TeleType head, tail, current;
TeleType *phead = &head;
TeleType *pcurrent = ¤t;
TeleType *ptail = &tail; // 3 pointers to structures of
// type TeleType
// get a pointer to the first structure in the list
int main()
{
int i;
cout << "The list will now be made" << endl;
make_list();
cout << "\nThe list consists of the following records:\n";
display(phead); // display the structures
cout << "Which record would you like to remove?" << endl;
cout << "select 1 if you want to remove the head, 2 for the middle and 3 for the tail" << endl;
cin >> i;
switch (i)
{
case 1:
remove_head(phead);
display(pcurrent);
case 2:
remove_current(pcurrent);
display(phead);
case 3:
remove_tail(pcurrent);
break;
}
system("pause");
return 0;
}
void make_list()
{
//Input values for head structure
populate(phead);
//Assign nextaddr pointer to current
head.nextaddr = pcurrent;
//Input values for current structure
populate(pcurrent);
//Assign current structure's newaddr and preaddr
current.prevaddr = phead;
current.nextaddr = ptail;
//Input values for tail
populate(ptail);
//Assign prevaddr pointer
tail.prevaddr = pcurrent;
//assign newaddr pointer as null to tail structure
tail.nextaddr = NULL;
// assign prevaddr pointer as current pointer for tail structure
return;
}
// input a name and phone number
void populate(TeleType *record) // record is a pointer to a
{ // structure of type TeleType
cout << "Enter a name: ";
getline(cin, record->name);
cout << "Enter the phone number: ";
getline(cin, record->phoneNo);
return;
}
void display(TeleType *contents) // contents is a pointer to a
{ // structure of type TeleType
while (contents != NULL) // display until end of linked list
{
cout << endl << setiosflags(ios::left)
<< setw(30) << contents->name
<< setw(20) << contents->phoneNo
<< setw(20) << contents->prevaddr
<< setw(20) << contents->nextaddr;
contents = contents->nextaddr;
}
cout << endl;
return;
}
void remove_head(TeleType *record)
{
current.prevaddr = NULL;
current.nextaddr = ptail;
tail.prevaddr = pcurrent;
tail.nextaddr = NULL;
delete record;
return;
}
void remove_current(TeleType *record)
{
head.nextaddr = ptail;
tail.prevaddr = phead;
tail.nextaddr = NULL;
delete record;
return;
}
void remove_tail(TeleType *record)
{
}
foo.cpp: In function ‘int main()’:
foo.cpp:51:11: warning: this statement may fall through [-Wimplicit-fallthrough=]
foo.cpp:54:11: warning: this statement may fall through [-Wimplicit-fallthrough=]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
switch (i)
{
case 1:
remove_head(phead);
display(pcurrent);
break; //<--- add this
case 2:
remove_current(pcurrent);
display(phead);
break; //<--- add this
case 3:
remove_tail(pcurrent);
break;
}
without the breaks, the code keeps executing.
if you keep having issues, provide an example input.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
struct TeleType
{
string name;
string phoneNo;
TeleType *nextaddr;
TeleType *prevaddr;
};
void populate(TeleType *); // function prototype needed by main()
void display(TeleType *); // function prototype needed by main()
void remove_head(TeleType *); //function prototype for main()
void remove_current(TeleType *); //function prototype for main()
void remove_tail(TeleType *); //function prototype for main()
void make_list();
void insert(TeleType *);
TeleType head, tail, current;
TeleType *phead = &head;
TeleType *pcurrent = ¤t;
TeleType *ptail = &tail; // 3 pointers to structures of
// type TeleType
// get a pointer to the first structure in the list
int main()
{
int i;
cout << "The list will now be made" << endl;
make_list();
cout << "\nThe list consists of the following records:\n";
display(phead); // display the structures
cout << "Which record would you like to remove?" << endl;
cout << "select 1 if you want to remove the head, 2 for the middle and 3 for the tail, 4 to insert a new record" << endl;
cin >> i;
cout << endl;
switch (i)
{
case 1:
remove_head(phead);
display(pcurrent);
break;
case 2:
remove_current(pcurrent);
display(phead);
break;
case 3:
remove_tail(pcurrent);
display(phead);
break;
case 4:
insert(ptail);
display(phead);
break;
}
system("pause");
return 0;
}
void make_list()
{
//Input values for head structure
populate(phead);
//Assign nextaddr pointer to current
head.nextaddr = pcurrent;
//Input values for current structure
populate(pcurrent);
//Assign current structure's newaddr and preaddr
current.prevaddr = phead;
current.nextaddr = ptail;
//Input values for tail
populate(ptail);
//Assign prevaddr pointer
tail.prevaddr = pcurrent;
//assign newaddr pointer as null to tail structure
tail.nextaddr = NULL;
// assign prevaddr pointer as current pointer for tail structure
return;
}
// input a name and phone number
void populate(TeleType *record) // record is a pointer to a
{ // structure of type TeleType
cout << "Enter a name: " << endl;
getline(cin, record->name);
cout << "Enter the phone number: ";
getline(cin, record->phoneNo);
return;
}
void display(TeleType *contents) // contents is a pointer to a
{ // structure of type TeleType
while (contents != NULL) // display until end of linked list
{
cout << endl << setiosflags(ios::left)
<< setw(30) << contents->name
<< setw(20) << contents->phoneNo
<< setw(20) << contents->prevaddr
<< setw(20) << contents->nextaddr;
contents = contents->nextaddr;
}
cout << endl;
return;
}
void remove_head(TeleType *record)
{
current.prevaddr = NULL;
current.nextaddr = ptail;
tail.prevaddr = pcurrent;
tail.nextaddr = NULL;
return;
}
void remove_current(TeleType *record)
{
head.nextaddr = ptail;
tail.prevaddr = phead;
tail.nextaddr = NULL;
return;
}
void remove_tail(TeleType *record)
{
current.nextaddr = NULL;
current.prevaddr = phead;
return;
}
void insert(TeleType *record)
{
TeleType *ptr_new_record = new TeleType; //create a new structure, return an address and assign it to a ptr
//new record.
record->nextaddr = ptr_new_record; //change the nextaddr pointer to point to the new record
ptr_new_record->nextaddr = NULL; //assign new null pointer
ptr_new_record->prevaddr = ptail; //assign new pointer of new record to point to the the old tail structure.
populate(ptr_new_record); //call populate function to fill in data members of the structure.
return;
}