-2

I am making a program in which I will have to make an element of an array empty as new.

int Heap[5];
//Heap[4]=Null/Empty/0

I don't want to remove element using stack because it doesn't actually remove an element. Plz tell me what is the correct Method for achieving this.

1 Answers1

0

C++ arrays does not have any empty elements. All elements has a value.

One approach is to use a sentinel value. Simply pick a value that fits in an int and use that to denote empty.

I'm more familiar with C than C++, but on the other hand, you seem to code things that are pretty close to C. So another approach is this:

struct Element {
    int value;
    bool isnull;
}

Element Heap[5];

To delete an element, simply do:

Heap[index].isnull = true;

Another approach that involves pointers:

int values[5];

int *Heap[5];

// Assign a value
values[index] = 42;
Heap[index] = &values[index];

// Delete a value
Heap[index] = NULL;
klutt
  • 30,332
  • 17
  • 55
  • 95