@bintou
2017-11-30T21:04:14.000000Z
字数 786
阅读 1896
CSI
Source
Code
#include <stdio.h>
#include <stdlib.h>
typedef struct _slist_t{
int data;
struct _slist_t *next;
} slist_t;
// This function prints contents of linked list starting from head
void printList(slist_t *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
printf("\n");
}
int main()
{
slist_t *list = NULL, *visitor; //empty list;
slist_t *new_node = (slist_t*)malloc(sizeof(slist_t));
new_node->data = 2017;
new_node->next = NULL;
//insert in list
list = new_node;
new_node = (slist_t*)malloc(sizeof(slist_t));
new_node->data = 2018;
new_node->next = NULL;
//insert in head
new_node->next = list;
list = new_node;
//Run through the list. We write this to warn you sth...
visitor = list;
int i = 0;
while (visitor != NULL){
printf("The %d th member is %d.\n", i++, visitor->data);
visitor = visitor->next;
}
//Print the list using a function.
printList(list);
return 0;
}