Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to insert an element at some specific position in the list using classes?

#include<iostream>
using namespace std;
template<typename T>
class List
{
public:
    T *values;
    int capacity;
    int counter;
public:
    List()
    {
        values = NULL;
        capacity = 0;
        counter = 0;
    }
    List(int cap)
    {
        capacity = cap;
        values = new T[cap];
        counter = 0;
    }
    bool insert(T item)
    {
        if (isFull() == false)
        {
            values[counter] = item;
            counter++;
            return true;
        }
        return false;
    }
    bool insertAt(T item, int index)
    {
        
        if (isFull() == false && index < counter)
        {
            capacity++;
            for (int i = capacity; i > index; i--)
                values[i] = values[i - 1];
            
            values[index] = item;
            
            return true;
        }
        return false;
    }
    bool isFull()
    {
        if (counter == capacity)
        {
            return true;
        }
        return false;
    }
    void print()
    {
        for (int i = 0; i < capacity; i++)
        {
            cout << values[i] << " ";
        }
    }
    
};
int main()
{
    List<int> obj1(5);
    obj1.insert(1); //0
    obj1.insert(2); //1
    obj1.insert(3); //2
    obj1.insert(4); //3
    
    obj1.insertAt(3, 1);
    obj1.values[1];
    obj1.print();
    
}

Kindly look into this program I have to insert an element at given position. But when I run this program I am getting garbage at the end element of an array. Kindly check and let me know where is the problem? Please check the insertAt function I think this function has some logical error. I have added the main function when I call print function it give garbage at the last index

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are a few issues with your insertAt function:

  1. You need to increase counter, not capacity. Your capacity is fixed at construction time. It cannot change without allocating a new underlying array and copying the elements over.
  2. You also should only run your loop from counter to index rather than starting at capacity, since the elements between counter and capacity will be uninitialized assuming T is a trivial type.
  3. The loop should run be inclusive of index
  4. You shouldn’t set values[index] = item until after the loop has finished moving all of the existing items.
  5. You should return true after the loop, and return false outside the if block
bool insertAt(T item, int index)
{
    if (!isFull() && index < counter)
    {
        for (int i = counter; i >= index; i--)
            values[i] = values[i - 1];    
        }
        values[index] = item;
        counter++;
        return true;
    }
    return false;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading