Скачиваний:
13
Добавлен:
01.05.2014
Размер:
2.54 Кб
Скачать
// ” ©«: clist.cpp
// ђҐ «Ё§ жЁп Є« бб  cList
// Ђўв®а: ђг­®ў Ђ.Ђ. Ја. 2355
// „ в : 20.02.2005

#include <stdio.h>
#include "clist.h"

// Є®­бвагЄв®а:
	template <class tInfo> cList<tInfo>::cList()
	{
		first = NULL;
	}

// ¤ҐбвагЄв®а:
	template <class tInfo> cList<tInfo>::~cList()
	/*
		“¤ «Ґ­ЁҐ ўбҐе н«Ґ¬Ґ­в®ў бЇЁбЄ 
	*/
	{
		if (!empty())
		{
			int count = length();
			for (int i = count; i>=1; i--)
				delete getElementAddr(i);
		}
	}


// ᥫҐЄв®ал:
	template <class tInfo> tInfo cList<tInfo>::getElement(int nr)
	/*
		‚®§ўа й Ґв Ё­д®а¬ жЁ®­­го з бвм н«Ґ¬Ґ­в  бЇЁбЄ 
		б Ї®ап¤Є®ўл¬ ­®¬Ґа®¬ nr=1..N
	*/
	{
		cElement<tInfo> *c = getElementAddr(nr);
		if (c != NULL)
			return getElementAddr(nr)->getInfo();
		else
			return 0;
	}

	template <class tInfo> cElement<tInfo> *cList<tInfo>::getElementAddr(int nr)
	/*
		‡ Єалвл© бҐ«ҐЄв®а.
		‚®§ўа й Ґв гЄ § вҐ«м ­  н«Ґ¬Ґ­в бЇЁбЄ 
		б Ї®ап¤Є®ўл¬ ­®¬Ґа®¬ nr=1..N
	*/
	{
		if ((!empty()) && (nr > 0) && (nr <= length()))
		{
			if (nr==1) return first;
			cElement<tInfo>* i = first;
			i = i->getNext();
			long count=2;
			while ((i != first) && (count != nr))
			{
				i = i->getNext();
				count++;
			}
			return i;
		}
		else
		return NULL;
	}


	template <class tInfo> int cList<tInfo>::length()
	/*
		‚®§ўа й Ґв Є®«ЁзҐбвў® н«Ґ¬Ґ­в®ў бЇЁбЄ .
	*/
	{
		if (!empty())
		{
			cElement<tInfo>* i = first;
			i = i->getNext();
			long count=1;
			while (i != first)
			{
				i = i->getNext();
				count++;
			}
			return count;
		}
		else
		return 0;
	}


	template <class tInfo> int cList<tInfo>::empty()
	/*
		‚®§ўа й Ґв 0 Ґб«Ё бЇЁб®Є Їгбв,
				   зЁб«® < 0 Ґб«Ё ­ҐЇгбв.
	*/
	{
		return (first == NULL);
	}



// ¬®¤ЁдЁЄ в®ал:
	template <class tInfo> void cList<tInfo>::addElement(tInfo el)
	/*
		„®Ў ў«пҐв н«Ґ¬Ґ­в ў Є®­Ґж бЇЁбЄ .
	*/
	{
		if (empty())
		{
			first = new cElement<tInfo>(el);
			first->setNext(first);
			first->setPrev(first);
		}
		else
		{
			cElement<tInfo>* last = first->getPrev();
			cElement<tInfo>* newel = new cElement<tInfo>(el);
			last->setNext(newel);
			newel->setNext(first);
			newel->setPrev(last);
			first->setPrev(newel);
		}
	}


	template <class tInfo> void cList<tInfo>::setElement(int nr, tInfo elnew)
	/*
		“бв ­ ў«Ёў Ґв а ў­л¬ elnew Ё­д®а¬ жЁ®­­го з бвм н«Ґ¬Ґ­в  бЇЁбЄ 
		б Ї®ап¤Є®ўл¬ ­®¬Ґа®¬ nr=1..N
	*/
	{
		cElement<tInfo> *c = getElementAddr(nr);
		if (c != NULL)
			getElementAddr(nr)->setInfo(elnew);
	}
Соседние файлы в папке all