반응형
//********************************************************************
//	list class
//********************************************************************
#include 
#include  
using namespace std;
// 이중연결리스트
template 
class Node
{
public:
	T data;
	Node * next;
	Node * prev;
};
template 
class List
{
private:
	Node * head;
	Node * tail;
public:
	List()
	{
		head = new Node;
		tail = new Node;
		head->next = tail;
		head->prev = NULL;
		tail->prev = head;
		tail->next = NULL;
	}
	Node * Begin() { return head->next; }	// 맨 앞 노드를 리턴
	Node * End() { return tail; }		// 맨 뒤 노드를 리턴
	// 삽입
	void insert(Node * Where, T data)
	{
		Node * NewNode = new Node;
		NewNode->data = data;
		NewNode->next = Where->next;
		NewNode->prev = Where;
		Where->next->prev = NewNode;	// 순서주의
		Where->next = NewNode;
	}
	void push_back(T data)
	{
		insert(End()->prev, data);
	}
	void push_front(T data)
	{
		insert(head, data);
	}
	// 삭제
	void erase(Node * Where)
	{
		Where->prev->next = Where->next;
		Where->next->prev = Where->prev;
		delete Where;l
	}
	void pop_back()
	{
		erase(End()->prev);
	}
	void pop_front()
	{
		erase(Begin());
	}
	// 출력
	void show()
	{
		Node *p = Begin();
		while(p != End()->next)
		{
			cout << p->data << endl;
			p = p->next;
		}
	}
	class iterator
	{
	public:
		Node * cur;
	public:
		iterator( Node * p = 0 ) : cur(p){}
		// 역참조 오버로딩
		T & operator *()
		{
			return cur->data;	// 현재의 값을 리턴
		}
		// 다음 칸으로 이동
		iterator operator++()
		{
			if(cur != NULL)	// tail이 아닐때만
				cur = cur->next;
			return *this;
		}
		// 이전 칸으로 이동
		iterator operator--()
		{
			if(cur != NULL)	// head가 아닐때만
				cur = cur->prev;
			return *this;
		}
		bool operator != ( const Node * p )
		{
			if(cur != p)
				return true;
			else
				return false;
		}
	};
};
template  
class Queue
{
private:
	List list; 
public : 
	void push( T data)
	{
		list.push_back(data);
	}
	void pop()
	{
		list.pop_front();
	}
	T & front(){ return list.Begin()->data; }
	T & back(){ return  (list.End()-1)->data; }
};
void main()
{
	queue q;
	q.push(10);
	q.pop(); 
	List l;
	//List::iterator p = l.Begin();// 노드를 가리키는 객체 p 생성
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	// 노드를 가리킬 수 있는 포인터 클래스
	List::iterator p;
	for( p = l.Begin(); p != l.End(); p++ )
		cout << *p << endl;
}

'자료구조' 카테고리의 다른 글

[자료구조] 이중 연결 리스트  (0) 2014.12.01
[자료구조] 연결 리스트  (0) 2014.12.01
[자료구조] 단일 연결리스트  (0) 2014.12.01
[자료구조] vector  (0) 2014.12.01
[자료구조] stack  (0) 2014.12.01

+ Recent posts