我在准备面试时遇到了这个问题,并且很想知道它可以写出的不同方式.我在http://cslibrary.stanford.edu/103/找到了这个,并且已经给出了问题.
这是构建列表{1,2,3}的代码
struct node* BuildOneTwoThree() {
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->data = 1; // setup first node
head->next = second; // note: pointer assignment rule
second->data = 2; // setup second node
second->next = third;
third->data = 3; // setup third link
third->next = NULL;
// At this …Run Code Online (Sandbox Code Playgroud)