创建双向训练链表

下面创建的是双向训练链表,注意 是循环的。

#include 
using namespace std;
struct BiD_LNode
{
	int data;
	BiD_LNode *next;
	BiD_LNode *prior;
};
//使用头插法创新双向循环链表
void Create_LinkList(BiD_LNode *&head)
{
	head = new BiD_LNode();
	head->prior = head->next = head;
	BiD_LNode *p = head;
	int data;
	while (cin >> data)
	{
		BiD_LNode *q = new BiD_LNode();
		q->data = data;
		p->next = q;
		q->prior = p;
		q->next = head;
		head->prior = q;
		p = q;
	}
}
//打印双向循环链表
void Print_LinkList(BiD_LNode *&head)
{
	//正序打印
	BiD_LNode *p = head->next;
	while (p != head)
	{
		cout << p->data << " ";
		p = p->next;
	}
	//逆序打印
	/*BiD_LNode *p = head->prior;
	while (p != head)
	{
		cout << p->data << " ";
		p = p->prior;
	}*/
}
int main()
{
	BiD_LNode *head = NULL;
	int count = 0;
	Create_LinkList(head);
	Print_LinkList(head);
	system("pause");
	return 0;
}


你可能感兴趣的:(C++基础)