Creating a linked list with a for loop

心已入冬 提交于 2019-12-02 09:45:26

Don't modify a. Take a temp node starting as a. Make it's next point to the new node and then set temp node to the new node. Also allocate dynamically in heap. Otherwise the memory will get deallocated after every loop run

struct ListItem a[5] = { {0, NULL}};
struct ListItem *pointer = &a[0];

for (int i = 0; i < 5; i++){
    a[i].data = i;
    if(i != 5 -1)
        a[i].next = &a[i+1];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!