C语言双向循环链表的建立

1.头文件

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


typedef struct Link{
    int data;
    struct Link *prev;
    struct Link *next;
}link;

2.功能函数

link *link_init()
{
    link *node = (link*)malloc(sizeof(link));
    node->prev = node;
    node->next = node;
    return node;
}

//头插法
void link_insert(link *head,int value)
{
    link *node = (link*)malloc(sizeof(link));
    node->data = value;
    if(head->next == head)
    {
	node->prev = head;
	node->next = head->next;
	head->next = node;
	head->prev = node;
    }else
    {
	node->prev = head;
	node->next = head->next;
	head->next->prev = node;
	head->next = node;
    }
}

//尾插法
void link_Insert_tail(link *head,int value)
{
    link *node = (link*)malloc(sizeof(link));
    node->data = value;
    link *temp = head;
    while(temp->next != head)
    {
	temp = temp->next;
    }
    node->next = temp->next;
    node->prev = temp;
    temp->next = node;
}

//删除
void link_del(link *head,int value)
{
    int ret = link_find(head,value);
    if(ret == -1)
    {
	printf("不存在此数\n");
	return;
    }
    link *temp = head->next;
    while(temp != head)
    {
	if(temp->data == value)
	{
	    temp->next->prev = temp->prev;
	    temp->prev->next = temp->next;
	    return;
	}
	temp = temp->next;
    }

}

//修改
void link_change(link *head,int old,int new)
{
    int ret = link_find(head,old);
    if(ret == -1)
    {
	printf("不存在此数\n");
	return;
    }
    link *temp = head->next;
    while(temp != head)
    {
	if(temp->data == old)
	{
	    temp->data = new;
	    break;
	}

	temp = temp->next;
    }
    
}

//查找
int link_find(link *head,int value)
{
    link *temp = head->next;
    int ret = 1;
    while(temp != head)
    {
	if(temp->data == value)
	{
	    printf("查找成功\n");
	    return ret;
	}

	ret++;
	temp = temp->next;
    
    }
    printf("查找失败\n");
    return -1;
}

//排序
void link_sort(link *head)
{
    int i,j,count = 0;
    link *p,*q;
    link *temp = head->next;
    while(temp != head)
    {
	count++;
	temp = temp->next;
    }
    for(i = 0;i < count;i++)
    {
	for(p = head->next,q = p->next;q != head;p = p->next,q = q->next)
	{
	    if(p->data > q->data)
	    {
		j = p->data;
		p->data = q->data;
		q->data = j;
	    }
	    
	}
    }
}

//奇升偶降
void link_k(link *head)
{
    link *temp = head->prev;
    link *q;
    int count = 1;
    int count1 = 1;
    while(temp != head)
    {
	if((temp->data % 2) == 0)
	{
	    q = temp->prev;
	    link_del(head,temp->data);
	    link_Insert_tail(head,temp->data);
	    temp = q;
	    count++;
	}else
	{
	    temp = temp->prev;
	}
	count1++;
    }
    printf("%d\n",count);
    printf("%d\n",count1);
}

//头部打印
void link_show(link *head)
{
    link *temp = head->next;
    while(temp != head)
    {
	printf("%d  ",temp->data);
	temp = temp->next;
    }
    printf("\n");
}

//尾部打印
void link_show_tail(link *head)
{
    link *temp = head->prev;
    while(temp != head)
    {
	printf("%d  ",temp->data);
	temp = temp->prev;
    }
    printf("\n");
}

3.主函数

int main(void)
{
    //头节点初始化
    link *head = link_init();

    //头插法
    printf("头插法插入函数\n");
    link_insert(head,1);
    printf("%d",head->prev->data);
    link_insert(head,2);
    link_insert(head,3);
    link_insert(head,4);
    link_insert(head,5);
    printf("打印数据\n");
    link_show(head);
    link_show_tail(head);

    //尾插法
    printf("尾插法插入函数\n");
    link_Insert_tail(head,6);
    link_Insert_tail(head,7);
    link_Insert_tail(head,8);
    link_Insert_tail(head,9);
    link_Insert_tail(head,10);
    printf("打印数据\n");
    link_show(head);
/*
    //查找数据
    int a;
    printf("请输入要查找的数据\n");
    scanf("%d",&a);
    int x = link_find(head,a);
    printf("在第%d个位置\n",x);

    //删除数据
    int b;
    printf("请输入要删除的数据\n");
    scanf("%d",&b);
    link_del(head,b);
    printf("打印数据\n");
    link_show(head);
  
    //修改数据
    int c,d;
    printf("请输入要修改的数据和新数据\n");
    scanf("%d%d",&c,&d);
    link_change(head,c,d);
    printf("打印数据\n");
    link_show(head);
*/
    //排序
    printf("数据排序\n");
    link_sort(head);
    printf("打印数据\n");
    link_show(head);

    //奇升偶降
    printf("奇升偶降\n");
    link_k(head);
    printf("打印数据\n");
    link_show(head);
    

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值