循环链表的C语言实现

//循环链表的C语言实现 

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//双链表结点结构 
typedef struct _CYCLE_DOUBLE_LINK_NODE{
	int data;
	struct _CYCLE_DOUBLE_LINK_NODE *prior;
	struct _CYCLE_DOUBLE_LINK_NODE *next; 
}CYCLE_DOUBLE_LINK_NODE;

//创建空的双向循环链表 ,表头值为value; 
CYCLE_DOUBLE_LINK_NODE *Create(int value){
	CYCLE_DOUBLE_LINK_NODE *head;
	head=(CYCLE_DOUBLE_LINK_NODE*)malloc(sizeof(CYCLE_DOUBLE_LINK_NODE));
	if(head==NULL){
		printf("创建失败!");
		return NULL;
	}
	head->prior=head;
	head->next=head;
	head->data=value;
	return head;
} 

//创建双向循环链表,长度为length(包含头结点) 
CYCLE_DOUBLE_LINK_NODE *Create_length(int value,int length){
	CYCLE_DOUBLE_LINK_NODE *head,*p,*pnew;
	head=p=(CYCLE_DOUBLE_LINK_NODE*)malloc(sizeof(CYCLE_DOUBLE_LINK_NODE));
	head->data=-1;
	if(p==NULL){
		printf("创建失败!");
		return NULL;
	}
	while((length-1)>0){
		pnew=(CYCLE_DOUBLE_LINK_NODE*)malloc(sizeof(CYCLE_DOUBLE_LINK_NODE));
		pnew->data=length-1;
		pnew->next=head;
		pnew->prior=p;
		p->next=pnew;
		head->prior=pnew;
		length--;
		p=pnew;
	}
	return head;
}

//插入指定数据,到循环链表的指定位置 
CYCLE_DOUBLE_LINK_NODE *Insert(CYCLE_DOUBLE_LINK_NODE *head,int location,int value){
	CYCLE_DOUBLE_LINK_NODE *pnew,*s,*t;
	int i=0;
	pnew=head;
	s=(CYCLE_DOUBLE_LINK_NODE*)malloc(sizeof(CYCLE_DOUBLE_LINK_NODE));
	s->data=value;
	for(i=0;i<location-1;i++){
		pnew=pnew->next;
	}
	t=pnew->next;
	s->next=pnew->next;
	s->prior=pnew;
	pnew->next=s;
	t->prior=s;
	return head;
}

//打印链表 
void Printf(CYCLE_DOUBLE_LINK_NODE *head){
	CYCLE_DOUBLE_LINK_NODE *pnew;
	pnew=head;
	do
	{
		printf(" %d ",pnew->data);
		pnew=pnew->next;
	}while(pnew->data!=-1);
	printf("\n");
}


int main(int argc, char *argv[]) {
	CYCLE_DOUBLE_LINK_NODE *cycleDoubleLinkNode;
	cycleDoubleLinkNode=Create_length(-1,5);
	Printf(cycleDoubleLinkNode);
	Insert(cycleDoubleLinkNode,2,0);
	Printf(cycleDoubleLinkNode);
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值