嗯,这不是一个答案,但我想这是注册的另一个好处 - 编辑我的文章。我会稍后注册。
无论如何,这里是代码,而且,我没有使用长*这就是为什么我很困惑。至少,据我所知,我不使用长*。为了澄清另一件事,代码可能有点陈旧/过时,这是因为我们的老师坚持使用一本非常古老的书。
#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