Using dynamic array implement the following :
1. Create the list. The list is initialized to an empty state.
2. Determine whether the list is empty.
3. Determine whether the list is full.
4. Find the size of the list.
5. Destroy, or clear, the list.
6. Determine whether an item is the same as a given list element.
7. Insert an item in the list at the specified location.
8. Remove an item from the list at the specified location.
9. Replace an item at the specified location with another item.
10. Retrieve an item from the list at the specified location.
11. Search the list for a given item
#include<iostream>
using namespace std;
class ListN : public List {
Litemtype* listArr;
static const int DEFAULT_SIZE = 10;
int MS;
int Lsize;
int current;
public:
AList(int S = DEFAULT_SIZE) :Lsize(0), curr(0) {
MS = S;
listArray = new ListItemType[S];
}
~ListN() { delete [] listArr; }
void clear() {Lsize = current = 0; }
bool insert(const Litemtyp& it) {
if (Lsize >= MS) return false;
for (int i = Lsize; i > curr; i--)
listArr[i] =listArr[i-1];
listArr[current] = it;
return true;
}
bool append(const Litemtype& it) {
if (Lsize >= MS) return false;
listArr[Lsize++] = it;
return true;
}
Litemtype remove() {
if ((current < 0) || (current >=Lsize))
throw std::out_of_range("current of" + to_string(current) + " and size of "
+ to_string(listSize) + " invalid element");
Litemtype it = listArr[current];
for(int i = current; i < Lsize-1; i++)
listArr[i] = listArr[i+1];
Lsize--;
return it;
}
void moveToStart() {current = 0; }
void moveToEnd() {current = Lsize; }
void delete(){cout<<"Enter the item to delete: ";
cout<<"Item successfully deleted: ";
}
void prev() { if (current != 0) current--; }
void search(){
}
void next() { if (current < Lsize) current++; }
int length() { return Lsize; }
int currPos() { return current; }
bool moveToPos(int pos) {
if ((pos < 0) || (pos >Lsize)) return false;
current = pos;
return true;
}
bool isAtEnd() { return current == listSize; }
Litemtype getValue() {
if ((current < 0) || (current >= lsize))
throw std::out_of_range("current " + to_string(current) + + " and size of "
+ to_string(Lsize) + "invalid element");
return listArr[current];
}
bool isEmpty() { return Lsize == 0; }
};
Comments
Leave a comment