zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[数据结构]双向循环链表及其基本操作

循环链表数据结构 及其 基本操作 双向
2023-09-11 14:18:49 时间
#include <iostream>
#include <cstdio>

using namespace std;

typedef int Elem;
typedef int Status;

typedef struct DulList
{
	/* data */
	Elem data;
	struct DulList *prior;
	struct DulList *next;
} DulList, *DulLinkList;

Status InitDulList(DulLinkList &L)
{
	L = new DulList;
	L->next = NULL;
	L->prior = NULL;

	return 1;
}

Status CreatDulList_H(DulLinkList &L, int num)
{
	L = new DulList;
	L->next = NULL;

	for (int i = 1; i <= num; i++)
	{
		DulList *p = new DulList;
		cin >> p->data;

		p->next = L->next;
		if (L->next)
			L->next->prior = p;
		L->next = p;
		p->prior = L;
	}

	return 1;
}

Status ShowDulList(DulLinkList &L)
{

	DulList *q = L->next;
	while (q->next)
		q = q->next;
	q->next = L->next;//怎么双向没写 我试了一下 把尾巴指向头 就循环了...

	DulList *p = L->next;

	while (p)
	{
		cout << p->data << " ";
		p = p->next;
	}

	return 1;
}

int main()
{
	DulLinkList L;

	//1
	InitDulList(L);

	//1
	CreatDulList_H(L, 3);

	//1
	ShowDulList(L);
}