Sonzaschool
Rudi

Sekondari ya Juu · Kidato cha Sita

Sayansi ya Kompyuta

Create a program that implements array and a linked list data structure using object-oriented programming language

takriban dakika 9 kusoma

Mada za sehemu hiiDemonstrate mastery of basic principles of Algorithms and Data structuresMada 7

Implementing Arrays and Linked Lists Using Object-Oriented Programming

Data structures are methods of storing and organising data in a computer so that it can be used efficiently. In this study note, you will learn how to implement two fundamental linear data structures—arrays and linked lists—using an object-oriented programming language (C++).

An array is a data structure used to store a collection of elements of the same data type in contiguous memory locations. Each element is identified by an index, which allows fast and direct access.

Declaring and Initialising Arrays in C++

#include <iostream>
using namespace std;

int main() {
    // Declaring an array of 5 integers
    int marks[5] = {60, 70, 80, 75, 90};
    
    // Accessing elements using index (starts from 0)
    cout << "First element: " << marks[0] << endl;
    cout << "Third element: " << marks[2] << endl;
    
    return 0;
}

Key points:

  • Array index in C++ starts from 0, not 1
  • The size must be defined at compile time (static)
  • All elements must be of the same data type

Basic Array Operations

Insertion in an array:

#include <iostream>
using namespace std;

int main() {
    int arr[6] = {10, 20, 30, 40, 50};
    int n = 5;         // current number of elements
    int pos = 2;       // position to insert (index 2)
    int value = 25;    // value to insert
    
    // Shift elements to the right to create space
    for (int i = n; i > pos; i--) {
        arr[i] = arr[i - 1];
    }
    arr[pos] = value;
    n++;
    
    // Display the array after insertion
    cout << "Array after insertion: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

Output: 10 20 25 30 40 50

Deletion from an array:

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int n = 5;
    int pos = 2; // delete element at index 2
    
    // Shift elements left to fill the gap
    for (int i = pos; i < n - 1; i++) {
        arr[i] = arr[i + 1];
    }
    n--;
    
    // Display the array after deletion
    cout << "Array after deletion: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

Output: 10 20 40 50

Limitations of Arrays

  1. Fixed size – cannot grow or shrink during execution
  2. Memory wastage – unused allocated space cannot be reused
  3. Inefficient insertion/deletion – requires shifting elements

Swali

In C++, if you declare an array as int marks[5] = {60, 70, 80, 75, 90};, what is the index used to access the value 80?

Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.

Ingia ili kufanya mazoezi

Mwalimu

Umekwama? Niulize chochote kuhusu mada hii.

Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.

Ingia ili kuuliza