I am writing a simple program that works as a telephone directory but in a doubly linked list format. I am unsure why I am only able to display 2 items after calling my make list function and showing no data members. But when I call my insert function everything works fine!
The list will now be made
How many records would you like?
2
Enter a name:
1
Enter the phone number: 1
Enter a name:
2
Enter the phone number: 2
The list consists of the following records:
Names Phone number Previous mem addr next mem addr 00000000 013D4370
Names Phone number Previous mem addr next mem addr 013D4370 00000000
Which record would you like to remove?
1 if you want to remove the head
2 for the middle
3 for the tail
4 to insert a new record
5 to find a record
6 Display records
4
Enter a name:
3
3
Enter the phone number: 3
Names Phone number Previous mem addr next mem addr 00000000 013D4370
Names Phone number Previous mem addr next mem addr 013D4370 008C4318
Names Phone number Previous mem addr next mem addr 3
// Exercise13.5_4revised.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <list>
usingnamespace std;
struct TeleType
{
string name;
string phoneNo;
TeleType *nextaddr;
TeleType *prevaddr;
};
void populate(TeleType *); // function prototype needed by main()
void make_head_tail(TeleType *);
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 *);
void push_front(TeleType *);
void push_back(TeleType *);
TeleType *findRec(string, TeleType *);
TeleType head, current, tail;
TeleType *phead = &head;
TeleType *pcurrent = ¤t;
TeleType *ptail = &tail;
int main()
{
int i;
string search_term;
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 << "1 if you want to remove the head" << endl;
cout << "2 for the middle " << endl;
cout << "3 for the tail" << endl;
cout << "4 to insert a new record "<< endl;
cout << "5 to find a record" << endl;
cout << "6 Display records" << endl;
cin >> i;
cin.ignore();
cout << endl;
switch (i)
{
case 1:
remove_head(phead);
display(phead);
break;
case 2:
remove_current(pcurrent);
display(phead);
break;
case 3:
remove_tail(pcurrent);
display(phead);
break;
case 4:
insert(ptail);
display(phead);
break;
case 5:
cout << "What are you looking for? " << endl;
getline(cin, search_term);
cin.ignore();
cout << "The address matching is " << findRec(search_term, phead) << endl;
break;
case 6:
display(phead);
break;
}
system("pause");
return 0;
}
void make_head_tail(TeleType *record)
{
populate(phead);
head.prevaddr = nullptr;
head.nextaddr = ptail;
tail.prevaddr = phead;
tail.nextaddr = nullptr;
}
void make_list()
{
//Setting the head of the linked list
//assign head to a new structure
//Call populate function to fill in data
//set nextaddr and prevaddr pointers to null
//assign tail with head
int number_of_records = 1;
cout << "How many records would you like?" << endl;
cin >> number_of_records;
//populate head with data
//assign prev addr for head as a null
//assign next addr for head as tail;
make_head_tail(phead);
for (int i = 0; i < number_of_records - 1; i++)
{
insert(ptail);
//populate(ptail); //call populate function to fill in data members of the structure.
push_back(pcurrent);
tail = current;
}
//set up a for loop to make records
//make a new node assigned to current structure
//call populate function to fill in data members
//assign a pointer to to current's nextaddr as NULl
//assign pointer prevaddr of current as the pointer of head.
//assign tail to current
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);
cin.ignore();
cout << "Enter the phone number: ";
getline(cin, record->phoneNo);
cin.ignore();
return;
}
void insert(TeleType *record)
{
TeleType *pcurrent = new TeleType; //create a new structure, return an address and assign it to a ptr
//new record.
record->nextaddr = pcurrent; //change the nextaddr pointer to point to the new record
populate(pcurrent); //populate the members of the new record.
pcurrent->nextaddr = NULL; //assign new null pointer
pcurrent->prevaddr = record; //assign new pointer of new record to point to the the old tail structure.
return;
}
void push_back(TeleType *record)
{
record->nextaddr = NULL;
record->prevaddr = ptail;
ptail->nextaddr = pcurrent;
tail = current;
//assign the next addr pointer as null
//assign the prevaddr pointer to point to the tail
//assign the pointer of tail for next addr to to point to current.
//assign tail as current.
}
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(10) << "Names" << setw(30) << "Phone number" << setw(30) << "Previous mem addr" << setw(30) << "next mem addr"
<< setw(10) << contents->name
<< setw(10) << contents->phoneNo
<< setw(10) << contents->prevaddr
<< setw(10) << contents->nextaddr;
contents = contents->nextaddr;
}
cout << endl;
return;
}
void remove_head(TeleType *record)
{
return;
}
void remove_current(TeleType *record)
{
return;
}
void remove_tail(TeleType *record)
{
return;
}
TeleType *findRec(string term, TeleType *contents)
{
TeleType *pointer_to_address = phead;
while (contents != NULL)
{
if (term == contents->name)
{
pointer_to_address = contents;
break;
}
else
{
cout << "match not found" << endl;
}
contents = contents->nextaddr;
}
return pointer_to_address;
}