双向链表

#include <iostream>

using namespace std;

class DblList;  // 前置声明,

class DblListNode
{
	friend class DblList;
public:
	int data;
	DblListNode *llink, *rlink;
};

class DblList
{
public:
	DblList()
	{
		first = new DblListNode();
		first->llink = first->rlink = first;  // 左直线的指针和右直线指针都指向第一个,
	}
	void Insert(DblListNode*, DblListNode*);
	void Delete(DblListNode*);
//private:
public:
	DblListNode *first;  // 指向表头结构的指针,
};

void DblList::Insert(DblListNode *p, DblListNode *x)
{
	p->llink = x;
	p->rlink = x->rlink;
	x->rlink->llink = p;
	x->rlink = p;
}

void DblList::Delete(DblListNode *x)
{
	if(x == first)
		cout << "Deletion of head node not permitted." << endl;
	else
	{
		x->llink->rlink = x->rlink;
		x->rlink->llink = x->llink;
		delete x;
	}
}

int main()
{
	DblList intList;
	DblListNode *node1,*node2,*node3,*node4;
	node1 = new DblListNode();
	node2 = new DblListNode();
	node3 = new DblListNode();
	node4 = new DblListNode();
	node1->data = 10;
	node2->data = 20;
	node3->data = 30;
	node4->data = 40;
	intList.Insert(node1,intList.first);
	intList.Insert(node2,intList.first);
	intList.Insert(node3,intList.first);
	intList.Insert(node4,intList.first);
	intList.Delete(node3);

	cout << intList.first->rlink->data << endl;
	cout << intList.first->rlink->rlink->data << endl;
	cout << intList.first->rlink->rlink->rlink->data << endl;
	cout << intList.first->rlink->rlink->rlink->rlink->data << endl;// 双向列表既可以向右也可以向左,
	cout << intList.first->rlink->rlink->rlink->rlink->llink->data << endl;

	return 0;
}

你可能感兴趣的:(双向链表)