To write C++ code to implement a List ADT using array with operations find(), makeEmpty(), printList(), findKth(), insert(), delete() etc.


Even though an array is usually viewed as a consecutive set of memory locations, this concept may bring about some implementation issues. For the purpose of defining a List ADT using array, we consider an array as a set of pairs, <index, value>, such that each index that is defined has a value associated with it. With this idea in mind, we can define the basic operations on an array ADT like find(), findKth(), insert(), delete() etc. as follows.



Algorithm:
template<class T>
class List
{
private:
int size;
T *list;
public:
List ( int size, T initvalue );
T Retrieve( int i);
void insert( int i, T item );
int find( T item );
void makeEmpty();
void printList();
T findKth( int k );
}
// Constructor
template < class T >
List<T>:: List( int size, T initval )
{
this.size = size;
list= new T[size];
for (i=0 ; i<=size ; i++)
list [i]= initval;
}
// retrieve function
template<class T>
T List<T>:: Retrieve(int i)
{
return list[i];
}
// Insert Function
void List<T>:: insert (int i, T x)
{
list[i]=x;
}
//Find Function
int List<T>:: find(T item)
{
int found=0;
for(int index =0; index<size; index++)
if(list[index]==item)
found=1;
break;
}
return found;
}


//makeEmpty() function
void List<T> :: makeEmpty()
{
for(int index=0; index<size; index++)
list[index]=NULL;
}
//printList() function
void List<T>::PrintList()
{
for( int index =0;index<size; index++ )
print(list[index]);
}
// findKth()function
T List<T>::findKth (int k)
{
return(list[k])
}




Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments:

Thanks
Mukesh Rajput