代碼:
1 #include "stdio.h"
2 #include "stdlib.h"
3
4 typedef struct node
5 {
6 int data;
7 struct node * next;
8 }node;
9
10 node * createLinkedList(int n);
11 void displayList(node * head);
12
13 int main(int argc, char const *argv[])
14 {
15 int n = 0;
16 node * HEAD = NULL;
17 printf("How many nodes:\n");
18 scanf("%d",&n);
19 HEAD = createLinkedList(n);
20 displayList(HEAD);
21 return 0;
22 }
23
24 node * createLinkedList(int n)
25 {
26 int i = 0;
27 node * head = NULL;
28 node * temp = NULL;
29 node * p = NULL;
30
31 for (int i = 0; i < n; ++i)
32 {
33 //let us create isolated node
34 temp = (node *)malloc(sizeof(node));
35 printf("Enter the data for node number: \n",i+1);
36 scanf("%d",&(temp->data));
37 temp->next = NULL;
38
39 if(head == NULL)
40 {
41 head = temp;
42 }
43 else
44 {
45 p = head;
46 while(p->next != NULL)
47 p = p->next;
48 p->next = temp;
49 }
50
51 }
52 return head;
53 }
54
55 void displayList(node * head)
56 {
57 node * p = head;
58 while(p != NULL)
59 {
60 printf("\t%d->",p->data );
61 p = p->next;
62 }
63 }
原文:https://www.cnblogs.com/s0ra365/p/12689100.html