Question

//C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;   ...

//C++ program

#include<iostream>
using namespace std;

template<typename T>
class list{
   private:
       T*arr;
       int size;
       int capacity;
      
       void resize(){
           capacity*=2;
           T * newArr = new T[capacity];
           for(int i=0;i<size;i++){
               newArr[i] = arr[i];
           }
           delete(arr);
           arr = newArr;
          
       }
      
   public:
       list(){
           size=0;
           capacity = 1;
           arr = new T[capacity];
       }
      
       bool isFull(){
           return size==capacity;
       }
       bool isEmpty(){
           return size==0;
       }
      
       void insert(T data){
           if(isFull())resize();
           arr[size++] = data;
       }
       int getSize(){
           return size;
       }
   void printList(){
       for(int i=0;i<size;i++){
           cout<<arr[i]<<" ";
           }
           cout<<"\n";
       }
};

int main(){
   list<int> l1;
   for(int i=0;i<10;i++){
       l1.insert(i);
       l1.printList();
   }
   return 0;
}

CHANGE THE MAIN AND ANY NEEDED CHANGES IN THE CLASS!!!!!!!!!!!!!!!!

add items of type vector to the list instead of int items.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#include<vector>

using namespace std;

template<typename T>

class list{

   private:

       T*arr;

       int size;

       int capacity;

     

       void resize(){

           capacity*=2;

           T * newArr = new T[capacity];

           for(int i=0;i<size;i++){

               newArr[i] = arr[i];

           }

           delete[] arr; //use this syntax to deallocate array

           arr = newArr;

         

       }

     

   public:

       list(){

           size=0;

           capacity = 1;

           arr = new T[capacity];

       }

     

       bool isFull(){

           return size==capacity;

       }

       bool isEmpty(){

           return size==0;

       }

     

       void insert(T data){

           if(isFull())resize();

           arr[size++] = data;

       }

       int getSize(){

           return size;

       }

   void printList(){

       for(int i=0;i<size;i++){

           cout<<arr[i]<<" ";

           }

           cout<<"\n";

       }

};

//an overloaded << operator to print the contents of an int vector when printed using

//<< operator. the printList method in list class simply print each item on the list

//and it doesn't bother whether or not there is << method defined for them. so if we

//simply use it to print a list of vectors, we will run into errors saying no match for

//operator<< (for vector type). which is why we are defining this method

ostream& operator<<(ostream &out, const vector<int> &v){

                //printing the contents of vector v inside a pair of curly braces, separated by

                //comma and space

                out<<"{";

                for(int i=0;i<v.size();i++){

                                out<<v[i];

                                //printing a comma and space if this is not the last element

                                if(i!=v.size()-1){

                                               out<<", ";

                                }

                }

                out<<"}";

                return out;

}

int main(){

                //creating a list of vectors of int

   list<vector<int> > l1;

   //looping from i=1 to 5

   for(int i=1;i<=5;i++){

                //creating an int vector

       vector<int> v;

       //adding numbers from 1 to i to the vector

       for(int j=1;j<=i;j++){

                                v.push_back(j);

                   }

                   //inserting vector to the list

                   l1.insert(v);

                   //printing the list

       l1.printList();

   }

   //after the loop, list will contain 5 vectors

   //{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4} and {1, 2, 3, 4, 5}

   return 0;

}

/*OUTPUT*/

{1}

{1} {1, 2}

{1} {1, 2} {1, 2, 3}

{1} {1, 2} {1, 2, 3} {1, 2, 3, 4}

{1} {1, 2} {1, 2, 3} {1, 2, 3, 4} {1, 2, 3, 4, 5}

Add a comment
Know the answer?
Add Answer to:
//C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;   ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void...

    use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void setSize() { cout << "Enter an integer larger than 2: "; cin >> size; } int getSize() { return size; } void fibTerms(int x) // Write a recursive function { } explicit FibObj() // Default constructor was made explicit to avoid possible implicit type conversion { setSize(); int *arr; arr = new int[getSize()]; arr[0] = 1; arr[1] = 1; // Call recursive function here...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

    Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT