Problem: Create a template class that has one call it myList. The class has one
ID: 3706144 • Letter: P
Question
Problem: Create a template class that has one call it myList. The class has one datatype T1. It has four variables: size and CAP of type int, pointer *p and dynamic array *A of type T1. Create the following functions members: 1- (1 pts) A default constructor to set the size to 0, and create CAP to 10, initialize the pointer to 0 the array A (use new) 2- (1 pt) A function myFill) to fill in the array with CAP elements of type T1 3- (1 pt) Print function to print the array. 4- (2 pts) myDelete (T1 k) to delete value k from the array. 5- (1.5 pts) Function myBegin () to returns a pointer to the beginning of the list (i.e. beginning of array). 6- (1.5 pts) Function myEnd ) to return a pointer to the end of the list (i.e. array) In main: 1- Create an int default object of myList. 2- Fill it with random values. 3- Delete one element that exists in your list 4- Print the list again. 5- Print the beginning and the end of the list. 6- Repeat all the above steps with a list of characters instead of int.Explanation / Answer
#include<stdio.h>
#include<iomanip.h>
#include<conio.h>
template <class T>
class myList
{
private:
int size,cap;
T *a,*p;
public:
myList();
void myFill();
void print();
void myDelete(T);
void myBegin();
void myEnd();
};
template <class T>
myList<T> :: myList()
{
size=0;
cap=10;
*p=0;
a=new T[cap];
}
template <class T>
void myList<T>::myFill()
{
int i;
cout<<"enter the size of array";
cin>>cap;
a = new T[cap];
cout<<endl<<"enter"<<cap<<"elements";
for(i=0;i<cap;i++)
cin>>*(a+i);
}
template <class T>
void myList <T>::print()
{
int i;
cout<<endl<<"ARRAY ELEMENTS ARE";
for(i=0;i<cap;i++)
cout<<" "<<*(a+i);
}
template <class T>
void myList <T>:: myDelete(T k)
{
int i,j=0,count=0;
p= new T[cap];
for(i=0;i<cap;i++)
{
if(k!=*(a+i))
{
*(p+j)=*(a+i);
j++;
}
else
count++;
}
cap=cap-count;
for(i=0;i<cap;i++)
*(a+i) = *(p+i);
delete(p);
}
template <class T>
void myList <T>::myBegin()
{
int i;
cout<<endl<<"THE FIRST ELEMENT OF ARRAY"<<*(a+0);
}
template <class T>
void myList <T>::myEnd()
{
int i;
cout<<endl<<"THE LAST ELEMENT OF ARRAY"<<*(a+(cap-1));
}
void main()
{
int no;
char x;
myList<int> m;
m.myFill();
m.print();
cout<<endl<<"Enter an integer to delete";
cin>>no;
m.myDelete(no);
m.print();
m.myBegin();
m.myEnd();
myList<char> n;
n.myFill();
n.print();
cout<<endl<<"Enter a character to delete";
cin>>x;
n.myDelete(x);
n.print();
n.myBegin();
n.myEnd();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.