A couple of days ago I asked myself which data-structure I should in a function in C. I usually write in C++ and the choice would have fallen to std::vector.
There a some possible choices:
The last option seems to be unusual. Are there any bigger project where someone uses own structures like lists? Is there a general rule for the decision between array or own data-structures?
When I would need a tree structure I wouldn't think twice an just write a tree. Are there any widely used libs with such structures(like boost for C++)? Or is this considered as bad style because you would have to store a void* instead of the actual type?
Thank a lot for your experience!
Arrays. An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays. Here's an image of a simple array of size 4, containing elements (1, 2, 3 and 4).
Data Structures in C are used to store data in an organised and efficient manner. The C Programming language has many data structures like an array, stack, queue, linked list, tree, etc. A programmer selects an appropriate data structure and uses it according to their convenience.
Application of the Stack A Stack can be used for evaluating expressions consisting of operands and operators. Stacks can be used for Backtracking, i.e., to check parenthesis matching in an expression. It can also be used to convert one form of expression to another form. It can be used for systematic Memory Management.
An array is a linear data structure that collects elements of the same data type and stores them in contiguous and adjacent memory locations. Arrays work on an index system starting from 0 to (n-1), where n is the size of the array.
Different data structures offer different computational complexity for insertions, lookups and other operations.
To take your specific example, there is a number of differences between an array and a linked list:
O(1)
in an array and O(n)
in a list;O(1)
or O(n)
(depending on whether they require traversal), and into an array are amortized O(1)
if done well;O(n)
whereas certain types of list deletions can be done in O(1)
time;You might find the following page useful: http://essays.hexapodia.net/datastructures/
As a general rule, when choosing a data structure I first consider whether I have strong reasons to believe that performance of the code in question is going to be important:
As for recommendations for good C data structure libraries, take a look at Are there any open source C libraries with common data structures?
It completely depends on which operations you are going to perform on the data structure. If you will be retrieving data by index (eg, data[ 3 ]), then a list is a horrible idea since each read will require you to walk the list. If you will be inserting into the first position a lot (eg, data[ 0 ] = x), then an array will be terrible because you will be moving all the data for each insertion.
If you were going to use std::vector, then a dynamic array is probably the best replacement. But perhaps std::vector would not have been the correct choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With