开发者

How do we implement the functions of list (which was in python) as arrays in C language? [closed]

开发者 https://www.devze.com 2023-02-16 04:20 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit 开发者_运维百科the help center. Closed 11 years ago.

I need to implement the "list" data-structure that we use in python. list in python : http://docs.python.org/tutorial/datastructures.html I have to implement the functions of list, considering the lists as arrays and need to allocate the memory to it dynamically. I need to use functions, pointers and structures wherever necessary. (In C language!)


To simulate linked list you must study these topics

1.Dynamic Memory allocation using malloc calloc realloc and free

2.Structures in c

3.Basic pointers in c For Dynamic Memory allocation you could visit

Dynamic Memory Allocation in c

A nice tutorial is given here Linked List in C

EDIT

The array implementation of a list is easier as compared to linked list implementaion.In array implementaion also you could use malloc to dynamically allocate memory for your array and realloc to increase/decrease the memory.

for e.g

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;

But before this you must study how arrays and pointers work in C language, and more specifically Dynamic memeory allocation part..


You need a data structure that contains a pointer to an array containing the data and an integer containing the size of the array. If you don't want to expand the array every time a new item is added, keep track of the number of items in the list also, so you can grow the array by more than is needed.

You can use malloc to create the array and data structure, realloc to grow the array, and free to free the array and data structure.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号