Answer to Question #242115 in C++ for barki

Question #242115

implement a list data structure discussed above including following operations using array adt. ◼ get() ◼ update() ◼ length() ◼ back() ◼ next() ◼ start() ◼ end() ◼ remove() ◼ add()


1
Expert's answer
2021-09-25T05:57:19-0400
#include <iostream>
using namespace std;
template<class T>
class List{
    T *data;
    int size, pos = 0;
    public:
    List(){}
    List(T array[], int size){
        this->size = size;
        this->data = new T[size];
        for(int i = 0; i < size; i++){
            this->data[i] = array[i];
        }
    }
    T* get(int index){
        if(0 <= index && index < size) return &data[index];
        return NULL;
    }
    void add(T data){
        if(size == 0){
            this->data = new T[++size];
        }
        else{
            this->data = (T*) realloc(this->data, sizeof(T) * ++size);
        }
        this->data[size - 1] = data;
    }
    void remove(int index){
        if(0 > index || index >= size) return;
        T *temp = new T[size];
        for(int i = 0, j = 0; i < size; i++){
            if(i == index) continue;
            temp[j] = this->data[i];
            j++;
        }
        T *temp2 = this->data;
        this->data = (T*) realloc(temp, --size * sizeof(T));
        delete temp2;
    }
    int length(){
        return size;
    }
    void update(int index, T data){
        if(0 > index || index >= size) return;
        this->data[index] = data;
    }
    T* next(){
        if(pos >= size) return NULL;
        return &this->data[pos++];
    }
    T* back(){
        if(pos <= 0) return NULL;
        return &this->data[--pos];
    }
    T start(){
        return this->data[0];
    }
    T end(){
        return this->data[size - 1];
    }
};
template<typename type>
void display(List<type> list){
    int i = 0;
    while(type *x = list.get(i)){
        cout<<*x<<" ";
        i++;
    }
    cout<<endl;
}
int main(){
    int array[6] = {4, 5, 6, 7, 8, 9};
    List<int> list(array, 6);
    cout<<"Size: "<<list.length()<<endl;
    cout<<"Start: "<<list.start()<<endl;
    cout<<"End: "<<list.end()<<endl;


    cout<<"\nCalling Next: ";
    while(int *x = list.next()) cout<<*x<<" ";


    cout<<"\nCalling back: ";
    while(int *x = list.back()) cout<<*x<<" ";


    cout<<"\n\nAdding 10: \n";
    list.add(10);
    cout<<"Size: "<<list.length()<<endl;
    cout<<"Contents: "; display(list);


    cout<<"\nRemoving item at index 3: \n";
    list.remove(3);
    cout<<"Size: "<<list.length()<<endl;
    cout<<"Contents: "; display(list);


    cout<<"\nUpdating item at index 2 to 8: \n";
    list.update(2, 8);
    cout<<"Size: "<<list.length()<<endl;
    cout<<"Contents: "; display(list);


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS