Last active
August 29, 2015 14:12
-
-
Save dd1994/c140c6a51b81f15d6da4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
// DataType 用 int 只是个例子,可以用任何你想要的 | |
typedef int DataType; | |
typedef struct Node | |
{ | |
DataType data; | |
struct Node *next; // 不知为何这里不能写成 ListNode *next; | |
}ListNode, *LinkList; | |
// 初始化单向链表 | |
int init_list(LinkList list) | |
{ | |
list = (LinkList)malloc(sizeof(ListNode)); | |
if(list == NULL) | |
{ | |
printf("error! allocating memory failed!\n"); | |
return 1; | |
} | |
else | |
{ | |
list->next = NULL; | |
return 0; | |
} | |
} | |
// 头插法建立具有 n 个 int 类型元素的单链表 | |
LinkList create_list_head_insert (LinkList list, int n) | |
{ | |
init_list(list); | |
DataType a; | |
for(int i = 0; i < n; i++) | |
{ | |
LinkList next_node; | |
next_node = (LinkList)malloc(sizeof(ListNode)); | |
scanf("%d", &a); | |
next_node->data = a; | |
next_node->next = list->next; | |
list->next = next_node; | |
} | |
return list; | |
} | |
// 输出一个单链表 | |
void list_print (LinkList list) | |
{ | |
LinkList temp_node = list->next; | |
while(temp_node != NULL) | |
{ | |
printf("%d\n", temp_node->data); | |
temp_node = temp_node->next; | |
} | |
} | |
int main (void) | |
{ | |
int a; | |
LinkList list; | |
list = create_list_head_insert(list, 3); | |
printf("\nlet's printf!\n"); | |
printf("%d\n", list->next->data); | |
printf("%d\n", list->next->next->data); | |
printf("%d\n", list->next->next->next->data); | |
list_print(list); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
为何那个 list_print 不能正常工作?