クラステンプレート---双方向チェーンテーブル

2118 ワード

#ifndef DOUBLY_LINKED_LIST
#define DOUBLY_LINKED_LIST

template
class DLLNode{
public:
	DLLNode(){
		next = prev = 0;
	}
	DLLNode(const T& el, DLLNode *n = 0, DLLNode *p = 0){
		info = el; 
		next = n; 
		prev =p;
	}
	T info;
	DLLNode *next, *prev;
};

template
class DoublyLinkedList{
public:
	DoublyLinkedList(){
		head = tail = 0;
	}
	void addToDLLTail(const T&);
	void addToDLLHead(const T&);
	T deleteFromDLLTail();
	T print();
protected:
	DLLNode *head, *tail;
};

template
void DoublyLinkedList::addToDLLTail(const T& el){
	if (tail != 0)
	{
		tail = new DLLNode(el,0,tail);
		tail -> prev ->next = tail;
	}
	else head = tail = new DLLNode(el);
}
template
void DoublyLinkedList::addToDLLHead(const T& el){
	if (head != 0)
	{
		head = new DLLNode(el,head,0);
		head -> next->prev = head;
	}
	else head = tail = new DLLNode(el);
}
template
T DoublyLinkedList::deleteFromDLLTail(){
	T el = tail ->info;
	if (head == tail)
	{
		delete head;
		head = tail = 0;
	}
	else{
		tail = tail ->prev;
		delete tail->next;
		tail->next=0;
	}
	return 0;
}

template
T DoublyLinkedList::print(){

	DLLNode *tmp = head;
	while(0!=tmp){
		cout<info<next;
	}
	cout<
#include 
#include "genDLList.h"

using namespace std;


int main()
{
	DoublyLinkedList a;//     
	a.addToDLLTail(1);
	DoublyLinkedList b;
	b.addToDLLHead(1);
	DoublyLinkedList *list = new DoublyLinkedList;
	list->addToDLLTail(4);
	list->addToDLLTail(2);
	list->addToDLLHead(3);
	list->addToDLLHead(3);
	list->deleteFromDLLTail();
	list->print();


	//IntSLList *list = new IntSLList();
	//list->addToHead(1);
	//list->addToHead(2);
	//list->addToHead(3);
	//list->addToTail(5);
	//list->print();

	system("pause");

}