C++,学习链表
C++, learning linked list
我正在学习一个非常简单的链表,
但下面的代码不起作用,当我输入1和2时,系统输出只有1和NULL
有人能帮帮我吗
这是我的密码。
#include <iostream>
using namespace std;
struct node
{
char username[10];
node *next;
};
int main()
{
node *head;
node *place;
node *tail;
head = new node;
place = head;
tail = place->next;
cout<<"username 1 ";
cin>>place->username;
place = tail;
place = new node;
tail = place->next;
cout<<"username 2 ";
cin>>place->username;
place = head;
cout<<"username 1 ";
cout<<place->username<<endl;
place = place->next;
cout<<"username 2 ";
cout<<place->username<<endl;
return 17;
}
如有任何帮助,我们将不胜感激,谢谢。
从不将节点链接在一起。在整个程序中,没有任何地方设置下一个字段。把这个写在纸上,一次一句。而不是
place = new node;
tail = place->next;
创建新节点时,将其添加到列表的末尾:将当前末端链接到新节点,然后移动末端指针。
place = new node;
tail->next = place;
tail = place;
#include <iostream>
using namespace std;
struct node
{
char username[10];
node *next;
};
int main()
{
node *head;
node *place;
node *tail;
place = new node;
head = place; // head is assigned to the first created place and should never be modified ever after.
tail = head; // at the beginning the last element is also the first, it will move along and always point to the last node created
cout<<"username 1 ";
cin>>place->username;
place = new node; // create new node
tail->next = place; // assign next
tail = place; // then point the tail to the new node
cout<<"username 2 ";
cin>>place->username;
place = head; // rewind the list to the head
cout<<"username 1 ";
cout<<place->username<<endl;
place = place->next;
cout<<"username 2 ";
cout<<place->username<<endl;
return 17;
}
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表c++插入,所有情况都已检查,但没有任何工作
- 学习在C++中实现链表堆栈类
- 学习在C++中实现链表队列
- C++,学习链表