Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding proper data structure c++

I was looking for some simple implemented data structure which gets my needs fulfilled in least possible time (in worst possible case) :-

(1)To pop nth element (I have to keep relative order of elements intact)
(2)To access nth element .

I couldn't use array because it can't pop and i dont want to have a gap after deleting ith element . I tried to remove the gap , by exchanging nth element with next again with next untill last but that proves time ineffecient though array's O(1) is unbeatable .

I tried using vector and used 'erase' for popup and '.at()' for access , but even this is not cheap for time effeciency though its better than array .

like image 777
Manas Verma Avatar asked Oct 19 '12 14:10

Manas Verma


People also ask

What is data structure in C with example?

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.

How do you define a data structure in C?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).


2 Answers

What you can try is skip list - it support the operation you are requesting in O(log(n)). Another option would be tiered vector that is just slightly easier to implement and takes O(sqrt(n)). both structures are quite cool but alas not very popular.

like image 124
Ivaylo Strandjev Avatar answered Sep 27 '22 23:09

Ivaylo Strandjev


Well , tiered vector implemented on array would i think best fit your purpose . Though the tiered vector concept may be knew and little tricky to understand at first but then once you get it , it opens lot of question and you get a handy weapon to tackle many question's data structure part very effeciently . So it is recommended that you master tiered vectors implementation.

like image 27
Maggi Iggam Avatar answered Sep 28 '22 00:09

Maggi Iggam