You are currently browsing the tag archive for the ‘Simulated pointers’ tag.

The main idea of simulated pointers is to replace the c++ pointers( which are used to refer the next and previous elements in list ) by  integers.

This is program on linear list using Simulated pointers :

#include<iostream>
#include<stdlib.h>
using namespace std;

template<class T>
class simnode;

template<class T>
class node
{
	friend class simnode<T>;
	private:
		T data;
		int link;
};

template<class T>
class simnode
{
	public:
		simnode(int =10);
		~simnode();
		int allocate();
		void deallocate(int );
		bool insert(int ,T &);
		bool del(T &);
		int search(T &x);
		bool find(int k,T &x);
		void display();
	private:
		node<T> *n;
		int first,free;
};
template<class T>
simnode<T>::simnode(int size)
{
	int i;
	n=new node<T>[size];
	first=-1;
	free=0;
	for(i=0;i<size-1;i++)
		n[i].link=i+1;
	n[i].link=-1;
}
template<class T>
simnode<T>::~simnode()
{
	delete []n;
}
template<class T>
int simnode<T>::allocate()
{
	int temp=free;
	if(temp!=-1)
	{
		free=n[free].link;
	}
	return temp;
}
template<class T>
void simnode<T>::deallocate(int x)
{
	n[x].link=free;
	free=x;
	x=-1;
	return;
}
template<class T>
bool simnode<T>::insert(int k,T &x)
{
	if(k<0)
	{
		return false;
	}
	int temp=first,temp1;
	for(int i=1;i<k&&temp!=-1;i++)
		temp=n[temp].link;
	if(k>0&&temp==-1)
	{
		return false;
	}
	temp1=allocate();
	if(temp1==-1)
	{
		return false;
	}
	n[temp1].data=x;
	if(k!=0)
	{
		n[temp1].link=n[temp].link;
		n[temp].link=temp1;
	}
	else
	{
		n[temp1].link=first;
		first=temp1;
	}
	return true;
}
template<class T>
bool simnode<T>::del(T &x)
{
	int k=search(x);
	if(k<1||first==-1)
	{
		return false;
	}
	int temp=first;
	if(k==1)
		first=n[first].link;
	else
	{
		int temp1=first;
		for(int i=1;i<k-1&&temp1!=-1;i++)
			temp1=n[temp1].link;
		if(temp1==-1||n[temp1].link==-1)
		{
			return false;
		}
		temp=n[temp1].link;
		n[temp1].link=n[temp].link;
	}
	deallocate(temp);
	return true;
}
template<class T>
int simnode<T>::search(T &x)
{
	int temp=first;
	int i;
	for(i=1;temp!=-1&&n[temp].data!=x;i++)
		temp=n[temp].link;
	if(temp==-1)
		return 0;
	return i;
}
template<class T>
bool simnode<T>::find(int k,T &x)
{
	if(k<1)
		return false;
	int temp=first;
	for(int i=1;i<k&&temp!=-1;i++)
		temp=n[temp].link;
	if(temp!=-1)
	{
		x=n[temp].data;
		return true;
	}
	return false;
}
template<class T>
void simnode<T>::display()
{
	int temp=first;
	if(temp==-1)
	{
		cout<<"\n List is empty";
		return;
	}
	while(temp!=-1)
	{
		cout<<n[temp].data<<"\n";
		temp=n[temp].link;
	}
	return;
}
int main()
{
	int k,ch,x,l=0;
	cout<<"\nEnter number of elements : ";
	cin>>k;
	simnode<int> obj(k);
	while(1)
	{
		cout<<"\n1.insert\n2.delete\n3.find\n4.search\n5.display\n6.exit";
		cout<<"\nEnter choice: ";
		cin>>ch;
		switch(ch)
		{
			case 1:
				cout<<"\nEnter an element to insert: ";
				cin>>x;
				if(obj.insert(l,x))
				{
					cout<<"\nElement inserted at "<<l+1<<" location";
					l++;
				}
				else
					cout<<"\nError in inserting the element";
				break;
			case 2:
				cout<<"\nEnter the element to be deleted: ";
				cin>>x;
				if(obj.del(x))
				{
					cout<<"\nElement deleted successfully";
					l--;
				}
				else
					cout<<"\nError in deleting the element";
				break;
			case 3:
				cout<<"\nEnter location: ";
				cin>>k;
				if(obj.find(k,x))
					cout<<"\nElement is "<<x;
				else
					cout<<"\nInvalid location";
				break;
			case 4:
				cout<<"\nEnter element to be searched: ";
				cin>>x;
				if(obj.search(x)!=0)
					cout<<"\nThe element is at "<<obj.search(x)<<" position";
				else
					cout<<"\nElement not found";
				break;
			case 5:
				cout<<"\nThe elements in list are:\n";
				obj.display();
				break;
			case 6:
				exit(0);
			default:
				cout<<"\nEnter right choice";
		}
	}
}

Blog Stats

  • 9,744 hits

My tweets

Follow me on twitter
Follow

Get every new post delivered to your Inbox.