0% found this document useful (0 votes)
8 views

Class Chain

C++ Data Structure

Uploaded by

nandinimani2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Class Chain

C++ Data Structure

Uploaded by

nandinimani2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Class Chain

template<class T>
class chain : public linearList<T>
{
public:
chain(int initialCapacity = 10)
chain(const chain<T>& );
~ chain();
bool empty() const
{
return listSize == 0;
}
int size() const
{
return listSize;
}
T& get (int theIndex) const;
int indexOf (const &T theElement) const;
void erase(int theIndex);
void insert(int theIndex, const &T theElement);
void output (ostreamt out) const;
protected:
void checkIndex(int theIndex) const;
chainNode<T>* firstNode;;
int listSize;
};
template<class T>
chain<T> :: chain(int initialCapacity )
{
if (initialCapacity <1)
{
ostringstream s;

2
s << "Initial capacity = ”<<initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
firstNode=NULL;
listSize=0;
}
template<class T>
chain<T> :: chain(const chain<T>& theList)
{
listSize=theList.listSize;
if (listSize = 0)
{
firstNode =NULL;
return;
}
chainNode<T> * SourceNode= theList.firstNode;
firstNode =new chainNode<T>(sourceNode-›element);

3
sourceNode = sourceNode->next;
chainNode<T> * targetNode= firstNode;
while(sourceNode !=NULL)
{
targetNode->next= new chainNode<T>(sourceNode-›element);
targetNode= targetNode->next;
sourceNode = sourceNode->next;
}
targetNode->next=NULL;
}
template<class T>::~ chain()
{
while(firstNode !=NULL)
{
chainNode<T>* nextNode =firstNode-›next;
delete firstNode;
firstNode = nextNode;

4
}
}
template<class T›
int chain<T>:: index0f(const &T theElement) const
{
chainNode<T>* currentNode = firstNode;
int index = 0;
while (currentNode != NULL && currentNode-›element !=theElement)
{
currentNode = currentNode->next;
index++;
}
if (currentNode == NULL)
return -1;
else
return index;
}

5
template<class T>
void chain<T>: :erase(int theIndex)
{
checkIndex (theIndex);
chainNode<T>* deleteNode;
if (theIndex = 0)
{
deleteNode = firstNode;
firstNode = firstNode->next;
}
else
{
chainNode<T›* p =firstNode;
for (int i=0; i<theIndex - 1; i++)
p = p->next;
deleteNode = p-›next;
p-›next = p-›next-›next;

6
listSize--;
delete deleteNode;
}
template<class T>
void chain<T>: :insert(int theIndex)
{
if (theindex <0 || theIndex > listSize)
{
ostringstrean s;
s<<"index = " << theIndex <<" size = " << listSize;
throw iilegalIndex(s.str());
}
if (theIndex = 0)
firatNode =new chainNode<T>(theElement, firstNode);
else
{
chainNode<T›* p - firstNode;

7
for (int i=0; i<theindex - 1; i++)
p-p->next;
p-›next - new chainNode<T> (theElement, p-›next);
}
listSize++;
}
template<class T›
void chain<T>: :output(ostream& out) const
{
for (chainNode<T›* currentNode = firstNode;
currentNode =! NULL;
currentNode = currentNode-›next)
out << currentNode-›element<<" ";
}
template ‹class T›
ostreamd operators<<(ostreamk out, const chain<T>& x)
{
x.output (out);

8
return out;
}
template<class >T
class extendedLinearList : linearList< T>
{
public:
virtual ~extendedLinearList()
{
}
virtual void clear () =0;
virtual void push_back(const Tk theElement) = 0;
}
template<class T>
void extendedChain<T>:: clear ()
{
while (firstNode =( NULL)
{

9
chainNode<T>* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
listSize = 0;
}

template<class T>
void extendedChain<T>: :push_back(const &T theElement)
{
chainNode<T›* newNode=new chainNode<T>(theElement, NULL);
if (firstNode = NULL)
firstNode = lastNode = newNode;
else
{
lastNode-›next = newNode;
lastNode = newNode

10
}
listSize++;
}

11

You might also like