<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdulganiyy Balogun</title>
    <description>The latest articles on DEV Community by Abdulganiyy Balogun (@devabdulganiyy).</description>
    <link>https://dev.to/devabdulganiyy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3692561%2Fd846d76f-ee06-4d0f-8f38-a9e154af253b.png</url>
      <title>DEV Community: Abdulganiyy Balogun</title>
      <link>https://dev.to/devabdulganiyy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devabdulganiyy"/>
    <language>en</language>
    <item>
      <title>Implement Array from Scratch using C++</title>
      <dc:creator>Abdulganiyy Balogun</dc:creator>
      <pubDate>Tue, 06 Jan 2026 15:54:42 +0000</pubDate>
      <link>https://dev.to/devabdulganiyy/implement-array-from-scratch-using-c-430j</link>
      <guid>https://dev.to/devabdulganiyy/implement-array-from-scratch-using-c-430j</guid>
      <description>&lt;p&gt;This tutorial will teach you how to implement array from scratch. By learning this, you will understand how different methods like push,shift,unshift,pop,concat where implemented in your favourite programming language.&lt;/p&gt;

&lt;p&gt;I will be using C++ in this tutorial as low level programming language helps in improving problem solving skills and become a better programmer. You should be able to follow along with basic knowledge of Javascript or C++ or any other languages.&lt;/p&gt;

&lt;p&gt;We will be covering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Creation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Insertion&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deletion&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creation
&lt;/h2&gt;

&lt;p&gt;The code is a class to create a list/array with the variable to hold the current length, maximum length and the memory address of the array in heap. When the class in called to instantiate a list object,  the size is initialised and the blocks of memory to store each data in the array next to each other and the address to these blocks is what is held in the &lt;strong&gt;arr&lt;/strong&gt; variable in the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Array {

    private:
      int *arr;
      int size;
      int length = 0;

    public:

       Array(){

          arr = new int[20];
      }

      Array(int sz){

          size = sz;
          arr = new int[size];
      }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we are setting the default maximum size of the array to 20 in the default class constructor and using the size the user specified in the second constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Insertion
&lt;/h2&gt;

&lt;p&gt;To insert any value in the array, we need to specify the index/position it should be inserted. We will check if the position is not out of range before inserting in any position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void insert(int index, int data){

          if(index &amp;lt; 0 || index &amp;gt; length) return -1;

          for(int i = length; i &amp;gt; index; i--){

              arr[i] = arr[i-1];
          }

          arr[index] = data;
          length++;
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to implement a push or unshift method for instance, we will always insert at the last index for push and first index for unshift using this insert function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get
&lt;/h2&gt;

&lt;p&gt;When we want to get a value from any position, we need to check if the position is not out of range.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int getAt(int index){

          if(index &amp;lt; 0 || index &amp;gt;= length) return -1;

          return arr[index];
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deletion
&lt;/h2&gt;

&lt;p&gt;Deleting a value at any index will need shifting all other values backwards to fill in the removed values. We also need to confirm that index is within range.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
void deleteAt(int index){

          if(index &amp;lt; 0 || index &amp;gt;= length) return;

          int x = arr[index];

          for(int i = index; i &amp;lt; index - 1; i++){

              arr[i] = arr[i+1];
          }

          length--;
      }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To implement unshift and pop methods, we need to just remove from the start and end for unshift and pop respectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

class Array {

    private:
      int *arr;
      int size = 20;
      int length = 0;

    public:

       Array(){

          arr = new int[20];
      }

      Array(int sz){

          size = sz;
          arr = new int[size];
      }

      void insert(int index, int data){

          if(index &amp;lt; 0 || index &amp;gt; length) return;

          for(int i = length; i &amp;gt; index; i--){

              arr[i] = arr[i-1];
          }

          arr[index] = data;
          length++;
      }

      void deleteAt(int index){

          if(index &amp;lt; 0 || index &amp;gt;= length) return;

          int x = arr[index];

          for(int i = index; i &amp;lt; index - 1; i++){

              arr[i] = arr[i+1];
          }

          length--;
      }

       void getAt(int index){

          if(index &amp;lt; 0 || index &amp;gt;= length) return -1;

          return arr[index];
      }

      void display(){

          for(int i = 0; i &amp;lt; length; i++){

              printf("%d \n",arr[i]);
          }
      }
};

int main()
{

    Array arr(20);
    arr.insert(0,2);
    arr.display();

    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>dsa</category>
    </item>
  </channel>
</rss>
