C++ bidirectional linked list to achieve a simple address book
- 2020-09-16 07:39:38
- OfStack
This article Shared C++ bidirectional linked list for everyone to achieve simple address book specific code, for your reference, the specific content is as follows
#include<iostream>
#include<fstream>
#include <stdlib.h>
#include<string>
using namespace std;
typedef struct Directory
{
string Name;
string Mobile;
string Wechatnumber;
string STREET;
string CITY;
string EIP;
string STATE;
struct Directory* next;
struct Directory* prev;
}Directory;
// Header node initialization
Directory p0 = {"0","0","0","0","0","0","0",NULL,NULL};
Directory pn = {"0","0","0","0","0","0","0",NULL,NULL};
// Sets the header pointer to the header node
Directory *head = &p0;
// Function declaration
void enter(Directory*);
void display_list();
void printf_a();
void display_menu(Directory*);
int key_ramove(string);
void display_listfiile();
Directory* find_load(string);
Directory* load();
int main()
{
cout<<"========================================"<<endl;
cout<<"= tong - record ="<<endl;
cout<<"= ="<<endl;
cout<<"= ="<<endl;
cout<<"= 1. add 2. delete 3. To find the 4. View address book ="<<endl;
cout<<"========================================"<<endl;
int i = 0; // Define key variables to store key values
string key; // Defines a string variable to hold the input string
p0.next = &pn; // These two sentences initialize the heads and tails, and connect them together
pn.prev = &p0;
while(1)
{
cin>>i; // The input i , for which function to select
switch(i){ // Choice, i Equals something, just execute it case A few
case 1:
load(); // Create a new node and insert the linked list
cout<<endl;
cout<<" Add complete!! "<<endl;
printf_a();
break;
case 2:
cout<<" Please enter your name: ";
cin>>key;
key_ramove(key); // To delete a node is to delete 1 Personal Information
printf_a();
break;
case 3:
cout<<" Please enter your name: ";
cin>>key;
display_menu(find_load(key)); // Print the information within the find node, find_load(key) The traversal function used to find the node
printf_a(); /
break;
case 4:
display_list(); // Prints the names of all nodes
printf_a();
break;
case 5:
display_listfiile(); // Input the information of all nodes to "address.txt" file
break;
default:
break;
}
}
return 0;
}
/**************************************************
* Return type: void
* The print menu () function prints a menu
***************************************************/
void printf_a()
{
cout<<"----------------------------------------"<<endl;
cout<<"- 1. add 2. delete 3. To find the 4. View address book -"<<endl;
cout<<"- 5. export txt The document -"<<endl;
cout<<"----------------------------------------"<<endl;
}
/**************************************************
* Return type: Directory*
* The new node insert () function inserts a new node into a linked list
***************************************************/
Directory* load()
{
Directory *p = new Directory; // Allocate space to this new node
enter(p);
p->next = head->next; //p Under the 1 A pointer to the head points below the node 1 a
head->next = p; // The head pointer points below the node 1 A point to p
p->prev = head; //p On the 1 A pointer to the next node 1 a
p->next->prev = p;
head = p; // Head pointer to p
return p;
}
/**************************************************
* Return type: void
* The single node lookup () function finds a single node
* Pass in parameter: name 10 points
***************************************************/
Directory* find_load(string key_name)
{
Directory *p;
p = &pn;
for(p; p->prev != NULL ;p = p->prev)
{
if(p->Name == key_name )
{
return p;
}
}
return NULL;
}
/**************************************************
* Return type: void
* The delete () function removes a single node
* Pass in parameter: name 15 points
***************************************************/
int key_ramove(string key_name)
{
Directory *p; // Defines a pointer to a struct type
p = &pn;
for(p; p->prev != NULL;p = p->prev)
{
if(p->Name == key_name )
{
head = pn.prev;
p->prev->next = p->next; //p On the 1 A lower 1 A point to p Under the 1 a
p->next->prev = p->prev; //p Under the 1 On the 1 A point to p On the 1 a
free(p); // The release of p The space of
return 0; // After deletion, exit the function
}
}
cout<<" There is no such person!! "<<endl;
return 0;
}
/**************************************************
* Return type: void
* The single node input () function
* Pass in parameters: Directtory A pointer to a self-defined structure, 5 points
***************************************************/
void enter(Directory *P )
{
char jubge; // The variables used to judge
string name, mobile;
cout<<" Enter name: ";
cin>>name;
P->Name = name; // Put the input string on the node to which the current pointer is pointing
cout<<" Input telephone: ";
cin>>mobile;
P->Mobile = mobile; // Put the input string on the node to which the current pointer is pointing
cout<<" Is the information complete? ( Y/N ) "<<endl;
cin>>jubge;
if(jubge == 'y' || jubge == 'Y')
{
string wechatnumber; // WeChat
string street; // The street
string city; // city
string eip; // Zip code
string state; // countries
cout<<" WeChat: ";
cin>>wechatnumber;
P->Wechatnumber = wechatnumber;
cout<<" Street: ";
cin>>street;
P->STREET = street;
cout<<" City: ";
cin>>city;
P->CITY = city;
cout<<" Countries: ";
cin>>state;
P->STATE = state;
}else{ // In addition to the input y We do this everywhere else
P->Wechatnumber = "NULL";
P->STREET = "NULL";
P->CITY = "NULL";
P->STATE = "china";
}
}
/**************************************************
* Return type: void
* The address book (owner's name, in reverse order) is printed.
***************************************************/
void display_list()
{
Directory *p; // Defines a pointer to a struct type
p = head; // let p Is equal to the current head The node indicated by the pointer
int i = 1; // According to the serial number
cout<<endl;
cout<<"*******************************************"<<endl;
cout<<" tong - record "<<endl;
cout<<"------------------------------------------"<<endl;
while(p->prev != NULL) // Loop, know p Node-Pointing prev Pointer to NULL
{
cout<<" "<<i<<": "<<p->Name<<endl; // print p The name of the node that corresponds to the pointer
cout<<"------------------------------------------"<<endl;
p = p->prev; // Pointer pointing up 1 A node
i++;
}
cout<<"*******************************************"<<endl;
cout<<endl;
}
void display_listfiile()
{
Directory *p; // Defines a pointer to a struct type
p = &pn; // let p Is equal to the current head The node indicated by the pointer
int i = 1; // According to the serial number
ofstream fout("address.txt"); // Open the address.txt The file was not automatically created
while(p->prev != NULL) // Loop, know p Node-Pointing prev Pointer to NULL
{
fout << i <<":" << p->Name <<endl; // Outputs the name of the current node to address.txt file
fout <<" " << p->Mobile <<endl;
fout <<" " << p->Wechatnumber <<endl;
fout <<" " << p->STREET <<endl;
fout <<" " << p->CITY <<endl;
fout <<" " << p->STATE <<endl;
fout <<endl;
p = p->prev; // Pointer pointing up 1 A node
i++;
}
fout.close(); // Close file flow
}
/**************************************************
* The return type Directory
* The PrintDetails () function prints details
***************************************************/
void display_menu(Directory *P)
{
if(P == NULL) // judge if P If null, the person is not printed
{
cout<<" No one!! "<<endl;
}else{
cout<<"* * * * * * * * * * * * * * * * * * * * "<<endl;
cout<<"* The name : "<<P->Name<<endl;
cout<<"* The phone : "<<P->Mobile<<endl;
cout<<"* WeChat : "<<P->Wechatnumber<<endl;
cout<<"* The street : "<<P->STREET<<endl;
cout<<"* city : "<<P->CITY<<endl;
cout<<"* countries :"<<P->STATE<<endl;
cout<<"* * * * * * * * * * * * * * * * * * * * "<<endl;
}
}