尝试用 C++ 创建一个单链表按时间顺序存储值而不是向后存储所有内容
我已经工作了几个小时,试图让这个程序将值插入到这个程序的新节点或“玩家”结构中。
问题是,这发生在函数 main() 内。我的老师要求我插入尽可能少的代码,因为基本上“一切”都在那里,允许我更改它。
这是原始代码,它将每个新值存储在头部,同时将旧值放入“addNew”中:
Player * head = NULL;
for(int i=0; i<100; i++)
{
Player * addNew = (Player *)malloc(sizeof(Player));
if(head == NULL)
{
head->len = i;
Player * addNew = (Player *)malloc(sizeof(Player));
head->next = NULL;
}
addNew->next = addNew;
addNew->len = i;
}
Player * p = head;
//do this until 'p' has no address.
for(int i=0; p!=0; i++)
{
printf("%s ", p->str);
p = p->next;
}
有人对如何解决这个问题有任何想法吗? 重要提示:我的老师希望我不要添加任何新变量或尾巴。请不要问。
更新:这是一些较旧的代码:
//The head is the last one to hold a value. Therefore it gets pushed to the right.
Player * head = NULL;
Original Algorithm
for(int i=0; i<5; i++)
{
Player * addNew = (Player *)malloc(sizeof(Player));
printf("Insert a string: ");
scanf("%s", addNew->str);
addNew->next = head; //assign head's current address to addNew->next
head = addNew; //assign all of addNew to head
}
更新:这是一个不起作用的新实现。我真的无法找出另一个 for 循环到底应该去哪里。
addNew->ID = 1;
addNew->ID += i;
if(head == NULL)
{
head = addNew;
addNew->next = head; //assign head's current address to addNew->next
//head->next = addNew;
}
//head->next = addNew;
addNew->next = head;
//head = addNew; //assign all of addNew to head
printf("%d\n", addNew->ID);
I've been working for hours trying to get this program to insert values into new nodes or "Player" structures for this program.
The thing is, this takes place inside function main(). My teacher requires me to insert as little code as possible since essentially "everything" is there which allows me to change it.
Here's the original code that stores each new value in the head while putting the older value inside "addNew":
Player * head = NULL;
for(int i=0; i<100; i++)
{
Player * addNew = (Player *)malloc(sizeof(Player));
if(head == NULL)
{
head->len = i;
Player * addNew = (Player *)malloc(sizeof(Player));
head->next = NULL;
}
addNew->next = addNew;
addNew->len = i;
}
Player * p = head;
//do this until 'p' has no address.
for(int i=0; p!=0; i++)
{
printf("%s ", p->str);
p = p->next;
}
Does anyone have any ideas on how to solve this?
IMPORTANT: My teacher would like me to not add any new variables or a tail. Please don't ask.
Update: Here's some older code:
//The head is the last one to hold a value. Therefore it gets pushed to the right.
Player * head = NULL;
Original Algorithm
for(int i=0; i<5; i++)
{
Player * addNew = (Player *)malloc(sizeof(Player));
printf("Insert a string: ");
scanf("%s", addNew->str);
addNew->next = head; //assign head's current address to addNew->next
head = addNew; //assign all of addNew to head
}
Update: Here's a new implementation which doesn't work. I can't really find out where exactly that other for loop should go.
addNew->ID = 1;
addNew->ID += i;
if(head == NULL)
{
head = addNew;
addNew->next = head; //assign head's current address to addNew->next
//head->next = addNew;
}
//head->next = addNew;
addNew->next = head;
//head = addNew; //assign all of addNew to head
printf("%d\n", addNew->ID);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的技巧:让 list* 始终指向列表中的最后一个元素。并将最后一个元素的下一个指针设置为列表的开头。现在,您只需一个指针即可轻松找到列表的开头和结尾。开始位置是列表->下一个。不要称之为尾部,只是“列表”。
Simple trick: have the list* always point to the last element in the list. And set that last element's next pointer to the start of the list. Now you can always easily find both the start and the end of the list with just one pointer. The start is at list->next. Don't call it tail, just "list".
现在,您正在将所有内容添加到列表的头部。但是,您想添加到尾部。
你已经有了一个指向头部的指针。除此之外,您还需要一个指向尾部的指针。你能想出一种方法来获取(和更新)它吗?
Right now, you're adding everything to the head of the list. However, you want to add to the tail instead.
You already have a pointer to the head. In addition to that though, you'll also need a pointer to the tail. Can you think of a way to get (and update) it?
您可以保存指向列表尾部的指针,而不是保存每个节点上的下一项,而是保存前一项。
You could save a pointer to the tail of the list and instead of saving the next item on each node, save the previous one.
这可能不是一个选择,但我想我还是会建议它。您可以按原样存储内容,而不是按不同的顺序存储内容,而只需更改输出值的方式即可。这不仅实现起来非常简单,而且在列表插入期间也会更快。
This might not be an option but I thought I'd suggest it anyway. Instead of storing things in a different order, you could leave things stored as they are, but simply change the way you output the values. This would not only be very simple to implement, it would also be a faster during list inserts.