Skip to content

Data structures which can be useful in your work with C programming language

License

Notifications You must be signed in to change notification settings

wiseStann/CCollections

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

- About this project

        I am a big fan of C, that's why I want to make work with this language more comfortable and easier. So I've decided to write a library of generic data structures. Working on this project, I had a great experience in C programming and data structures. As we know, C doesn't have special collections like linked lists or stacks and this is rather terrible because embedded arrays often can't satisfy all our needs. First of all, they have a fixed size, i.e. they can't be dynamically expanded. Secondly, we can put the data only of the same type into array that also isn't convenient. So, I present to you the library of generic data structures! But first thing's first.

Contents

- What is a Data Structure?

Data structure is a way in which data are stored for efficient search and retrieval.

        Different data structures are suited for different problems. Some data structures are useful for simple general problems, such as retrieving data that has been stored with a specific identifier. For example, an online dictionary can be structured so that it can retrieve the definition of a word. On the other hand, specialized data structures have been devised to solve complex specific search problems.

- Why should we learn data structures?

        Computer software is all about handling data. Data covers all types of information and the more basic forms are numbers and strings. Data structures explain how data is organized. This structure dictates how fast and efficient your code will run, that's why, depending on what type of data structure you have chosen, your program can be executed in a millisecond or in a minute. Once you deeply understand how data structures work, your code will run better and be prettier. On the data scientist side, we all write code, but writing good code also opens you up to software engineering opportunities. Surely, learning something that allows you to be good in your own craft while expanding into other lucrative fields can’t be that bad. Data structures play crucial role for programmers.

- About popular data structures

Array

        An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). Each element can be uniquely identified by their index in the array. This data structure is the base of majority of data structures and it isn't surprising.

        As it was said above, this library is the library of generic data structures and array is not an exception. The usage of Array data structure is truly simple:



Linked List

        One disadvantage of using arrays to store data is that arrays are static structures and therefore can't be easily extended or reduced to fit the data set. Unlike arrays, the elements in linked lists are not stored at contiguous memory locations. They are linked using pointers as shown in the image above. Each element of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to null. A linked list is a dynamic data structure, therefore number of nodes in a list is not fixed and can grow and shrink on demand. Any application which has to deal with an unknown number of objects will need to use a linked list.

        Let's see what features the library provides to use a linked list data structure.



Stack


        A stack is a basic data structure that can be logically thought of as a linear structure represented by a real physical stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of your data set as a stack of books or cards where you can only take the top item off the stack in order not to destroy all the stack of things. The basic implementation of a stack is also called a LIFO (Last In First Out) to demonstrate the way it accesses data.

        In my library you can find two implementations of Stack, viz. dynamic array-based and singly linked list-based. Well, that's how you can use Stack in your code.




- How to build and run the library

- Building

      Before we start, there are some pre requirements for your platform, viz. cmake software. For Linux cmake can be installed by the next command:

$ sudo apt-get -y install cmake

      The most important part of this README is the build and run instructions which you can also find in INSTRUCTIONS.md
First of all, you need to clone this repository on your local PC:

$ git clone https://github.com/wiseStann/CCollections.git

When you clone this repository on your local machine, change your current directory to this Project folder:

$ cd CCollections

To build a pipeline Cmake needs to know where is the source folder and where is binary folder. The source folder is where your CMakeLists.txt is located. Binary folder you have to create and usually it is named 'build'. Using an empty "build" directory is a technique called "out-of-source build", in which all your generated files are generated in the separate "build" directory and not mixed with source files. So, make a 'build' folder and go into it:

$ mkdir build
$ cd build

The next what you have to do is to execute 'cmake' command with specifying directory with source files, in order to generate Makefile following the instructions in CMakeLists.txt in root directory. In our case directory with source files is the parent directory so you just type in dot 2 times:

$ cmake ..

The next step is to execute 'make' command which builds executable programs and libraries from source code:

$ make

When all files are generated, you should install the public headers, in order to use it in your programs. By default, all libraries will be installed in usr/local/include directory:

$ sudo make install

- Running

      Well, now we have built the project and we want to use the library in our code. Let's assume that we have a file 'test.c':

#include <stdio.h>
#include <stdlib.h>
#include <Collections/array.h>
#include <Collections/array_sortings.h>

int main(int argc, char** argv)
{
    Array* arr = arrayNew();
    arrayToEnd(arr, (void*)30);
    arrayToEnd(arr, (void*)20);
    arrayToEnd(arr, (void*)10);
    Array* sorted_array = arraySortNew(arr, bubbleSort);
    
    void* begin = arrayGetBegin(sorted_array);
    printf("First element of array: %d", toInteger(begin));
}

You want to compile this code and run the binary. You can do it with Static and Dynamic Linking:

  • Static. To link your code statically, you have to add flag -static to the command and directly tell compiler what the lib should be used:

(GCC compiler)

$ gcc test.c -static -lCollections -o test

(CLANG compiler)

$ clang test.c -static -lCollections -o test
  • Dynamic. To link your code dynamically, you can just remove -static flag from your command. You just have to tell compiler what the library should be used addind -l flag:

(GCC compiler)

$ gcc test.c -lCollections -o test

(CLANG)

$ clang test.c -lCollections -o test

Now you can easily execute your binary from the current directory:

./test

And your console should show you First element of array = 10

About

Data structures which can be useful in your work with C programming language

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published