链表1 — 单链表遍历输出
目的:实现数组元素利用单链表的遍历输出
单链表的基本结构
单链表的结点插入的尾插法
单链表结点的遍历输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <stdio.h> #include <stdlib.h>
typedef struct LinkList{ int d; struct LinkList *next; } List;
List *add_List(List* Head, int data){ List *newnode = (List*)malloc(sizeof(List)); newnode->d = data; newnode->next = NULL; if(Head == NULL) { Head = newnode; }else{ List *pT = Head; while (pT->next != NULL) { pT = pT->next; } pT->next = newnode; } return Head; }
int main(){ int n,i,j; List *Head = NULL; scanf("%d",&n); int a[n]; for(i = 0; i < n; i++){ scanf("%d",&a[i]); getchar(); } for(j = 0; j <n; j++){ Head = add_List(Head,a[j]); } List *p = Head;
while (p != NULL){ printf("%d",p->d); printf(" "); p = p->next; } List *temp; while (Head != NULL) { temp = Head; Head = Head->next; free(temp); } return 0; }
|