//循环链表的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;
}
循环链表的C语言实现
最新推荐文章于 2023-10-15 12:54:31 发布