数据结构之双循环链表

本文探讨双循环链表,它是双链表和循环链表的结合,具备两者优点。节点结构包括前驱和后继指针,形成首尾相连的环状结构。了解更多详情,请参阅相关文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

·双循环链表,顾名思义,就是讲双链表和循环链表结合起来而成的,因此兼有两者的优势,并且劣势互补,具体请看前面的文章。

节点结构:


链表结构:


头文件:DCList.h

#ifndef _DCLIST_H
#define _DCLIST_H

#include<iostream>
#include<assert.h>
using namespace std;


typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node *next;
	struct Node *prev;
}Node;

typedef struct List
{
	Node   *first;
	Node   *last;
	size_t size;
}List;

void InitList(List *list);
void push_back(List *list, ElemType x);
void push_front(List *list, ElemType x);
void show_list(List *list);
Node* find(List *list, ElemType key);
bool delete_val(List *list, ElemType key);
bool insert_val(List *list, ElemType x);
bool resver(List *list);
bool pop_back(List *list);
bool pop_front(List *list);
ElemType getvalue(List *list,ElemType key);
bool modify(List *list,ElemType key,ElemType nkey);
bool clear(List *list);
bool destory(List *list);
bool sort(List *list);
size_t length(List *list);
Node *next(List *list,ElemType key);
Node *prio(List *list,ElemType key);

#endif
函数实现:DCList.cpp

#include"DCList.h"

void InitList(List *list)
{
	Node *s = (Node *)malloc(sizeof(Node));
	assert(s != NULL);
	list->first = list->last = s;
	list->first->prev = list->last;
	list->last->next = list->first;
	list->size = 0;
}

void push_back(List *list, ElemType x)
{
	Node *s = (Node *)malloc(sizeof(Node));
	assert(s != NULL);
	s->data = x;

	s->prev = list->last;
	list->last->next = s;
	list->last = s;
	list->last->next = list->first;
	list->first->prev = list->last;
	list->size++;
}
bool pop_back(List *list)
{
	if (list->size==0)
	{
		return false;
	}
	Node *p=list->last;
	p->prev->next=p->next;
	p->next->prev=p->prev;
	list->last=p->prev;
	list->size--;
	free(p);
	return true;
}

void push_front(List *list, ElemType x)
{
	Node *s = (Node *)malloc(sizeof(Node));
	assert(s != NULL);
	s->data = x;

	s->prev = list->first;
	s->next = list->first->next;
	list->first->next = s;
	s->next->prev = s;

	if(list->size == 0)
		list->last = s;

	list->size++;
}
bool pop_front(List *list)
{
	if (list->size==0)
	{
		return false;
	}
	Node *p=list->first->next;
	p->next->prev=p->prev;
	p->prev->next=p->next;
	if (list->size==1)
	{
		list->last=list->first;
	}
	list->size--;
	free(p);
	return true;
}

void show_list(List *list)
{
	Node *p = list->first->next;
	while(p != list->first)
	{
		cout<<p->data<<"-->";
		p = p->next;
	}
	cout<<"Over!"<<endl;
}

Node* find(List *list, ElemType key)
{
	Node *p = list->first->next;
	while(p != list->first && p->data != key)
		p = p->next;

	if(p != list->first)
		return p;
	return NULL;
}

bool delete_val(List *list, ElemType key)
{
	Node *q = find(list,key);
	if(q == NULL)
		return false;

	if(q == list->last)
		list->last = q->prev;
	q->next->prev = q->prev;
	q->prev->next = q->next;
	free(q);
	list->size--;
	return true;
}

bool insert_val(List *list, ElemType x)
{
	Node *p = find(list,x);
	if(p != NULL)
		return false;

	Node *s = (Node *)malloc(sizeof(Node));
	assert(s != NULL);
	s->data = x;

	p = list->first;
	while(p->next != list->first)
	{
		if(x<p->next->data)
			break;
		p = p->next;
	}

	s->next = p->next;
	p->next->prev = s;

	s->prev = p;
	p->next = s;
	
	if(p == list->last)
	{
		list->last = s;
	}

	list->size++;
	return true;
}
ElemType getvalue(List *list,ElemType key)
{
	if (list->size=0)
	{
		return -1;
	}
	Node *p=list->first->next;
	while(p!=list->first&&p->data!=key)
	{
		p=p->next;
	}
	if (p==list->first)
	{
		return -1;
	}
	return p->data;
}

bool modify(List *list,ElemType key,ElemType nkey)
{
	if (list->size==0)
	{
		return false;
	}
	Node *p=list->first->next;
	while (p!=list->first&&p->data!=key)
	{
		p=p->next;
	}
	if (p==list->first)
	{
		return false;
	}
	p->data=nkey;
	return true;

}
bool clear(List *list)
{
	Node *p=list->first->next;
	Node *q;
	while(p!=list->first)
	{
		q=p;
		p=p->next;
		free(q);
	}
	list->first->next=list->first;
	list->first->prev=list->first;
	list->last=list->first;
	list->size=0;
	return true;
}
bool destory(List *list)		
{
	clear(list);
	free(list->first);		//为什么free(list->first?)
	list->first=list->last=NULL;
	return true;
}
bool sort(List *list)
{
	if (list->size==0)
	{
		return false;
	}
	Node *p=list->first->next;
	Node *q=p->next;
	p->next=list->first;
	list->first->prev=p;
	list->last=p;
	while (q!=list->first)
	{
		insert_val(list,q->data);
		q=q->next;
	}
	return true;
}

bool resver(List *list)		//以第一个节点为中心翻转过来
{
	Node *p = list->first->next;
	Node *q = p->next;
	p->next = list->first;
	list->first->prev = p;
	list->last = p;

	while(q!=list->first)
	{
		p = q;
		q = q->next;

		p->next = list->first->next;
		p->next->prev = p;
		p->prev = list->first;
		list->first->next = p;
	}
	return true;
}
size_t length(List *list)
{
	return list->size;
}
Node *next(List *list,ElemType key)
{
	if (list->size==0||list->size==1)
	{
		return NULL;
	}
	Node *p=list->first->next;
	while (p!=list->last&&p->data!=key)
	{
		p=p->next;
	}
	if (p==list->last)
	{
		return NULL;
	}
	return p->next;
}
Node *prio(List *list,ElemType key)
{
	
	if (list->size==0||list->size==1)
	{
		return NULL;
	}
	Node *p=list->first->next->next;
	while (p!=list->first&&p->data!=key)
	{
		p=p->next;
	}
	if (p==list->first)
	{
		return NULL;
	}
	return p->prev;
}

主函数:main.cpp

#include"DCList.h"

void main()
{
	List mylist;
	InitList(&mylist);
	
	int select = 1;
	ElemType item,nitem;
	Node *p = NULL;
	int pos;
	while(select)
	{
		cout<<"**********************************"<<endl;
		cout<<"* [1] push_back   [2] push_front *"<<endl;
		cout<<"* [3] show_seqlist[0] quit_system*"<<endl;
		cout<<"* [4] pop_back    [5] pop_front  *"<<endl;
		cout<<"* [6] insert_pos  [7] insert_val *"<<endl;
		cout<<"* [8] delete_pos  [9] delete_val *"<<endl;
		cout<<"* [10] find       [11]getvalue   *"<<endl;
		cout<<"* [12] modify     [13]clear      *"<<endl;
		cout<<"* [14] destroy    [15]sort       *"<<endl;
		cout<<"* [16] resver     [17]length     *"<<endl;
		cout<<"* [18] next       [19]prio       *"<<endl;
		cout<<"**********************************"<<endl;
		cout<<"请选择:>";
		cin>>select;
		switch(select)
		{
		case 1:
			cout<<"请输入要插入的数据(-1结束):>";
			while(cin>>item,item!=-1)
			{
				push_back(&mylist,item);
			}
			break;
		case 2:
			cout<<"请输入要插入的数据(-1结束):>";
			while(cin>>item,item!=-1)
			{
				push_front(&mylist,item);
			}
			break;
		case 3:
			show_list(&mylist);
			break;
		case 4:
			pop_back(&mylist);
			break;
		case 5:
			pop_front(&mylist);
			break;
		case 6:
			break;
		case 7:
			cout<<"请输入要插入的数据:";
			cin>>item;
			insert_val(&mylist,item);
			break;
		case 8:
			
			break;
		case 9:
			cout<<"请输入要删除的值:>";
			cin>>item;
			delete_val(&mylist,item);
			break;
		case 10:
			cout<<"请输入要查找的值:>";
			cin>>item;
			p = find(&mylist,item);
			break;
		case 11:
			cout<<"请输入要查找的值:>";
			cin>>item;
			cout<<getvalue(&mylist,item)<<endl;
			break;
		case 12:
			cout<<"请输入要修改的值:>";
			cin>>item;
			cout<<"请输入新的值:>";
			cin>>nitem;
			modify(&mylist,item,nitem);
			break;
		case 13:
			clear(&mylist);
			break;
		case 14:
			destory(&mylist);
			break;
		case 15:
			sort(&mylist);
			break;
		case 16:
			resver(&mylist);
			break;
		case 17:
			cout<<length(&mylist)<<endl;
			break;
		case 18:
			cout<<"请输入要查找的值:>";
			cin>>item;
			p=next(&mylist,item);
			if (NULL==p)
			{
				cout<<"查无此值!"<<endl;
			}
			else
			{
				cout<<p->data<<endl;
			}
			break;
		case 19:
			cout<<"请输入要查找的值:>";
			cin>>item;
			p=prio(&mylist,item);
			if (p==NULL)
			{
				cout<<"查无此值!"<<endl;
			}
			else
			{
				cout<<p->data<<endl;
			}
			break;
		default:
			break;
		}
	}
	
}

以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值