-3

I have seen insertion of element in array starting iteration from the rear end. But i wonder if it is possible to insert from the front

  • Assuming the array is large enough then copy each element at index X to index X+1 and then insert the new element into index 0. – kaylum Jan 02 '21 at 05:42
  • 2
    Are you talking about C, C++, or C#? Because C uses fixed-length arrays. Adding an element involves allocating another array and then copying over the items. – Ouroborus Jan 02 '21 at 05:43
  • You can't copy each element to the next starting from the beginning, because you'll end up copying same number to the rest of the array – Krishna Acharya Jan 02 '21 at 05:48
  • 1
    @KrishnaAcharya You can reverse the iteration. – KaiserKatze Jan 02 '21 at 05:48
  • 1
    If you have a [tag:C++20]-compatible compiler, you can check out [std::front_inserter](https://en.cppreference.com/w/cpp/iterator/front_inserter) when dealing with STD containers. – KaiserKatze Jan 02 '21 at 05:51
  • @KaiserKatze That's the thing which you are not allowed to do because i am working on circular priority queue – Krishna Acharya Jan 02 '21 at 05:51
  • 1
    Usually, when I want a data structure that allows me to perform insertion/deletion operations, I choose the [doubly linked list](https://en.wikipedia.org/wiki/Doubly_linked_list), which is not hard to implement as well as time-efficient. – KaiserKatze Jan 02 '21 at 05:53
  • its my assignment bro :( – Krishna Acharya Jan 02 '21 at 05:54
  • 1
    Did your assignment specifically say to use an array or was it just to create a priority queue? – Ouroborus Jan 02 '21 at 05:55
  • It said to use queue to implement circular priority queue.. – Krishna Acharya Jan 02 '21 at 05:59
  • 1
    Priority queues aren't normally implemented using an array because they require that you can do inserts between elements. If you're required to use an array (and you should clarify this with your teacher), you'd have to move the elements around in order to open up a slot for insertion. – Ouroborus Jan 02 '21 at 06:07
  • Yeah i'll definitely do that.. – Krishna Acharya Jan 02 '21 at 06:10

2 Answers2

0

I finally figured out a way, Here goes the code

 #include <stdio.h>
    int main()
    {
        int number = 5; //element to be inserted
        int array[10] = {1, 2, 3, 4, 6, 7, 8, 9};
        int ele, temp;
        int pos = 4; // position to insert
        printf("Array before insertion:\n");
        for (int i = 0; i < 10; i++)
        {
            printf("%d ", array[i]);
        }
        puts("");
    
        for (int i = pos; i < 10; i++)
        {
            if (i == pos) // first element
            {
                ele = array[i + 1];
                array[i + 1] = array[i];
            }
            else // rest of the elements
            {
                temp = array[i + 1];
                array[i + 1] = ele;
                ele = temp;
            }
        }
        array[pos] = number; // element to be inserted
        printf("Array after insertion:\n");
        for (int i = 0; i < 10; i++)
        {
            printf("%d ", array[i]);
        }
    
        return 0;
    }

The output looks like:

Array before insertion:
1 2 3 4 6 7 8 9 0 0
Array after insertion:
1 2 3 4 5 6 7 8 9 0
Flexo
  • 82,006
  • 22
  • 174
  • 256
0

In C the arrays have a "native" built-in implementation based upon the address (aka pointer) to the first element and a the [] operator for element addressing.

Once an array has been allocated, its actual size is not automatically handled or checked: the code needs to make sure boundaries are not trespassed.

Moreover, in C there is no default (aka empty) value for any variable, there included arrays and array element.

Still, in C there's no such a thing like insertion, appending or removal of an array element. You can simply refer to the n-th (with n starting at 0) array element by using the [] operator. So, if you have an array, you cannot insert a new item at its n-th position. You can only read or (over)write any of its items.

Any other operation, like inserting or removing, requires ad-hoc code which basically boils down to shifting the arrays elements forward (for making room for insertion) or backward (for removing one). This is the C-language nature and should not be seen as a limitation: any other language allowing for those array operations must have a lower-level hidden implementation or a non-trivial data structure to implement the arrays.

This means, in C, that while keeping the memory usage to a bare minimum, those array operations require some time-consuming implementation, like the item-shifting one. You can then trade-off the memory usage against the time usage and get some gains in overall efficiency by using, for example, single- and double-linked lists. You loose some memory for link pointer(s) in favor of faster insertion ad removal operations. This depends mostly upon the implementation goals.

Finally, to get to the original question, an actual answer requires some extra details about the memory vs time trade off that can be done to achieve the goal.

The solution depicted by @Krishna Acharya is a simple shift-based solution with no boundary check. A very simple and somehow naive implementation.

A final note. The 0s shown by Krishna's code at the end of the arrays should be considered merely random values. As I said earlier, there is no default value. The code should have been instead:

int array[10] = {1, 2, 3, 4, 6, 7, 8, 9, 0, 0};

in order to make sure that any unused value was 0 for the last two array elements.

EnzoR
  • 2,523
  • 2
  • 16
  • 19