C语言的数据结构之单链表插入操作说明
1.单链表插入的操作如下示意图:
、
2.具体的操作分为三步:
a.创建一个新的节点p3;
b.p3的next指针在插入的时候先指向p1
c.然后让原本指向p1的next指针指向p3
3.具体的插入方式有两种:
a.头插入法
b.尾部插入法
4.具体的代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 5 #define MAX_NUM 1000000 6 7 #define INSERT_FORWARD 1 8 9 typedef struct Student 10 { 11 int id; 12 13 struct Student *next; 14 15 }Stu; 16 17 18 void printListInfo(Stu* head); 19 void insertListData(Stu* head,int id); 20 void insertListData_forward(Stu* head,int id); 21 void deleteListData(Stu* head,int id); 22 Stu* createNewData(int id); 23 24 25 26 void printListInfo(Stu* head) 27 { 28 if(head->next == NULL) 29 { 30 fprintf(stderr,"the list is empty,cannot print student infomation\n"); 31 return; 32 } 33 34 else 35 { 36 Stu *stuInfo = head->next; 37 while(stuInfo) 38 { 39 if(stuInfo->next != NULL) 40 printf("%d->",stuInfo->id); 41 else 42 printf("%d\n",stuInfo->id); 43 44 stuInfo = stuInfo->next; 45 } 46 47 return; 48 } 49 } 50 51 void insertListData(Stu* head,int id) 52 { 53 54 Stu *newId = createNewData(id); 55 if(newId == NULL) 56 { 57 fprintf(stderr,"insert failed,because create a new student id failed\n"); 58 return; 59 } 60 61 if(head->next == NULL) 62 { 63 head->next = newId; 64 } 65 66 else 67 { 68 Stu *tempId = head; 69 while(tempId->next) 70 { 71 tempId = tempId->next; 72 } 73 74 tempId->next = newId; 75 76 return; 77 } 78 } 79 80 void insertListData_forward(Stu* head,int id) 81 { 82 Stu *newId = createNewData(id); 83 84 if(newId == NULL) 85 { 86 fprintf(stderr,"insert failed,because create a new student id failed\n"); 87 return; 88 } 89 90 if(head->next == NULL) 91 { 92 head->next = newId; 93 } 94 95 else 96 { 97 newId->next = head->next; 98 head->next = newId; 99 return; 100 } 101 } 102 103 void deleteListData(Stu* head,int id) 104 { 105 106 if(head->next == NULL) 107 { 108 fprintf(stderr,"sorry,the store student data is Empty\n"); 109 return ; 110 } 111 112 else 113 { 114 Stu *stu = head; 115 while(stu->next != NULL) 116 { 117 if(stu->next->id == id) 118 { 119 Stu *temp = stu->next; 120 stu->next = temp->next; 121 temp->next = NULL; 122 free(temp); 123 temp = NULL; 124 125 if(stu->next == NULL) 126 break; 127 } 128 129 stu = stu->next; 130 } 131 132 return; 133 } 134 135 } 136 137 Stu* createNewData(int id) 138 { 139 Stu *newId = (Stu*)malloc(sizeof(Stu)); 140 if(newId == NULL) 141 { 142 fprintf(stderr,"create a new student id failed\n"); 143 return NULL; 144 } 145 146 else 147 { 148 newId->id = id; 149 newId->next = NULL; 150 return newId; 151 } 152 } 153 154 155 int main(int argc,char *argv[]) 156 { 157 Stu *head = (Stu*)malloc(sizeof(Stu)); 158 if(head == NULL) 159 { 160 fprintf(stderr,"create a list header node failed\n"); 161 162 return -1; 163 } 164 165 else 166 { 167 head->next = NULL; 168 169 int i = 0; 170 171 #ifdef INSERT_FORWARD 172 { 173 for(i=0;i<MAX_NUM;i++) 174 insertListData_forward(head,i); 175 } 176 177 #else 178 { 179 for(i=0;i<MAX_NUM;i++) 180 insertListData(head,i); 181 } 182 183 #endif 184 185 printf("insert data finish\n"); 186 printf("\n\n*********************************\n"); 187 188 } 189 190 return 0; 191 }
5.性能分析前插入数据和尾插入数据
a.都是插入数据的方式,前插入的在于每次插入数据都只需要在头部插入即可,尾插入在于每次插入之前必须定位到链表的末端
b.前插入数据所用的时间是比较快的,尾插入的数据由于需要定位到末端,因此当数据很大的时候,插入的操作就变得非常慢
6.建议:
进行单链表的插入的操作时,应该选择前插入方式,这样插入相对比较快。如果是进行插入并排序的话,建议使用其他的排序方法,单链表也有他的局限性,
读者可以自行进行补全在数据结构方面的知识。