我需要实现我们在 python 中使用的“列表”数据结构。 python 中的列表:http://docs.python.org/tutorial/datastructures.html 我必须实现列表的功能,将列表视为数组,并且需要动态为其分配内存。 我需要在必要时使用函数、指针和结构。 (C语言!)
最佳答案
要模拟链表,您必须研究这些主题
1.动态内存分配使用malloc calloc realloc and free
2.c中的结构
3.c中的基本指针 对于动态内存分配,您可以访问
Dynamic Memory Allocation in c
这里给出了一个很好的教程Linked List in C
编辑
与链表实现相比,列表的数组实现更容易。在数组实现中,您还可以使用 malloc 为数组动态分配内存,并使用 realloc 来增加/减少内存。
例如
int *base;
int initial_size; //Take Input from user.
base=(int *)malloc(sizeof(int) * initla_size);
if(!base) //Ensure if memory is allocated
{
//Rest of the code
}
Now to insert eleements you could simply use
base[i]=element;
但在此之前,您必须研究数组和指针在 C 语言中的工作原理,更具体地说是动态内存分配部分。
关于c - 我们如何在C语言中将list(Python中的)函数实现为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5281226/