在linkedList C++的开头插入
Inserting at the head of the linkedList C++
我正试图创建一个函数,让我在列表的开头插入节点。
这是我目前所掌握的,但我需要一些帮助和建议。你能放些光吗?既然快到感恩节了,我不想再被这个练习折磨了?
void List::insertAtHead(int addData) {
nodePtr n = new node;
n->next = NULL;
n->data = addData;
if(head != NULL){
n->next = head;
head = n;
}
else
{
head = n;
}
尽管有node*
,但它可以简化为:
void List::insertAtHead(int addData){
node* n = new node;
n->next = head;
n->data = addData;
head = n;
}
根据Zac Howland的建议:
struct node
{
int data;
node* next;
node(int data,node* next)
:data(data)
,next(next)
{}
};
void List::insertAtHead(int addData){
head = new node(addData,head);
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 如何在C++中.txt文件的开头插入字符串
- C++ 在字符的开头插入字符
- 使用代码块在链表的开头插入节点
- 为什么 C++ 中的分号在插入代码开头时不显示任何错误?
- 如何在单向链表的开头插入
- C++中的数据结构,插入链表中节点的开头
- 如何将元素插入矢量的开头
- 将节点插入链表只会转到开头或结尾
- 无法在 c++ 链表的开头插入
- 在 std::forward_list 的开头插入元素
- 如何在结果列表的开头插入生成的列表
- 在linkedList C++的开头插入
- 在另一个字符*C++的开头插入一个字符*
- 在 protobuf 列表的开头插入项目
- 在链表的开头插入时读取内存时出错
- 如何在矢量的开头插入元素
- 关于在链表开头插入节点(程序未正确执行)
- 如何将字符串插入到字符串流的开头