- 在注释中找到程序 2(在列表的 FRONT 插入几个节点)。修改
程序在列表的前面仅插入三 (3) 个节点。测试程序。
Q1的解决方案:
类和功能都保持不变。这是使用函数的主要原因之一,
也就是说,我们可以更改一个函数(或主函数),而保持其他一切不变。将主要更改为:
int main() { // insert at the FRONT of the list
Node *temp;
int number, i;
listpointer = NULL;
for (i = 0; i < 3; i++) {
cout << "Enter an integer value ";
cin >> number;
temp = new Node;
temp->loaddata(number, listpointer);
listpointer = temp;
}
displaylist();
}
- 在注释中找到程序 3(在列表的 BACK 处插入多个节点)。修改
程序在列表的后面仅插入三 (3) 个节点。测试程序。
Q2的解决方案:
类和功能都保持不变。将主要更改为:
int main() { // insert at the BACK of the list
Node *temp, *lastnode;
int number, i;
listpointer = NULL;
// set up the first node
cout << "Enter the first integer value ";
cin >> number;
listpointer = new Node;
listpointer->loaddata(number, NULL);
lastnode = listpointer;
// insert the other nodes
for (i = 1; i < 3; i++) { // start from 1
cout << "Enter an integer value ";
cin >> number;
temp = new Node;
temp->loaddata(number, NULL);
lastnode->setnext(temp);
lastnode = temp;
}
displaylist();
}
- 在注释中找到程序 4(从列表中删除一个节点)。修改程序为(a)插入
列表中的四 (4) 个节点,然后 (b) 删除第三个节点。测试程序。
Q3的解决方案:
类和功能都保持不变。将主要更改为:
int main() {
Node *ptr1, *ptr2, * ptr3, *ptr4, *temp;
int i;
listpointer = NULL;
// insert four nodes in the list
for (i = 0; i < 4; i++) {
temp = new Node;
temp->loaddata(i * 10, listpointer);
listpointer = temp;
}
displaylist();
cout << endl;
// now remove the third node
ptr1 = listpointer; // ptr1 points to the first node
ptr2 = ptr1->getnext(); // the second node
ptr3 = ptr2->getnext(); // the third node
ptr4 = ptr3->getnext(); // the fourth node
// change node2 to point to node4
ptr2->setnext(ptr4);
delete ptr3; // delete node3 to free memory
displaylist();
cout << endl;
}
4**。展开注释中的程序 4 以执行以下操作:
a) 在列表中输入五 (5) 个值
b) 输入必须删除的值
c) 找到该值并将其从列表中删除
d) 测试程序(记得测试删除第一个或最后一个值)
Q4的解决方案:
类和功能都保持不变。将主要更改为:
int main() {
Node *temp, *nextnode, *prevnode, *current, *previous;
int searchvalue, currentvalue, i;
bool found;
listpointer = NULL;
// insert five nodes in the list – this could be a function
for (i = 0; i < 5; i++) {
temp = new Node;
temp->loaddata(i * 10, listpointer);
listpointer = temp;
}
displaylist();
cout << endl;
// enter the value that must be deleted
cout << "Enter the value to be deleted ";
cin >> searchvalue;
// find the node with the search value
found = false;
previous = NULL;
current = listpointer;
while (current != NULL) {
currentvalue = current->getvalue();
if (currentvalue == searchvalue) {
found = true;
temp = current;
prevnode = previous;
}
previous = current;
current = current->getnext();
}
if (found == false) {
cout << searchvalue << " is not in the list.\n";
exit(2);
}
previous = prevnode;
nextnode = temp->getnext();
if (previous == NULL) { // this is the first node
listpointer = nextnode;
} else {
previous->setnext(nextnode);
}
delete temp; // delete the node to free memory
displaylist();
cout << endl;
}
- 创建一个类来保存国内航班的数据。该类必须存储以下信息:
航班号,例如NZ161(字符串)
出发地,例如奥克兰(字符串)
到达地点,例如惠灵顿(字符串)
以分钟为单位的旅行时间,例如85(正整数)
创建一些有用的方法,例如 loaddata 和 display。
使用 库,创建一个列表,其中列表中的每个节点都将是飞行的对象
上面描述的类。
Q5的解决方案:
注意 – 您不需要提供完整的程序。
class flight_class {
private:
string fltnum, departure, arrival;
int minutes;
public:
void loaddata();
void display();
};
list<flight_class> mylist;
//-------------- methods for the flight_class ------------
void flight_class::loaddata() {
cout << "Enter the flight number, e.g. NZ161 ";
getline(cin, fltnum);
cout << "Enter the departure place ";
getline(cin, departure);
cout << "Enter the arrival place ";
getline(cin, arrival);
cout << "Enter the travel time in minutes ";
cin >> minutes;
getchar(); // flush the input buffer
}
void flight_class::display() {
cout << "Information about flight " << fltnum << endl;
cout << "Departs from " << departure << endl;
cout << "Arrives at " << arrival << endl;
cout << "Travel time is " << minutes << " minutes.\n";
}
6.使用笔记中的程序5(使用链表库)和问题5中的信息
多于。 编写一个程序,读入三 (3) 个航班的数据并显示航班的详细信息
所有 3 个航班。 飞行数据必须存储在列表中
Q6的解决方案:
#include <iostream>
#include <cstdio> // for getchar
#include <list>
using namespace std;
class flight_class {
private:
string fltnum, departure, arrival;
int minutes;
public:
void loaddata();
void display();
};
void displaylist(list<flight_class> l);
list<flight_class> mylist;
int main() {
int i;
flight_class temp;
for (i = 0; i < 3; i++) {
temp.loaddata();
mylist.push_back(temp);
}
displaylist(mylist);
}
void displaylist(list<flight_class> l) {
list<flight_class>::iterator ptr;
for (ptr = l.begin(); ptr != l.end(); ptr++) {
ptr->display();
}
cout << endl;
}
//-------------- methods for the flight_class ------------
void flight_class::loaddata() {
cout << "Enter the flight number, e.g. NZ161 ";
getline(cin, fltnum);
cout << "Enter the departure place ";
getline(cin, departure);
cout << "Enter the arrival place ";
getline(cin, arrival);
cout << "Enter the travel time in minutes ";
cin >> minutes;
getchar(); // flush the input buffer
}
void flight_class::display() {
cout << "Information about flight " << fltnum << endl;
cout << "Departs from " << departure << endl;
cout << "Arrives at " << arrival << endl;
cout << "Travel time is " << minutes << " minutes.\n";
}