Hello C++ Nerds!
I am creating a list using Arrays. And I want to have AddItem, DeleteItem functions in those lists.
But as you know, you can't delete an element from an Array because it's a fixed size. But we can shift all the left elements by one place and therefore we delete that Item.
I did the same thing, but I got a problem.
and I am trying to implement a function that deletes an element from my array and the function does remove the element, but it duplicates the next element in array two times.
void SortedList::DeleteItem(ItemType x)
{
for (int i = 0; i < MAX_ITEMS; i++)
{
if (values[i] == x)
{
values[i] = values[i - 1];
}
}
}
List is like this:
1
2
3.3
4.4
5.5
6.2
7.1
8
9
10
after deleting 5.5, the output will be like:
1
2
3.3
4.4
4.4
6.2
7.1
8
9
10
Length is: 10
see that 4.4? I don't want two of them! I just want one.
How?
Full code is here:
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <fstream>
#include <string>
#define MAX_ITEMS 10
typedef float ItemType;
using namespace std;
int compareItems(const void * x, const void * y)
{
if (*(float *)x > *(float *)y)
{
return 1;
}
else if (*(float *)x < *(float *)y)
{
return -1;
}
return 0;
}
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
public:
SortedList();
void MakeEmpty();
void InsertItem(ItemType x);
void DeleteItem(ItemType x);
bool IsFull();
int Lengthls();
void RetrieveItem(ItemType &x, bool &found);
void ResetList();
void GetNextItem(ItemType &x);
float getItem(int index);
};
SortedList::SortedList()
{
length = 0;
}
void SortedList::MakeEmpty()
{
length = 0;
}
void SortedList::InsertItem(ItemType x)
{
if (length == (MAX_ITEMS))
{
cout << "we reached the max number of elements allowed" << endl;
return;
}
values[length++] = x;
//sort the array
qsort(values, length, sizeof(float), compareItems);
}
void SortedList::DeleteItem(ItemType x)
{
for (int i = 0; i < MAX_ITEMS; i++)
{
if (values[i] == x)
{
values[i] = values[i - 1];
}
}
}
int SortedList::Lengthls()
{
return length;
}
float SortedList::getItem(int index)
{
return values[index];
}
void SortedList::GetNextItem(ItemType &x)
{
}
int main()
{
SortedList Instance1;
//insert txt file into array
string line;
ifstream myfile("float.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
Instance1.InsertItem(atof(line.c_str()));
}
myfile.close();
}
else cout << "Unable to open file";
//delete item from array
//Instance1.DeleteItem(5.5);
//add item into array
//Instance1.InsertItem(10);
//print out the array to screen
for (int i = 0; i < Instance1.Lengthls(); i++)
{
cout << Instance1.getItem(i) << endl;
}
//print the length to screen
cout << "Length is: " << Instance1.Lengthls() << endl;
system("Pause");
return 0;
}