DEV Community

Abhii
Abhii

Posted on

#0 Hello World of DATA STRUCTURES

According to wikipedia,

In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification.

In simple terms, it is a way of writing data which helps to manage, organise or modify data.

Arrays

  • Arrays are the most basic data structures.
  • Elements in array are stored in contiguous memory location.
  • Elements can be accessed with the help of their index.

We will be implementing our code in C++. Since logic is same for most of the languages you will only need to change the syntax to implement it in any other language.

Advantages of Arrays

  • Easy and random access to array elements.
  • Sorting of data can be done easily and in less code.
  • Traversal through array is easy.

Disadvantages of Arrays

  • Array size once specified cannot be changed.
  • Insertion and deletion is hectic as we have to move the elements to manage the continuous memory indexing.

Array declaration in C++

// Array declaration by specifying size
int arr1[5];

// Array declaration by specifying user defined size
int s = 5;
char arr2[s];

// Array declaration by initializing elements
float arr3[] = { 11.2, 34.5, 89.5, 32.09}

// above is same as "float arr3[4] = { 11.2, 34.5, 89.5, 32.09}"

Enter fullscreen mode Exit fullscreen mode

Accept elements from user in Array

int size, arr[size]; 

cout<<"Enter size of array: ";
cin>>size;    //prompt user to enter size of array

for (int i = 0; i < size; i++){
    cin>>arr[i];
}

Enter fullscreen mode Exit fullscreen mode

Insert at beginning of Array

void insertAtBeginning(int arr[], int size, int beg){
    if (isFull()){
        cout<<"Array is Full!";
    }

    else if (isEmpty()){
        arr[0] =  beg;
    }
    else{
        int arrSize = sizeof(arr[0])/sizeof(arr);
        for (int i = 0; i < arrSize; i++){
            arr[arrSize] = arr[arrSize - 1];
        }

        arr[0] = beg;
    }
}
Enter fullscreen mode Exit fullscreen mode

NOTE: Henceforth, only the algorithm will be discussed and it is expected that reader implements the algorithm by writing his own code.

Insert at position in Array

  1. First get the element to be inserted.
  2. Then get the position at which this element is to be inserted.
  3. Then shift the array elements from this position to one position forward, and do this for all the other elements next to pos.
  4. Insert the element at the given position like int[pos] = element
  5. Hope your element is inserted at proper position.

Insert at End

Oh c'mon... I know you can do it without the algorithm.

Delete the element from the Array
You can delete element either by getting it's position or by getting it's value
Delete element by Position

  1. Get the position of element to be deleted.
  2. Loop through the array from the position to end and moving each element one index down.

NOTE: Various testcases are ignored and are expected to be done by the reader.

Delete element by Value

  1. Get the value to be deleted.
  2. Loop through the array and search for the value. Remember to keep track of count/position.
  3. Once element is found, break the loop.
  4. Loop through the array from the position to end and moving each element one index down.

Update the value in Array

Dude...!! Seriously?!!
Just arr[pos] = newValue AS SIMPLE AS THAT.

Here are some problems to practice

  • Write a C++ program to find the second lowest and highest numbers in a given array.
  • Write a C++ program to arrange the numbers of a given array in a way that the sum of some numbers equal the largest number in the array.
  • Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
  • Write a C++ program to find and print all common elements in three sorted arrays of integers.

Find more at W3Resource

Enough theory now.. Just go around and mess with the stuff.
KEEP PRACTISING. STAY SAFE AND KEEP CODING!!

References

  1. https://en.wikipedia.org/wiki/Data_structure
  2. https://en.wikipedia.org/wiki/Array_data_structure
  3. https://www.geeksforgeeks.org/arrays-in-c-cpp/
  4. https://www.w3resource.com/cpp-exercises/array/index.php

Latest comments (7)

Collapse
 
nashmeyah profile image
Nashmeyah

This is awesome!

Collapse
 
abhilearnstocode profile image
Abhii

Thanks Nashmeyah for your encouraging words!

Collapse
 
pgradot profile image
Pierre Gradot • Edited

When I see functions such as isFull() or isEmpty(), you are no longer talking about arrays, but a more sophisticated data types. And by the way, because these functions have no parameter, you have some troubles to implement them ;)

But more important: in C++, you should probably use std::array or std::vector instead of plain C arrays, in most cases.

Collapse
 
abhilearnstocode profile image
Abhii

Hey Pierre,
I would love if you share some resources for us to learn more about Data Structures.

It would be a great help.

Collapse
 
pgradot profile image
Pierre Gradot

I don't have such documents available sorry ;)

Collapse
 
abhilearnstocode profile image
Abhii

Good point Pierre!
My focus is more to explain the logic rather than going into the syntax of the language.
And yes, you are right about the isFull() and isEmpty() has no parameters but again I focused more on explaining rather than validating my code.

Thank you for your suggestions buddy!

Collapse
 
pgradot profile image
Pierre Gradot

If code is not you main concern because you want to focus on algorithms, then you may use pseudo-code instead ;) Using a incomplete or incorrect may confuse your readers and distract them from your real purpose.