From 1ea2ab43947a1bc1693d47cee42334736da96bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=91=E9=BE=99?= <17352730732@163.com> Date: Fri, 5 Jan 2024 13:52:07 +0000 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E7=AB=A0=E5=AE=9E=E9=AA=8C?= =?UTF-8?q?=E5=AE=9E=E9=AA=8C=E5=8D=81=E5=85=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\346\215\256\347\273\223\346\236\204.cpp" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "2224020101/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\357\274\214\345\210\240\351\231\244\344\270\216\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" diff --git "a/2224020101/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\357\274\214\345\210\240\351\231\244\344\270\216\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" "b/2224020101/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\357\274\214\345\210\240\351\231\244\344\270\216\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" new file mode 100644 index 00000000..e80bc522 --- /dev/null +++ "b/2224020101/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\357\274\214\345\210\240\351\231\244\344\270\216\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" @@ -0,0 +1,70 @@ +#include //实验十六 +#include + +#define MAX_SIZE 1000 + +typedef struct Node { + int data; + struct Node* next; +} Node; + +typedef struct { + Node* array[MAX_SIZE]; + int size; +} LinkedArray; + +Node* createNode(int data) { + Node* newNode = (Node*)malloc(sizeof(Node)); + if (!newNode) { + return NULL; + } + newNode->data = data; + newNode->next = NULL; + return newNode; +} + +void insert(LinkedArray* la, int index, int data) { + if (index < 0 || index > la->size) { + printf("Invalid index\n"); + return; + } + Node* newNode = createNode(data); + if (!newNode) { + return; + } + for (int i = la->size; i > index; i--) { + la->array[i] = la->array[i - 1]; + } + la->array[index] = newNode; + la->size++; +} + +void delete(LinkedArray* la, int index) { + if (index < 0 || index >= la->size) { + printf("Invalid index\n"); + return; + } + for (int i = index; i < la->size - 1; i++) { + la->array[i] = la->array[i + 1]; + } + la->size--; +} + +int findByIndex(LinkedArray* la, int index) { + if (index < 0 || index >= la->size) { + printf("Invalid index\n"); + return -1; + } + return la->array[index]->data; +} + +int main() { + LinkedArray la = { .array = {NULL}, .size = 0 }; + insert(&la, 0, 1); + insert(&la, 1, 2); + insert(&la, 2, 3); + printf("%d\n", findByIndex(&la, 1)); // Output: 2 + delete(&la, 1); + printf("%d\n", findByIndex(&la, 1)); // Output: 3 (previously it was 2) + return 0; +} \ No newline at end of file -- Gitee