运行 ❯
获取你的
自己的
网站
×
更改方向
更改主题,暗色/亮色
前往 Spaces
#include <stdio.h> #include <stdlib.h> struct list { int *data; // Points to the memory where the list items are stored int numItems; // Indicates how many items are currently in the list int size; // Indicates how many items fit in the allocated memory }; void addToList(struct list *myList, int item); int main() { struct list myList; int amount; // Create a list and start with enough space for 10 items myList.numItems = 0; myList.size = 10; myList.data = malloc(myList.size * sizeof(int)); // Find out if memory allocation was successful if (myList.data == NULL) { printf("Memory allocation failed"); return 1; // Exit the program with an error code } // Add any number of items to the list specified by the amount variable amount = 44; for (int i = 0; i < amount; i++) { addToList(&myList, i + 1); } // Display the contents of the list for (int j = 0; j < myList.numItems; j++) { printf("%d ", myList.data[j]); } // Free the memory when it is no longer needed free(myList.data); myList.data = NULL; return 0; } // This function adds an item to a list void addToList(struct list *myList, int item) { // If the list is full then resize the memory to fit 10 more items if (myList->numItems == myList->size) { myList->size += 10; myList->data = realloc( myList->data, myList->size * sizeof(int) ); } // Add the item to the end of the list myList->data[myList->numItems] = item; myList->numItems++; }
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