Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ list functions/methods // List.h #ifndef _LIST_H_ #define _LIST_H_ #include

ID: 3568199 • Letter: C

Question

C++ list functions/methods

// List.h
#ifndef _LIST_H_
#define _LIST_H_

#include <cstdlib>

class List {

public:
   List(size_t capacity = 10); // constructor - allocates dynamic array
   List(const List &a); // copy constructor
   ~List(); // destructor

   int& operator[](size_t pos); // bracket operator
   List& operator=(const List &a); // assignment operator
   List& operator+=(const List &a); // += operator

   void append(int item);
   //void extend(int item);
   //void index(int item);
   //void insert(int item);
   //void pop(int item);
   //void remove(int item);
   size_t size() const { return size_; }

private:
   void copy(const List &a);
   void resize(size_t new_size); // allocate new larger array
   int *data_; // dynamic array
   size_t size_; // size of dynamic array
   size_t capacity_; // capacity of dynamic array
};

inline int& List::operator[](size_t pos)
{
   return data_[pos];
}

#endif // _LIST_H_

// List.cpp
#include "List.h"

List::List(size_t capacity)
{
   data_ = new int[capacity];
   capacity_ = capacity;
   size_ = 0;
}

List::List(const List &list)
{
   copy(list);
}

List::~List()
{
   delete[] data_;
}

void List::copy(const List &list)
{
   size_t i;

   size_ = list.size_;
   capacity_ = list.capacity_;
   data_ = new int[list.capacity_];
   for (i = 0; i<list.capacity_; ++i) {
       data_[i] = list.data_[i];
   }
}

List& List::operator=(const List &list)
{
   if (&list != this) {
       // deallocate existing dynamic array
       delete[] data_;
       // copy the data
       copy(list);
   }
   return *this;
}

List& List::operator+=(const List &list)
{
   size_t i;
   size_t pos = size_;

   if ((size_ + list.size_) > capacity_) {
       resize(size_ + list.size_);
   }

   for (i = 0; i<list.size_; ++i) {
       data_[pos++] = list.data_[i];
   }
   size_ += list.size_;
   return *this;
}

// if the size_ and capacity_ are equal, create a larger array by doubling the original
// array & placing the array data into the doubled array
// the data will then be appended into the new, larger array
void List::append(int item)
{
   if (size_ == capacity_) {
       resize(2 * capacity_);
   }
   data_[size_++] = item;
}

//void List::extend(int item)
//{
//  
//}

//void List::index(int item)
//{
//  
//}

//void List::insert(int item)
//{
//  
//}

//void List::pop(int item)
//{
//  
//}

//void List::remove(int item)
//{
//  
//}

// should this method have a precondition? see end of chapter exercises
void List::resize(size_t new_size)
{
   int *temp;
   size_t i;

   capacity_ = new_size;
   temp = new int[capacity_];
   for (i = 0; i<size_; ++i) {
       temp[i] = data_[i];
   }
   delete[] data_;
   data_ = temp;
}

Explanation / Answer

how would you modify the following testcode,cpp to work with list.cpp and header?

#include <iostream>
#include "List.h"

using namespace std;

int main()
{
   List l;
   int i;
   size_t p;
   int val;
   char c; //choice of operation

   while(1)
   {
       cout << "Select an operation: " << endl;
       cout << "(i)nsert i(n)dex (p)op (r)emove (e)xtend (q)uit: " << endl;
       cin >> c;
       if (c == 'i')
       {
           cout << "Enter a value:";
           cin >> val;
           cout << "Enter a position:";
           cin >> p;
           l.insert(p, val);
       }
       if (c == 'n')
       {
           cout << "Enter a value:";
           cin >> val;
           cout << val << " has index " << l.index(val) << endl;
       }
       if (c == 'p')
       {
           cout << "Enter a position:";
           cin >> p;
           l.pop(p);
       }
       if (c == 'r')
       {
           cout << "Enter a value:";
           cin >> val;
           l.remove(val);
       }
       if (c == 'e')
       {
           List l2;
           cout << "Enter a new list:";
           cin >> l2;
           l += l2;
       }
       cout << "List = " << l << endl;
       if (c == 'q')
           break;


   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote