데이터 구조: 선두 노드 양 방향 순환 링크 의 실현 (연구)

2413 단어 데이터 구조
#include 
using namespace std;



class DoubleLinkedCircleList {
private:
	struct node {
		int val;
		node *next, *prior;
		
		node(int x) {
			this->val = x;
		}
		
		node() {}
	}; 
	
	node *head;
	int size;
public:
	DoubleLinkedCircleList() {
		head = new node;
		head->next = head->prior = head;
		size = 0;
	}
	~DoubleLinkedCircleList() {
		while (head->next != head && head->prior != head) delTail();
		delete head;
	}
	
	void insert(int index, int x) {
		if (index<0 || index>size) {
			cout << "the index is invalid!" << endl;
			return;
		} 
		
		node *p = head;
		for (int i=0; inext;
		node *q = new node(x);
		q->next = p->next;
		if (p->next) p->next->prior = q;
		q->prior = p;
		p->next = q;
		size ++;
	}
	void insertTail(int x) {insert(size, x);}
	void insertHead(int x) {insert(0, x);}
	
	int del(int index) {
		if (index<0 || index>=size) {
			cout << "the index is invalid!" << endl;
			return -1;
		}
	
		if (head->next == head && head->prior==head) {
			cout << "the DoubleLinkedCircleList is null!" << endl;
			return -1;
		} 
		node *p = head;
		for (int i=0; inext;
		node *del = p->next;
		int res = del->val;
		del->next->prior = p;
		p->next = del->next;
		delete del;
		size --;
	}
	
	
	int delHead() {del(0);}
	int delTail() {del(size-1);}
	
	
	int get(int index) {
		if (index<0 || index>=size) {
			cout << "the index is invalid!" << endl;
			return -1;
		}
		
		if (head->next == head && head->prior==head) {
			cout << "the DoubleLinkedCircleList is null!" << endl;
			return -1;
		} 
		
		node *p = head->next;
		for (int i=0; inext;
		return p->val;
	}
	int getHead() {return get(0);}
	int getTail() {return get(size-1);}
	
	void show() {
		node *p = head->next;
		while (p != head) {
			cout << p->val << " ";
			p = p->next;
		}
		cout << endl;
	}
	bool isEmpty() {}
};


int main() {
	int a[] = {1,2,3,4,5,6};
	int n = sizeof(a)/sizeof(int);
	DoubleLinkedCircleList l = DoubleLinkedCircleList();
	for (int i=0; i

좋은 웹페이지 즐겨찾기