#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
CodePudding user response:
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;
}
void print()
{
for (int i = 0; i < counter; i )
{
cout << values[i] << " ";
}
}
I have corrected these two function. Try these functions instead of the one you implemented
CodePudding user response:
There are a few issues with your insertAt
function:
- You need to increase
counter
, notcapacity
. Yourcapacity
is fixed at construction time. It cannot change without allocating a new underlying array and copying the elements over. - You also should only run your loop from
counter
toindex
rather than starting atcapacity
, since the elements betweencounter
andcapacity
will be uninitialized assumingT
is a trivial type. - You shouldn't set
values[index] = item
until after the loop has finished moving all of the existing items. - You should
return true
after the loop, andreturn false
outside theif
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;
}
Note: You have a similar mistake in your print
function. You're printing all of the values up to capacity
, but the ones between counter
and capacity
are uninitialized. That loop should also terminate at counter
.
It's also worth pointing out that you never delete[] values
, so that memory will leak when your list
object dies. You also haven't followed the rule of three though, so if you add a destructor that delete[]
s values
you'll also need a copy-constructor and copy-assignment operator that copy the underlying array so that multiple list
objects don't try to delete[]
the same array.