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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| #include <stdio.h> #include <stdlib.h> #ifndef _SINGLY_LINKED_LIST_H_H #define _SINGLY_LINED_LIST_H_H
typedef struct Node { int data; struct Node *pNext; } NODE, *pNODE;
pNODE CreateSgLinkList(void);
void TraverseSgLinkList(pNODE pHead);
int IsEmptySgLinkList(pNODE pHead);
int GetLengthSgLinkList(pNODE pHead);
int InsertEleSgLinkList(pNODE pHead, int pos, int data);
int DeleteEleSgLinkList(pNODE pHead, int pos);
void FreeMemory(pNODE *ppHead);
#endif
pNODE CreateSgLinkList(void) { int i, length, data; pNODE p_new = NULL, pTail = NULL; pNODE pHead = (pNODE)malloc(sizeof(NODE));
if (NULL == pHead) { printf("内存分配失败!\n"); exit(EXIT_FAILURE); } pHead->data = 0; pHead->pNext = NULL; pTail = pHead; printf("请输入要创建链表的长度:"); scanf("%d", &length); for (i=1; i<length+1; i++) { p_new = (pNODE)malloc(sizeof(NODE)); if (NULL == p_new) { printf("内存分配失败!\n"); exit(EXIT_FAILURE); } printf("请输入第%d个节点的值:", i); scanf("%d", &data); p_new->data = data; p_new->pNext = NULL; pTail->pNext = p_new; pTail = p_new; } return pHead;
}
void TraverseSgLinkList(pNODE pHead) { pNODE pt = pHead->pNext;
printf("打印链表:"); while (pt != NULL) { printf("%d ", pt->data); pt = pt->pNext; } putchar('\n');
}
int IsEmptySgLinkList(pNODE pHead) { if (pHead->pNext == NULL) return 1; else return 0; }
int GetLengthSgLinkList(pNODE pHead) { int length = 0; pNODE pt = pHead->pNext;
while (pt != NULL) { length++; pt = pt->pNext; } return length;
}
int InsertEleSgLinkList(pNODE pHead, int pos, int data) { pNODE pt = NULL, p_new = NULL;
if (pos > 0 && pos < GetLengthSgLinkList(pHead) + 2) { p_new = (pNODE)malloc(sizeof(NODE)); if (NULL == p_new) { printf("内存分配失败!\n"); exit(EXIT_FAILURE); } p_new->data = data; while (1) { pos--; if (0 == pos) break; pHead = pHead->pNext; } pt = pHead->pNext; pHead->pNext = p_new; p_new->pNext = pt; return 1; } else return 0;
}
int DeleteEleSgLinkList(pNODE pHead, int pos) { pNODE pt = NULL;
if (pos > 0 && pos < GetLengthSgLinkList(pHead) + 1) { while (1) { pos--; if (0 == pos) break; pHead = pHead->pNext; } pt = pHead->pNext->pNext; free(pHead->pNext); pHead->pNext = pt; return 1; } else return 0;
}
void FreeMemory(pNODE *ppHead) { pNODE pt = NULL; while (*ppHead != NULL) { pt = (*ppHead)->pNext; free(*ppHead); *ppHead = pt; } }
int main(void) { int flag = 0, length = 0; int position = 0, value = 0; pNODE head = NULL;
head = CreateSgLinkList(); flag = IsEmptySgLinkList(head); if (flag) printf("单向链表为空!\n"); else { length = GetLengthSgLinkList(head); printf("单向链表的长度为:%d\n", length); TraverseSgLinkList(head); } printf("请输入要插入节点的位置和元素值(两个数用空格隔开):"); scanf("%d %d", &position, &value); flag = InsertEleSgLinkList(head, position, value); if (flag) { printf("插入节点成功!\n"); TraverseSgLinkList(head); } else printf("插入节点失败!\n"); flag = IsEmptySgLinkList(head); if (flag) printf("单向链表为空,不能进行删除操作!\n"); else { printf("请输入要删除节点的位置:"); scanf("%d", &position); flag = DeleteEleSgLinkList(head, position); if (flag) { printf("删除节点成功!\n"); TraverseSgLinkList(head); } else printf("删除节点失败!\n"); } FreeMemory(&head); if (NULL == head) printf("已成功删除单向链表,释放内存完成!\n"); else printf("删除单向链表失败,释放内存未完成!\n"); return 0;
}
|