嗯,這不是一個答案,但我想這是註冊的另一個好處 - 編輯我的文章。我會稍後註冊。
無論如何,這裏是代碼,而且,我沒有使用長*這就是爲什麼我很困惑。至少,據我所知,我不使用長*。爲了澄清另一件事,代碼可能有點陳舊/過時,這是因爲我們的老師堅持使用一本非常古老的書。
#include <iostream>
#include <iomanip>
using namespace std;
const long length = 4; // prevents years larger than 4 figures from being entered
long hist_year[length], hist_event;
// history_node which contains the year and a historical event
struct history_node
{
long hist_year[length];
long hist_event;
history_node *next;
};
history_node *head_ptr;
history_node *current_ptr;
// prototypes
void insert_node(long hist_year[length], long hist_event);
void sort_inserted_node(long hist_year[length]);
void make_node_new_head(history_node *new_rec_ptr);
void move_current_to_end();
void display_list();
void delete_list();
int main()
{
long year[length], h_event;
if(get_history_data(year, hist_event))
{
head_ptr = new history_node;
head_ptr->hist_year, year;
head_ptr->hist_event = h_event;
head_ptr->next = NULL;
while(get_history_data(year, h_event))
{
insert_node(year, h_event);
}
display_list();
delete_list();
}
system("pause");
return 0;
}
// function that gets data from user
int get_history_data(long year[length], long &h_event)
{
int keep_data = 1;
cout << "Enter the year (Press Enter alone to stop): ";
cin >> *year;
cin.ignore(80,'\n');
if(year[0] != 0)
{
cout << "Enter the historical event: ";
cin >> h_event;
cin.ignore(80,'\n');
}
else
{
keep_data = 0;
}
return(keep_data);
}
// function that inserts new node sorted by year
void insert_node(long year[length], long h_event)
{
history_node *new_rec_ptr;
history_node *before_ptr;
history_node *after_ptr;
new_rec_ptr = new history_node;
new_rec_ptr->hist_event, h_event;
new_rec_ptr->hist_year, year;
if(year > head_ptr->hist_year)
{
make_node_new_head(new_rec_ptr);
}
else
{
sort_inserted_node(year);
before_ptr = current_ptr;
after_ptr = current_ptr->next;
before_ptr->next = new_rec_ptr;
new_rec_ptr->next = after_ptr;
}
}
// function that points current_ptr to node before position where new should be inserted
void sort_inserted_node(long year)
{
long temp_year;
history_node *temp_ptr;
if(head_ptr->next != NULL)
{
current_ptr = head_ptr;
temp_ptr = current_ptr->next;
temp_year = temp_ptr->hist_year;
while((current_ptr->next !=NULL) && (year < temp_year))
{
current_ptr = temp_ptr;
temp_ptr = current_ptr->next;
temp_year = temp_ptr->hist_year;
}
}
else
{
current_ptr = head_ptr;
}
}
// function makes node new head of linked list
void make_node_new_head(history_node *new_rec_ptr)
{
new_rec_ptr->next = head_ptr;
head_ptr = new_rec_ptr;
}
// function that moves current_ptr to end of list
void move_current_to_end()
{
current_ptr = head_ptr;
while(current_ptr->next != NULL)
{
current_ptr = current_ptr->next;
}
}
// function displays list
void display_list()
{
current_ptr = head_ptr;
do
{
cout.setf(ios::left);
cout << setw(25) << current_ptr->hist_event;
cout.setf(ios::right);
cout << setw(12) << current_ptr->hist_year << endl;
current_ptr = current_ptr->next;
}
while(current_ptr != NULL);
}
// function frees memory used by linked list
void delete_list()
{
history_node *temp_ptr;
current_ptr = head_ptr;
do
{
temp_ptr = current_ptr->next;
delete current_ptr;
current_ptr = temp_ptr;
}
while(temp_ptr != NULL);
}
您需要包括結構的定義'history_node'除了上面的代碼 – 2010-12-12 09:24:00
@保羅 - [R說什麼,但我們當然可以推斷出'hist_year'聲明'長*';問題是爲什麼,以及這是否正確,這似乎不大可能。 – Clifford 2010-12-12 10:57:37