-3
我是新来的链接list..My简单的代码是创建链接列表,并在年底插入节点并遍历它..
我的问题是 -
1)-Every时间插入功能叫,头指针变空
2)-not工作的权利,而在播放功能去..遍历链表
提前
#include<iostream>
#include<malloc.h>
using namespace std;
struct linkedList
{
int value;
linkedList *next;
};
linkedList* head = NULL;
void insert(linkedList* head, int data)
{
linkedList *ptr;
linkedList *node;
node = (linkedList*) malloc(sizeof(struct linkedList));
node->value = data;
node->next = NULL;
if (head == NULL)
{
head = node;
}
else
{
ptr = head;
while (ptr != NULL)
{
ptr = ptr->next;
}
ptr = node;
}
}
void show(struct linkedList *head)
{
struct linkedList *ptr;
ptr = head;
while (ptr != NULL)
{
cout << ptr->value << endl;
ptr = ptr->next;
}
}
int main()
{
int size = 5;
int array[size];
for (int i = 0; i < 5; i++)
{
cout << "Enter value" << endl;
cin >> array[i];
insert(head, array[i]);
}
show(head);
}
了解传递参数之间用* *值和参考差异*。 –
欢迎来到堆栈溢出!这听起来像你可能需要学习如何使用调试器来遍历代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。延伸阅读:** [如何调试小程序(http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** –
看起来像你学习'C'而不是'C++'。那里有什么'malloc'(而不是'new')? – PaulMcKenzie