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
| #include<stdio.h> #include<iostream> #include<stdlib.h> #include<time.h>
#define true 1 #define false 0
typedef struct Node { int data; struct Node* next; }Node; Node* Initializelist() { Node* root = (Node*)malloc(sizeof(Node)); root->data = 0; root->next = NULL; return root; } void Headinsert(Node* root, int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = root->next; root->next = node; root->data++; } int ishavadata(Node* root, int data) { Node* node = root->next; while (node) { if (node->data == data) { return true; } node = node->next; } return false; } void Tailinsert(Node* root, int data) { Node* beginroot = root; for (int i = 0;i < root->data;i++) { beginroot = beginroot->next; } Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = beginroot->next; beginroot->next = node; root->data++; } void deletedata(Node* root, int data) { if (ishavadata(root, data)) { Node* prenode = root; Node* node = root->next; while (node) { if (node->data == data) { prenode->next = node->next; free(node); root->data--; printf("删除成功!(^_^)"); break; } else { prenode = node; node = node->next; } } } else { printf("该数据在这个链表中不存在!删除失败!"); } } void checkdata(Node* root, int data) { Node* node = root->next; int ans = 1; while (node) { if (node->data == data) { printf("存在这个链表中,而且下标为:%d\n", ans); } node = node->next; ans++; } printf("不存在这个数据在这个链表中!!\n"); } void insertdata(Node* root, int data, int unmber) { Node* node = root->next; int ans = 1; if (unmber < 0) { printf("抱歉!,你输入的下标小于0!不正确!\n"); } else if (unmber == 0) { Headinsert(root, data); printf("插入成功!\n"); } else if (unmber == root->data) { Tailinsert(root, data); printf("插入成功!"); } else if (unmber > root->data) { printf("你选择插入的链表的位置不正确!超出了链表的长度!!!\n"); } else { while (node) { if (ans == unmber) { Node* newnode = (Node*)malloc(sizeof(Node)); newnode->data = data; newnode->next = node->next; node->next = newnode; root->data++; printf("插入成功!\n"); break; } node = node->next; ans++; } } } void reverse(Node* root) { if (root->data == 0) { printf("空链表!"); } else if (root->data == 1) { return; } else { Node* fristnode = root->next; Node* secondnode = fristnode->next; root->data = 1; while (secondnode) { Node* thirdnode = secondnode; Headinsert(root, thirdnode->data); fristnode->next = secondnode->next; secondnode = secondnode->next; } } } void printnode(Node* root) { Node* node = root->next; printf("root"); while (node) { printf("->%d", node->data); node = node->next; } printf("\n这个链表的长度是:%d\n", root->data); } void mergenode(Node* fristnode, Node* secondnode) { Node* pa = fristnode->next; Node* pb = secondnode->next; Node* pc = fristnode; pc->data = fristnode->data + secondnode->data; while (pa && pb) { if (pa->data >= pb->data) { pc->next = pa; pc = pa; pa = pa->next; } else { pc->next = pb; pc = pb; pb = pb->next; } } pc->next = (pa) ? pa : pb; free(secondnode); secondnode = NULL; } int main() { srand((unsigned int)time(NULL)); int app = 10; Node* root = Initializelist(); while (app) { if (root->data == 0) { int numberdata = rand() % 999 + 100; Headinsert(root, numberdata); app--; } else { int numberdata = rand() % 999 + 100; if (ishavadata(root, numberdata)) { continue; } else { Headinsert(root, numberdata); app--; } } } printnode(root); reverse(root); printnode(root); checkdata(root, 234); checkdata(root, 667); checkdata(root, 112); insertdata(root, 30000, 8); insertdata(root, 9999999, 45); printnode(root); Node* rootsortup = Initializelist(); Node* rootsortdown = Initializelist(); Headinsert(rootsortup, 9999); Headinsert(rootsortup, 345); Headinsert(rootsortup, 222); Headinsert(rootsortup, 111); Headinsert(rootsortup, 23); Headinsert(rootsortup, 22); Headinsert(rootsortup, 13); printf("upsort"); printnode(rootsortup); Tailinsert(rootsortdown, 2); Tailinsert(rootsortdown, 23); Tailinsert(rootsortdown, 231); Tailinsert(rootsortdown, 235); Tailinsert(rootsortdown, 300); Tailinsert(rootsortdown, 8888); printf("downsort"); printnode(rootsortdown); reverse(rootsortup); reverse(rootsortdown); mergenode(rootsortdown, rootsortup); printf("mergesort"); printnode(rootsortdown); return 0; }
|