数据结构--单链表 C语言
//单链表基本操作
1.1创建空链表:
1.2创建N个节点的链表
2.删除节点
3.添加新节点
1 #include <stdio.h> 2 3 #include <stdlib.h> 4
5 6 typedef struct _NODE 7 { 8 int data; 9 struct _NODE *pNext; 10 }NODE,*PNODE; 11 12 PNODE Create_List(void) 13 { 14 int len = 0; 15 int data,i = 0; 16 PNODE pHead = NULL; 17 pHead = (PNODE)malloc(sizeof(NODE)); 18 PNODE pTail = pHead; 19 pTail->pNext = NULL; 20 21 if(pHead == NULL) 22 { 23 printf("内存分配失败!\r\n"); 24 } 25 else 26 { 27 printf("请输入链表长度:\r\n"); 28 scanf_s("%d",&len); 29 for(;i < len ; i++) 30 { 31 PNODE q = (PNODE)malloc(sizeof(NODE)); 32 if(q == NULL) 33 { 34 printf("内存分配失败!\r\n"); 35 } 36 else 37 { 38 printf("请输入数据:\r\n"); 39 scanf_s("%d",&data); 40 q->data = data; 41 pTail->pNext = q; 42 q->pNext = NULL; 43 pTail = q; 44 } 45 } 46 } 47 48 return pHead; 49 } 50 51 void Traverse_List(PNODE pHead) 52 { 53 PNODE p = pHead->pNext; 54 if(p == NULL) 55 { 56 printf("链表为空!\r\n"); 57 } 58 else 59 { 60 while(p != NULL) 61 { 62 printf("%d\r\n",p->data); 63 p = p->pNext; 64 } 65 } 66 67 } 68 69 70 bool List_Insert(PNODE pHead,int pos, int val) 71 { 72 int i = 0; 73 PNODE p = pHead; 74 if(p == NULL) 75 { 76 return false; 77 } 78 else 79 { 80 while(p != NULL && i < pos-1) 81 { 82 i++; 83 p = p->pNext; 84 } 85 86 if(i > pos -1) return false; 87 88 PNODE q = (PNODE)malloc(sizeof(NODE)); 89 if(q == NULL) 90 { 91 printf("内存分配失败!\r\n"); 92 return false; 93 } 94 q->data = val; 95 96 PNODE temp = p; 97 q->pNext = p->pNext; 98 p->pNext = q; 99 100 return true; 101 102 } 103 104 } 105 106 bool List_Delete(PNODE pHead,int pos) 107 { 108 int i = 0; 109 PNODE p = pHead; 110 if(p == NULL) 111 { 112 return false; 113 } 114 else 115 { 116 while(p != NULL && i < pos-1) 117 { 118 i++; 119 p = p->pNext; 120 } 121 122 if(i > pos -1) return false; 123 124 PNODE temp = p->pNext; 125 p->pNext = temp->pNext; 126 free(temp); 127 temp =NULL; 128 129 return true; 130 131 } 132 } 133 134 bool List_Isempty(PNODE pHead) 135 { 136 if(pHead->pNext == NULL) 137 return true; 138 else 139 return false; 140 } 141 142 int List_len(PNODE pHead) 143 { 144 int len = 0; 145 PNODE p = pHead->pNext; 146 while(p != NULL) 147 { 148 len++; 149 p = p->pNext; 150 } 151 return len; 152 } 153 154 void List_sort(PNODE pHead) 155 { 156 int len = List_len(pHead); 157 int i,j; 158 PNODE p = pHead->pNext; 159 for(i = 0;i < len - 1; i++) 160 { 161 for(j = i+1,p = pHead->pNext;j < len;j++,p=p->pNext ) 162 { 163 /* 164 if(a[j] < a[i]) 165 { 166 int temp = a[i]; 167 a[i] = a[j]; 168 a[j] = temp; 169 } 170 */ 171 if(p->pNext->data < p->data) 172 { 173 int temp = p->data; 174 p->data = p->pNext->data; 175 p->pNext->data = temp; 176 } 177 } 178 } 179 180 181 } 182 183 184 int main(void) 185 { 186 PNODE pHead = Create_List(); 187 printf("链表长度:%d\r\n",List_len(pHead)); 188 if(List_Isempty(pHead)) 189 printf("链表为空\r\n"); 190 else 191 { 192 printf("链表不为空\r\n"); 193 Traverse_List(pHead); 194 List_sort(pHead); 195 printf("链表排序;\r\n"); 196 Traverse_List(pHead); 197 printf("链表插入;\r\n"); 198 List_Insert(pHead,3,100); 199 Traverse_List(pHead); 200 printf("链表删除;\r\n"); 201 List_Delete(pHead,4); 202 Traverse_List(pHead); 203 } 204 system("pause"); 205 return 0; 206 }
程序运行结果: