You are currently browsing the tag archive for the ‘Indirect addressing’ tag.
In this,an array of pointers is created and the memory locations of the elements of linear list are stored in specified order.
This is program on linear list with Indirect Addressing :
/*
This program is just an example to demonstrate
Linear list with Indirect Addressing.By modifying
the main() function the working of several functions
in the indirectlist class can be examined.
*/
#include<iostream>
using namespace std;
template<class T>
class indirectlist
{
public:
indirectlist(int =10);
bool isempty()
{
if(length==0)
return true;
return false;
}
int len(){ return length; }
bool find(int k ,T &x);
int search(T &x);
indirectlist<T>& del(int k,T &x);
indirectlist<T>& insert(int k,T &x);
void display();
private:
T **table;
int length,size;
};
template<class T>
indirectlist<T>::indirectlist(int n)
{
size=n;
table=new T*[size];
length=0;
}
template<class T>
bool indirectlist<T>::find(int k,T &x)
{
if(k<1||k>length)
return false;
x=*table[k-1];
return true;
}
template<class T>
int indirectlist<T>::search(T &x)
{
for(int i=0;i<length;i++)
{
if(*table[i]==x)
return i+1;
}
return -1;
}
template<class T>
indirectlist<T>& indirectlist<T>::del(int k,T &x)
{
if(find(k,x))
{
for(int i=k;i<length;i++)
table[i-1]=table[i];
length--;
}
else
cout<<"Element at "<<k<<" cannot be deleted\n";
return *this;
}
template<class T>
indirectlist<T>& indirectlist<T>::insert(int k,T &x)
{
if(k>0||k<length+1)
{
for(int i=length;i>k;i--)
table[i]=table[i+1];
table[k-1]=&x;
length++;
}
else
cout<<"Insertion of "<<k<<" element not possible\n";
return *this;
}
template<class T>
void indirectlist<T>::display()
{
cout<<"\nElements in list : ";
for(int i=0;i<length;i++)
cout<<*table[i]<<"\t";
cout<<endl;
return;
}
int main()
{
int n;
int a=2,b=5,c=23;
indirectlist<int> i1(3);
i1.insert(1,a).insert(2,b).insert(3,c);
i1.display();
i1.del(2,b).display();
}

