Use of Pointers For Lists

C++’ta diziler sabit uzunlukludur; yani program çalışırken bir diziyi doğrudan büyütüp küçültmek mümkün değildir. Bu yüzden, eleman ekleyip silmeyi destekleyen dinamik bir liste yapısı oluşturmak için bellek yönetimini kendimiz yapmamız gerekir. Aşağıdaki örnek, C++’ta new ve delete kullanarak basit bir dinamik dizi tabanlı liste (dynamic array list) yapısının nasıl uygulanacağını gösterir.

Bu yapıda:

  • addItem() → Mevcut diziyi kopyalayıp yeni bir eleman ekleyerek listeyi dinamik olarak büyütür
  • deleteItem() → İstenilen index’teki elemanı silip diziyi küçültür
  • Yapıcı (constructor) → Listeyi boş başlatır (size = 0, ptr = nullptr)
  • Yıkıcı (destructor) → Bellek sızıntısını önlemek için ayrılan belleği serbest bırakır

Bu örnek, C++’ta manuel bellek yönetiminin nasıl yapıldığını ve dinamik veri yapılarının temel mantığını anlamak için oldukça öğreticidir.

#ifndef LIST_H
#define LIST_H

class List {
public:
    List();  // Constructor
    ~List();  // Destructor
    void addItem(int item);  // Add integer item
    void deleteItem(int loc);  // Delete item

private:
    int *ptr = nullptr;
    int size = 0;  // Pointer and its size
};

#endif

Here’s the List.cpp

#include "List.h"
#include <stdexcept>

List::List() {
    // The constructor initializes size to 0 and ptr to nullptr
}

void List::addItem(int item) {
    int *newPtr = new int[size + 1];
    for (int i = 0; i < size; i++) {
        newPtr[i] = ptr[i];
    }
    newPtr[size] = item;
    delete[] ptr;
    ptr = newPtr;
    size++;
}

void List::deleteItem(int loc) {
    if (loc < 0 || loc >= size) {
        throw std::out_of_range("Index out of range");
    }

    int *newPtr = new int[size - 1];
    for (int i = 0; i < loc; i++) {
        newPtr[i] = ptr[i];
    }
    for (int i = loc + 1; i < size; i++) {
        newPtr[i - 1] = ptr[i];
    }
    delete[] ptr;
    ptr = newPtr;
    size--;
}

List::~List() {
    delete[] ptr;
}

Stay updated

Receive insights on tech, leadership, and growth.

Subscribe if you want to read posts like this

No spam. One email a month.

2 thoughts on “Use of Pointers For Lists

    • Author gravatar

      Are you sure the “delete[], delete” thing is not wrong? Something tells me this is a double delete on the same pointer. Also, this class could benefit if you allocated memory in chunks (have a “size” member and “allocated_size”, allocate e.g. 1024 bytes more when when “size” >= “allocated_size” and vice versa when freeing memory).

    • Author gravatar

      Thank you very much for your attention. The first delete [] ptr is to delete array elements. The second one is not necessarry and we delete ptr. Yout are right about “size” and “allocated_size”. However, I wanted to show people the use of pointers and I did not care about efficiency. It will be more beneficial if we use chunks.
      Thank you very much…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.