实战数据结构(5)_双向循环链表的基本操作

/************************************************************************/
/* @author lynnbest
双向循环列表的使用:
1.创建
2.插入
3.删除
4.打印
5.按位置查找
6.按内容查找
7.退出                                                                */
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
	int data;
	struct node *prior;
	struct node *next;
}Dlistnode;
Dlistnode *CreateDClist(int n);
void printflist(Dlistnode *head);
int lengthlist(Dlistnode *head);
void InsertDClist(Dlistnode *head,int pos);
void DeleteDClist(Dlistnode *head,int pos);
void main()
{
	printf("   双向循环链表基本操作	\n");
	printf("----by lynnbest ----\n\n");
	int  choice,num;
	Dlistnode *head;
	while(1)
	{
		printf("1---创建一个双向循环链表\n");
		printf("2---插入节点操作\n");
		printf("3---删除节点操作\n");
		printf("4---打印链表元素\n");
		printf("5---查找操作(按位置)\n");
		printf("6---查找操作(按内容)\n");
		printf("7---退出\n请输入操作:\n");
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:
					printf("请输入创建元素的个数:\n");
					scanf("%d",&num);
					head=CreateDClist(num);
					break;
			case 2:	
					printf("输入插入的位置:\n");
					scanf("%d",&num);
					InsertDClist(head,num);
					break;
			case 3:
					printf("输入删除的位置:\n");
					scanf("%d",&num);
					DeleteDClist(head,num);
					break;
			case 4:
				printflist(head);
			//	printf("共有%d个元素\n",lengthlist(head));
				break;
			case 5:
				break;
			case 6:
				break;
			case 7:
				return ;
				break;
			default :
				break;
		}
		
	}


}
Dlistnode *CreateDClist(int n) //创建一个带头节点的双向循环链表
{
	Dlistnode *head,*newnode,*pre;
	int data;
	if(NULL==(head=(Dlistnode *)malloc(sizeof(Dlistnode))))
	{
		printf("头结点创建失败\n");
		exit(-1);
	}
	pre=head;
	for(int i=1;i<=n;i++)
	{
		if(NULL==(newnode=(Dlistnode *)malloc(sizeof(Dlistnode))))
		{
			printf("创建失败\n");
			exit(-1);
		}
		printf("请输入第%d个元素\n",i);
		scanf("%d",&data);
		newnode->data=data;
		//开始插入
		pre->next=newnode;	
		newnode->prior=pre;
		pre=newnode;
		newnode->next=NULL;
	}
	newnode->next=head;
	head->prior=newnode;	//做首位循环
	return head;
}
void printflist(Dlistnode *head)
{
	Dlistnode *cur=head->next;
	while(cur->next!=head)
	{
		printf("%3d",cur->data);
		cur=cur->next;
	}
	printf("%3d\n",cur->data);
		return ;
}
int lengthlist(Dlistnode *head)
{
	Dlistnode *cur=head;
	int count=0;
	while(cur->next!=head)
	{	
		count++;
		cur=cur->next;
	}
	return count;
}
void InsertDClist(Dlistnode *head,int pos)
{	
	if(pos<1||pos>lengthlist(head)+1){
		printf("插入位置非法\n");
		return ;
	}
	Dlistnode *cur=head,*newnode;
	int data;
	//完成插入点数据建立
	if(NULL==(newnode=(Dlistnode *)malloc(sizeof(Dlistnode))))
	{
		printf("创建失败\n");
		exit(-1);
	}
	printf("请输入要插入的节点数据:\n");
	scanf("%d",&data);
	newnode->data=data;
	for(int i=0;i<pos;i++)	//查找要插入的位置
		cur=cur->next;
	//开始插入
	cur->prior->next=newnode;	//插入需要4步
	newnode->prior=cur->prior;
	newnode->next=cur;
	cur->prior=newnode;
}
void DeleteDClist(Dlistnode *head,int pos)
{
	if(pos<1||pos>lengthlist(head)){
		printf("删除位置非法\n");
		return ;
	}
	Dlistnode *cur=head;
	for(int i=0;i<pos;i++)	//查找要删除节点的位置
		cur=cur->next;
	//删除 需要3步
	cur->prior->next=cur->next;	//前一个指针指向删除节点的下一个
	cur->next->prior=cur->prior;//删除节点的前驱指针 指向删除节点前一个节点
	free(cur);//释放堆
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值