(2011.07.20) 双链表.cpp -- 最基本的C++双链表

// 双链表.cpp -- 最基本的C++双链表
// Doubly-linked list link node with freelist support
template <class Elem>
class Link
{
private:
	static Link<Elem>* freelist;		// Head of the freelist
public:
	Elem element;						// Value for this node
	Link *next;							// Pointer to next node in list
	Link *prev;							// Pointer to previous node
	// 构造一个带元素和前后指针的构造函数
	Link(const Elem& e, Link* prevp = NULL, Link* nextp = NULL)
		{ element = e; prev = prevp; next = nexp; }
	// 构造一个只带前指针和后指针的函数
	Link(Link*prevp = NULL, Link* nextp = NULL)
		{ prev = prevp; next = nextp; }

	// Overload new and delete operators for freelist
	void* operator new(size_t);
	void operator delete(void*);
};

template <class Elem>
Link<Elem>* Link<Elem>::freelist = NULL;

template <class Elem>					// overload for new operator
void* Link<Elem>::operator new(size_t)
{
	if(freelist == NULL) return ::new Link;	// creat space
	Link<Elem>*temp = freelist;				// can take from freelist
	freelist = freelist -> next;
	return temp;							// Return the link
}

template <class Elem>					// overload for delete operator
void Link<Elem>::operator delete(void* ptr)
{
	((Link<Elem>*)ptr) -> next = freelist;		// put on freelist
	freelist = (Link<Elem>*)ptr;
}


template <class Elem>
class LList: public Link<Elem>
{
private:
	int rightcnt;			// size of right partition
	int leftcnt;			// size of left partition
	Link<Elem> *head;		// Pointer to list header
	Link<Elem> *tail;		// Pointer to last elem in list
	Link<Elem> *fence;		// Last element on left side

	void init()				// Intialization routine
	{
		fence = tail = head = new Link<Elem>;
		leftcnt = rightcnt = 0;
	}

	void removeall()		// Return link nodes to free store
	{
		while(head != NULL)
		{
			fence = head;
			head = head -> next;
			delete fence;
		}
	}
};

public:
	LList(int size = DefaultListSize) { init();}
	~LList() { removeall(); init(); }
	bool insert(const Elem& item);
	bool append(const Elem& item);
	bool remove(Elem& it);
	void prev();
//  ....
};

template <class Elem> // Insert at front of right partition
bool LList<Elem>::insert(const Elem& item)
{
	fence -> next = new Link<Elem>(item, fence, fence -> next);
	if (fence -> next -> next != NULL)	// if note deleting at end
		fence -> next -> next -> prev = fence -> next;
	if (tail == fence)				// Appending new Elem
		tail = fence -> next;		/// so set tail
	rightcnt++;
	return true;
}

template <class Elem>	// Append Elem to end of the list.
bool LList<Elem>::append(const Elem& item)
{
	tail = tail -> next = new Link <Elem>(item, tail, NULL);
	rightcnt++;
	return true;
}

// Removve and return first Elem in right partition
template <class Elem> bool LList<Elem>::remove(Elem& it)
{
	if (fence -> next == NULL) return false;	// empty right
	it = fence -> next -> element;				// Remember value
	Link<Elem>* ltemp = fence -> next;			// Remember link node
	if (ltemp -> next != NULL) ltemp -> next -> prev = fence;
	else tail = fence;							// Reset tail
	fence -> next = ltemp -> next;				// Remove from list
	delete ltemp;								// Reclaim space
	rightcnt--;									// Removed from right
	return true;
}

// Move fence one step left; no change if left is empty
template <class Elem> void LList<Elem>::prev()
{
	if (fence != head)	// can't back up from list head
	{
		fence = fence -> prev; leftcnt--; rightcnt++;
	}
}


你可能感兴趣的:(C++,list,null,delete,Class,insert)