I am using an insert function to add a node to a doubly linked list after the tail node. When I call a populate function to fill the data members of the structures, I can only input 1 for one member of the structure and not the other.
[output]
The list will now be made
Enter a name:
1
Enter the phone number: 1
Enter a name:
2
Enter the phone number: 2
Enter a name:
3
Enter the phone number: 3
The list consists of the following records:
1 1 00000000 00E63370
2 2 00E632F0 00E63330
3 3 00E63370 00000000
Which record would you like to remove?
select 1 if you want to remove the head, 2 for the middle and 3 for the tail, 4 to insert a new record
4
#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;
}
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;
cin.ignore(); //<--- add this
you pressed <Return> when you input your choice, so the input buffer contains "4\n", `i' gets the 4, but the "\n" remains in the input buffer, so when you do getline(cin, record->name); in `populate()' the name will have an empty string.
cin.ignore() will discard that line break.
Your linked list handling code doesn't look right to me. Normally head and tail are just pointers to the first and last items in the list. The items are allocated dynamically. You have allocated global nodes for the head and tail. So what should happen if you try to delete the head?
Why do you have a "current" node? I understand the need for a current pointer, but not a current node. The "current" node will be different ones depending on what you're doing.
These problems bleed into remove_head(), remove_current() and remove_tail(). None of them do anything with the record parameter.
My advice is to think about what these functions are supposed to do. Write it down as a comment before the function and the make the code do what the function is supposed to do.