Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
5
Добавлен:
28.06.2014
Размер:
3.59 Кб
Скачать
// ---------------------------------------------------------------------------

#pragma hdrstop

#include "class.h"

// ---------------------------------------------------------------------------
#pragma package(smart_init)

template<class TYPE>BiList<TYPE>::BiListItem::BiListItem() {
	next = pred = NULL;
}

template<class TYPE> BiList<TYPE>::iterator& BiList<TYPE>::iterator:: operator++(int) {
	if ((*this).data != NULL)
		(*this).data = (*(*this).data).next;
	return *this;
}

template<class TYPE> BiList<TYPE>::iterator& BiList<TYPE>::iterator:: operator--(int) {
	if ((*this).data != NULL)
		(*this).data = (*(*this).data).prev;
	return *this;
}

template<class TYPE> BiList<TYPE>::iterator& BiList<TYPE>::iterator::remove() {
	if ((*this).data != NULL) {
		if ((*(*this).data).next == NULL)
			(*(*(*this).data).pred).next = NULL;
		else {
			if ((*(*(*this).data).pred).next == NULL)
				(*(*(*this).data).next).pred = (*(*this).data).pred;
			else {
				(*(*(*this).data).next).pred = (*(*this).data).pred;
				(*(*(*this).data).pred).next = (*(*this).data).next;
			}
		}
		if ((*this).data == *(*this).parent)
			*(*this).parent = (*(*this).data).next;
		BiListItem *tmp = (*this).data;
		(*this).data = (*(*this).data).next;
		if ((*this).data == NULL)
			(*this).data = *(*this).parent;
		delete(tmp);
	}
	return *this;
}

template<class TYPE> BiList<TYPE>::iterator& BiList<TYPE>::iterator::insert(TYPE v) {
	if ((*this).data != NULL) {
		BiList<TYPE>::BiListItem *Item = new BiList<TYPE>::BiListItem;
		(*Item).data = v;
		if ((*(*this).data).next == NULL) {
			(*Item).next = NULL;
			(*(*this).data).next = (Item);
			(*Item).pred = (*this).data;
			(**(*this).parent).pred = (Item);
		}
		else {
			(*Item).pred = (*this).data;
			(*Item).next = (*(*this).data).next;
			(*(*this).data).next=Item;
			(*(*Item).next).pred = Item;
		}
	}
	return *this;
}

template<class TYPE> bool BiList<TYPE>::push_back(TYPE v) {
	if (v != NULL) {
			BiList<TYPE>::BiListItem *Item = new BiList<TYPE>::BiListItem;
			(*Item).data = v;
			if (pbegin == NULL) {
				(*pbegin).pred = pbegin = Item;
				(*pbegin).next = NULL;
			}
			else {
				BiList<TYPE>::BiListItem *ItemEnd = (*pbegin).pred;
				(*Item).next = NULL;
				(*Item).pred = ItemEnd;
				(*ItemEnd).next = Item;
				(*pbegin).pred = Item;
			}
			return true;
	}	else
		return false;
}

template<class TYPE>bool BiList<TYPE>::push_front(TYPE v) {
	if (v != NULL) {
			BiList<TYPE>::BiListItem *Item = new BiList<TYPE>::BiListItem;
			(*Item).data = v;
			if (pbegin == NULL) {
				(*pbegin).pred = pbegin = Item;
				(*pbegin).next = NULL;
			}
			else {
				(*Item).next = pbegin;
				(*Item).pred = (*pbegin).pred;
				(*pbegin).pred = Item;
				pbegin = Item;
			}
			return true;
	}
	else
		return false;
}

template<class TYPE>BiList<TYPE>::iterator& BiList<TYPE>::begin() {
	BiList<TYPE>::iterator *it;
	it = new BiList<TYPE>::iterator;
	(*it).data = pbegin;
	(*it).parent = &pbegin;
	return *it;
}

template<class TYPE>BiList<TYPE>::iterator& BiList<TYPE>::end() {
	BiList<TYPE>::iterator *it;
	it = new BiList<TYPE>::iterator;
	(*it).data = NULL;
	(*it).parent = &pbegin;
	return *it;
}

template<class TYPE>void BiList<TYPE>::clear() {
	BiList<TYPE>::iterator it = begin();
	while (it != end()) {
		it.remove();
		it = begin();
	}
}

template<class TYPE>int BiList<TYPE>::size() {
	int i = 0;
	for (BiList<TYPE>::iterator it = begin(); it != end(); it++, i++);
	return i;
}
Соседние файлы в папке laba13