我已经实现了一个短链接列表代码,以添加到列表的开头.
然而,头总是包含NULL.我真的不明白为什么它以这种方式行事.任何帮助表示赞赏!以下是代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int iData;
struct node *next;
} Node;
void add2Beg(Node* head, int num);
int main(int argc, char const *argv[])
{
Node *head = NULL;
add2Beg(head, 5);
if (head == NULL)
printf("nothing in head !!!\n");
else{
printf("not null\n");
}
add2Beg(head, 15);
return 0;
}
//adds to the beginning of the linked list
void add2Beg(Node* head, int num)
{
//create a temporary location to hold the new entry
Node* temp = (Node *)malloc(sizeof(Node)); …Run Code Online (Sandbox Code Playgroud)