1.單鏈表插入的操做以下示意圖:
、node
2.具體的操做分爲三步:數據結構
a.建立一個新的節點p3;性能
b.p3的next指針在插入的時候先指向p1spa
c.而後讓本來指向p1的next指針指向p3指針
3.具體的插入方式有兩種:code
a.頭插入法blog
b.尾部插入法排序
4.具體的代碼 io
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.建議:
進行單鏈表的插入的操做時,應該選擇前插入方式,這樣插入相對比較快。若是是進行插入並排序的話,建議使用其餘的排序方法,單鏈表也有他的侷限性,
讀者能夠自行進行補全在數據結構方面的知識。