我正在研究以下代碼,但對函式 read_file() 中單鏈表的構建感到困惑,我在下面代碼旁邊的函式中標記了問題:
這是完整的代碼:https ://github.com/mickyjm/c.cpp.address.book.project/tree/master/CPP
列出頭檔案 list.h
#ifndef LLIST_H
#define LLIST_H
#include <string>
class llist {
private:
record *start;
std::string file_name;
int read_file();
int write_file();
record* reverse_llist(record *);
void delete_all_records();
public:
llist();
llist(std::string);
~llist();
int add_record(std::string, std::string, int, std::string);
int print_record(std::string);
int modify_record(std::string, std::string, std::string);
void print_all_records();
int delete_record(std::string);
void reverse_llist();
};
#endif
記錄頭檔案record.h
#include <string>
#ifndef RECORD_H
#define RECORD_H
struct record {
std::string name;
std::string address;
int birth_year;
std::string phone_number;
struct record* next;
};
#endif
list.cpp read_file 函式
int llist::read_file() {
// read_file variables
std::ifstream read_file(file_name.c_str());
struct record *temp = NULL;
struct record *index = NULL;
struct record *previous = NULL;
int file_char_length = 0;
int record_count = 0;
std::string dummy = "";
if (!read_file.is_open()) {
read_file.close();
return -1;
} // end if !read_file.is_open()
read_file.seekg(0, read_file.end); // move read pointer to end of file
file_char_length = read_file.tellg(); // return file pointer position
if (file_char_length == 0) {
read_file.close();
return 0;
} // end file_char_length == 0
read_file.seekg(0, read_file.beg); // reset file pointer to beginning
do { // do while !read_file.eof()
// do while temporary variables
std::string address = "";
temp = new record;
index = start;
std::getline(read_file, temp->name);
std::getline(read_file, temp->address, '$');
read_file >> temp->birth_year;
std::getline(read_file, dummy);
std::getline(read_file, temp->phone_number);
std::getline(read_file, dummy);
record_count;
while (index != NULL) { <-- what's the purpose of this loop?
previous = index;
index = index->next;
} // end while index != NULL
if (previous == NULL) { <-- why would the start pointer of the
temp->next = start; list not at the start but after temp?
start = temp;
} else { // else if previous != NULL
previous->next = temp; <-- what is the purpose of this loop?
temp->next = index;
} // end if previous == NULL
} while (!read_file.eof()); // end do while
read_file.close();
return record_count; // read_file return - end of function
}
uj5u.com熱心網友回復:
按照評論的順序。
- 這個回圈的目的是什么?
while (index != NULL) {
previous = index;
index = index->next;
} // end while index != NULL
答:此回圈用于定位串列中previous
的最后一個節點,只要至少有一個節點即可。有多種技術可以做到這一點;這是比較常見的一種。隨著index
比賽結束回圈,previous
落后一步。
- 為什么串列的開始指標不在開始處而是在 temp 之后?
if (previous == NULL) {
temp->next = start;
start = temp;
回答:與該問題的語法作斗爭,使用start
here作為該作業的右側是沒有意義的。它必須為 NULL,否則先前的回圈將加載previous
非 NULL 值,并且此 if 條件將失敗。代碼可以很容易地閱讀:temp->next = nullptr; start = temp;
- 這個回圈的目的是什么?
} else { // else if previous != NULL
previous->next = temp;
temp->next = index;
} // end if previous == NULL
答:首先,這不是一個回圈。這是else
prior 的子句if
,這意味著如果它運行它是因為它previous
是非空的。類似于start
上面(2)中奇怪的用法,index
這里的使用是沒有意義的。如果您查看您在 (1) 中詢問的回圈,您會清楚地看到事情不會停止,直到index
為 NULL。那里和這里之間沒有任何東西可以改變這一事實。因此,temp->next = nullptr;
等價于這里發生的事情。
不給它加糖;這個實作很薄弱,那是美好的一天。無論它來自誰,都將其視為如何完成事情的集合,但絕不是應該如何完成的事情。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/439686.html
上一篇:洗掉重復的字典python