我已经尝试了三天了。我要做的是创建足球运动员的链表。每个足球运动员都有以下信息。
玩家姓名(最多15个字符) 球员的球衣号码(整数) 玩家得分(整数)
我必须定义一个名为Player的结构来存储有关足球运动员的信息。除了上面的数据成员,该结构应该有另一个指针成员,可以指向另一种Player类型的结构。要创建链接列表,请遵循以下算法:
声明头指针和前一个节点指针并将它们设置为NULL。
动态创建一个Player结构,如果这是第一个节点,则将head和previous指针指向新创建的结构。
要求用户输入播放器信息并将其存储在结构中。将指针成员设置为NULL。
如果不是第一个节点设置上一个节点指向的节点的指针成员指向新节点。
询问用户是否要继续。
创建链接列表后,我的程序应打印一个表,列出每个玩家的号码,名称和得分。程序必须使用函数名displayPlayer来打印表。主程序应该将头节点指针传递给函数。该功能应在适当宽度的字段中以单行显示播放器的信息(使用setw)。该函数的原型是void displayPlayer(Player *)。
我的节目不应接受球员数量或得分的负值。如果用户输入负数,我的程序应显示相应的消息并再次询问用户输入。
#include <iostream>
#include <cctype>
using namespace std;
struct Player
{
char name[16];
int jersey;
int points;
// declare the required members of the structure here
Player *next;
};
void displayPlayer(Player *); // prototype of the display function
int main()
{
Player *headptr;
Player *lastptr;
Player *newnode;
headptr = NULL;
lastptr = NULL;
//declare three pointers of Player type
//one pointer to point to head node; one to point to previous node
//and one to point to new node
do {
Player info;
cout << " Enter name then jersey, points ";
cin >> info.name;
cin >> info.jersey;
cin >> info.points;
newnode->
//dynamically create a new node of Player type and make the new node pointer point to it
//enter data in to the new node (name, jersy no, score)
//set the next field of new node to NULL
if ()
{
//make head node and previous node pointers equal to the new node pointer
} else {
//set the next member of of the previous node eqal to ne node pointer
//make previous node pointer point to the new new node
}
} while(user wants to continue);
// Call displayPlayer function with headnode pointer
}
void displayPlayer(Player *h)
{
while( not end of the list) {
//display node content
} // set h to next node
}
答案 0 :(得分:3)
好吧,让我做笔和纸的布局,但是在电脑上而不是真正的笔和纸上......
当列表为空时,你的头指针和尾指针都指向NULL:
head ---> NULL tail ---> NULL
现在让我们将节点A插入空列表:
head --\ +------+ }--> | A | tail --/ | next | ---> NULL +------+
head和tail都指向唯一的节点,而节点next指针指向NULL(意味着它是列表中的最后一个节点)。
如果我们现在在列表的末尾插入一个新节点B,它将如下所示:
+------+ head ---> | A | | next | ---------\ +------+ +------+ }--> | B | tail --/ | next | ---> NULL +------+
现在头部仍然指向A,但是尾部指向B,以及来自A的下一个链接.B中的下一个链接指向NULL,表示这是列表中的最后一个节点。
如果我们将一个节点C添加到列表的头部,它现在将如下所示:
+------+ head ---> | C | +------+ | next | ---> | A | +------+ | next | ---------\ +------+ +------+ }--> | B | tail --/ | next | ---> NULL +------+
添加到头部要求您只需将头指针更改为指向新节点,并将C的下一个指针指向旧头。
希望这有助于您了解链接列表的工作原理。
答案 1 :(得分:2)
这将需要几次迭代。
你拥有的玩家结构非常适合开始:
struct Player
{
char name[16];
int jersey;
int points;
Player *next;
};
这涵盖了基础知识。现在这个结构只有数据成员,没有方法;它是一个不能做任何东西的数据容器 - 其他东西在它上面运行。稍后您可能会发现方法很方便。下一步是什么?
修改
在main
中声明指针。再一次,你已经拥有它了:
int main()
{
Player *headptr;
Player *lastptr;
Player *newnode;
...
}
这可能不是宣传newnode
最方便的地方。在例程顶部声明所有变量的做法来自堆栈内存是必须仔细husbanded的资源的日子;现在,拥有易于阅读的代码更为重要,因此不应在需要之前声明变量。但是我们以后可以担心。下一步是什么?
修改
动态创建新的Player
:
new Player;
这会创造一个新玩家 - 但会立即失去它。当你给气球充气时,你有必要抓住它:
newnode = new Player;
new
运算符返回指向新对象的指针,即它的地址。现在我们已将其保留在newnode
中,我们可以用它来做事:
newnode->jersey = 34;
cout << newnode->jersey << endl;
现在是什么?
修改
Now things get dangerous. Character arrays are a pain in the neck, always have been. And getting input by `cin >> ...` is dangerous, and should be avoided in Real Life. But for now we can get away with:
newnode = new Player;
cout << "Enter name, then jersey number, then number of points:" << endl;
cin >> newnode->name >> newnode->jersey >> newnode->points;
cout << newnode->name << " " << newnode->jersey << " scored " << newnode->points << endl;
请注意,此代码可以作为方法进入Player
结构;如果我们有时间,也许我们会在以后这样做。接着?