链表学习
单链表 头插法 这里用一个学号+姓名的结构体学习单链表的 “头插法” 。
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 70 71 72 73 74 75 76 77 78 79 #include <stdio.h> #include <stdlib.h> struct STU { int nums; char name[10 ]; struct STU *next ; }; void opdata (struct STU *student) { puts ("input the nums: " ); scanf ("%d" , &student->nums); puts ("input the name: " ); scanf ("%s" , student->name); } void addnode (struct STU **student) { struct STU *temp , *new_stu ; if (*student != NULL ) { new_stu = (struct STU*)malloc (sizeof (struct STU)); opdata(new_stu); temp = *student; *student = new_stu; new_stu->next = temp; } else { new_stu = (struct STU*)malloc (sizeof (struct STU)); opdata(new_stu); *student = new_stu; } } void PrintData (struct STU *student) { struct STU *stu ; stu = student; while (stu != NULL ) { printf ("xuehao is %u\n" , stu->nums); printf ("name is %s\n" , stu->name); stu = stu->next; printf ("\n" ); } } void releaseMem (struct STU **student) { struct STU *temp ; while ( *student != NULL ) { temp = *student; *student = (*student)->next; free (temp); } } int main () { struct STU * student = NULL ; puts ("test" ); printf ("the addr of (*student) is %p\n" ,student); printf ("the addr of (&student) is %p\n" ,&student); addnode(&student); PrintData(student); releaseMem(&student); puts ("successful" ); return 0 ; }
关于程序中指向指针的指针我理解为
这里addnode和releaseMem函数的参数均是二级指针,这个指针指向了 “指向数据内容的一级指针”
尾插法 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 70 71 72 73 74 75 76 77 78 79 80 #include <stdlib.h> #include <stdio.h> struct stu { char name[20 ]; int score; struct stu *next ; }; void OpStu (struct stu *student) { printf ("name : " ); scanf ("%s" , student -> name); printf ("\n" ); printf ("Score : " ); scanf ("%d" , &student -> score); } void AddStu (struct stu **student) { struct stu *new_stu = (struct stu*)malloc (sizeof (struct stu)); static struct stu *end_node ; OpStu(new_stu); if (*student == NULL ) { *student = new_stu; new_stu->next = NULL ; } else { end_node->next = new_stu; new_stu->next = NULL ; } end_node = new_stu; } void PrintNode (struct stu *students) { struct stu *temp ; temp = students; while (temp->next != NULL ) { printf ("name is %s, got %d scores\n" , temp->name, temp->score); temp = temp->next; } printf ("FINISH\n" ); } void ReleasMem (struct stu **students) { struct stu *temp ; while (*students != NULL ) { temp = *students; *students = (*students)->next; free (temp); } puts ("free end\n" ); } int main () { struct stu *student = NULL ; int i=5 ; while (i!=0 ) { AddStu(&student); i--; } PrintNode(student); ReleasMem(&student); return 0 ; }
插入,删除 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 void InsertNode (int addr, struct stu *student) { struct stu *stu = student; struct stu *newstu = NULL ; struct stu *temp ; for (;addr-1 !=0 ;addr--) { stu = stu->next; } newstu = (struct stu*)malloc (sizeof (struct stu)); OpStu(newstu); temp = stu->next; stu->next = newstu; newstu->next = temp; } void DeletNode (int addr, struct stu* student) { struct stu *under , *stu , *pre ; stu = student; for (;addr!=1 ;addr--) { if (addr == 2 ) { pre = stu; } stu = stu->next; } under = stu->next; pre->next = under; free (stu); puts ("delete success" ); }
插入: