📄 5_5.cpp
字号:
current_ptr = current_ptr->next;
}
if(current_ptr != NULL) // If current_ptr is not NULL, then match was
{ // found.
cout << "\nRECORD FOUND\n";
cout << current_ptr->first_name << ' '
<< current_ptr->last_name << endl;
cout << current_ptr->phone_num << endl;
}
else
{
cout << "NO MATCH FOUND\n";
cout << "Press Enter to Contiune\n";
cin.get(pause);
system("cls");
}
} // end of function search_by_lastname
// Function that deletes individual nodes from the linked list.
void delete_record()
{
system("cls");
char search_string[20];
friend_node *previous_ptr;
previous_ptr = NULL; // Initialize previous_ptr to NULL.
current_ptr = head_ptr; // Move current_ptr to head of list
// to begin search.
cin.ignore(20,'\n');
cout << "\nEnter the last name of the friend you want to delete: ";
cin.get(search_string,20);
cin.ignore(20,'\n');
// Loop to find matching record.
while((current_ptr != NULL) &&
(strcmp(current_ptr->last_name, search_string) != 0))
{
previous_ptr = current_ptr; // A pointer must be maintained that
current_ptr = current_ptr->next; // points to the node before the node
} // to be deleted.
if(current_ptr != NULL) // If current_ptr is not NULL, then match was
{ // found.
cout << "\nRECORD FOUND\n";
cout << current_ptr->first_name << ' '
<< current_ptr->last_name << endl;
cout << current_ptr->phone_num << endl;
if(verify_delete()) // Ask user if he/she wants to delete the record.
{ // If user wants to delete the record,
delete_node(previous_ptr); // delete the node that follows the
cout << "\nRECORD DELETED\n"; // one pointed to by previous_ptr.
}
else // Otherwise, do nothing.
{
cout << "\nRECORD NOT DELETED\n";
}
}
else // If no match for the record found, display message.
{
cout << "\nNO MATCH FOUND. NO RECORD DELETED.\n";
}
system("cls");
} // end of function delete_record
//Function that delete's the file for reset.
void help_me()
{
help:
int choice2;
system("cls");
cout << "Welcome to help please choose help type\n";
cout << "1: What happened to my records\n";
cout << "2: How do I clear all records\n";
cout << "3: When will there be a new version\n";
cout << "4: What will be in the new versions\n";
cout << "5: Exit\n";
cin >> choice2;
switch(choice2) //Menu for help
{
case 1:
cout << "Check the directory with the executable for a file named Friends.dat\n";
cout << "If its not there its gone forever if renamed rename it to Friends.dat\n";
cout << "Press enter to contiune\n";
pause=getch();
system("cls");
goto help;
break;
case 2:
cout << "Delete The file Friends.dat\n";
cout << "Press Enter to contiune\n";
pause=getch();
system("cls");
goto help;
break;
case 3:
cout << "When I get time\n";
cout << "Press Enter to contiune\n";
pause=getch();
system("cls");
goto help;
break;
case 4:
cout << "I hope to add the following\n";
cout << "Multi User's and passwords\n";
cout << "Encryption \n";
cout << "Clear all records command\n";
cout << "Bug fixes\n";
cout << "Press Enter to contiune\n";
pause=getch();
system("cls");
goto help;
break;
default:
cout << "Press Enter To exit";
cout << "Press Enter to contiune\n";
break;
}
} // end of function delete_all
// Function to ask user to verify intention to delete the node.
int verify_delete()
{
char YesNo;
cout << "\nAre you sure (Y/N) ";
cin >> YesNo;
if((YesNo == 'Y') || (YesNo == 'y'))
{
return(1); // Return TRUE if user want to delete.
}
else
{
return(0); // Return FALSE if user does not want to delete.
}
} // end of function verify_delete
// Function that deletes node pointed to by current_ptr.
void delete_node(friend_node *previous_ptr)
{
if(current_ptr == head_ptr) // If node to be deleted is the head of the
{ // list, call a special function that
delete_head_of_list(); // deletes the first node in the list.
}
else
{ // Otherwise:
if(current_ptr->next == NULL) // If node to be deleted is at the
{ // end of the list, call a special
delete_end_of_list(previous_ptr); // function to delete that node.
}
else // Otherwise:
{ // Delete the node from the
delete_from_middle_of_list(previous_ptr); // middle of the list using
} // a function that does that.
}
} // end of function delete_node
//Function that deletes the head of the list.
void delete_head_of_list()
{
current_ptr = head_ptr; // Make current_ptr point to the head of the list.
if(head_ptr->next != NULL)
{ // If more than one node is in the list,
head_ptr = current_ptr->next; // make second node in list the new head.
}
else // Otherwise, just set head_ptr to NULL
{ // to signal that the list is empty.
head_ptr = NULL;
}
delete current_ptr; // Deallocate memory used by the deleted node.
} // end of function delete_head_of_list
// Function that deletes the last node of the linked list.
void delete_end_of_list(friend_node *previous_ptr)
{
delete current_ptr; // Deallocate memory used by the deleted node.
previous_ptr->next = NULL; // Make node before deleted node the end of list.
current_ptr = head_ptr; // Set current_ptr to head to give it a value.
} // end of function delete_end_of_list
// Function that deletes a node from the middle of the list.
void delete_from_middle_of_list(friend_node *previous_ptr)
{
// Set pointers of the nodes before and after the node to be deleted to
// skip the node that is to be deleted.
previous_ptr->next = current_ptr->next;
delete current_ptr; // Deallocate memory used by the deleted node.
current_ptr = head_ptr; // Set current_ptr to head to give it a value.
} // end of function delete_from_middle_of_list
// Function that frees the memory used by the linked list.
void delete_list()
{
friend_node *temp_ptr; // pointer used for temporary storage
current_ptr = head_ptr; // Move current_ptr to head of the list.
do // Traverse list, deleting as we go.
{
temp_ptr = current_ptr->next; // Set temporary pointer to point
// to the remainder of the list.
delete current_ptr; // Delete current node.
current_ptr = temp_ptr; // Set current_ptr to next node after the
} while(temp_ptr != NULL); // deleted one.
} // end of function delete_list
// Function to write linked list data to the data file.
void write_list_to_file()
{
ofstream outfile; // output file pointer
outfile.open("FRIENDS.DAT",ios::out); // Open file for output.
if (outfile) // If no error occurred while opening the file,
{ // it is okay to write the data to the file.
current_ptr = head_ptr; // Set current_ptr to head of list.
if(head_ptr != NULL) // If the list is not empty, begin
{ // writing data to the file.
do // Traverse list until the end is reached.
{
// Write the nodes data to the file.
outfile << current_ptr->last_name << endl;
outfile << current_ptr->first_name << endl;
outfile << current_ptr->phone_num << endl;
current_ptr = current_ptr->next; // Move current_ptr to next node.
} while(current_ptr != NULL); // Loop until end of list is reached.
}
// The word END OF FILE are written to the end of the file to make it
// easy to locate the end of the file when the data is read back in.
outfile << "END OF FILE" << endl;
outfile.close(); // Close the file.
}
else // If an error occurs while opening the file, display a message.
{
cout << "Error opening file.\n";
}
} // end of function write_list_to_file
// Function to load the linked list from the data file.
void load_list_from_file() // 从数据文件FRIENDS.DAT中读取数据重建链表处理函数
{
friend_node *new_rec_ptr;
ifstream infile; // input file pointer
int end_loop = 0;
infile.open("FRIENDS.DAT",ios::in); // Open file for input.
if (infile) // If no error occurred while opening file
{ // input the data from the file.
do
{
new_rec_ptr = new friend_node; // Allocate memory for a node.
if(new_rec_ptr != NULL) // Check for allocation error.
{
// Get the next last name from the file.
infile.get(new_rec_ptr->last_name,20);
infile.ignore(20,'\n');
// If the end of the file has not yet been reached, get other data.
if((strcmp(new_rec_ptr->last_name, "") != 0) &&
(strcmp(new_rec_ptr->last_name, "END OF FILE") != 0))
{
infile.get(new_rec_ptr->first_name, 15);
infile.ignore(20,'\n');
infile.get(new_rec_ptr->phone_num, 15);
infile.ignore(20,'\n');
insert_node(new_rec_ptr);
}
else // If end of file has been reached, delete the most recently
{ // created node and set the flag that ends the loop.
delete new_rec_ptr;
end_loop = 1;
}
}
else // If a memory allocation error occurs, display a message and
{ // set the flag that ends the loop.
cout << "WARNING: Memory error. Load from disk was unsuccessful.\n";
end_loop = 1;
}
} while(end_loop == 0); // Loop until the end_loop flag becomes TRUE.
infile.close(); // Close the file.
}
else // If error occurred opening file, display message.
{
cout << "No usable data file located. List is empty.\n";
}
} // end of function load_list_from_file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -