C语言双向链表的建立

1.头文件

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

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

2.功能函数

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

//头插法
void link_insert(link *head,int value)
{
    link *node = (link*)malloc(sizeof(link));
    node->data = value;
    if(head->next == NULL)
    {
	node->prev = head;
	node->next = head->next;
	head->next = 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 != NULL)
    {
	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;
    int i;
    while(temp != NULL)
    {
	if(temp->data == value)
	{
	    temp->next->prev = temp->prev;
	    temp->prev->next = temp->next;
	    free(temp); 
	    break;
	}
	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 != NULL)
    {
	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 != NULL)
    {
	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 != NULL)
    {
	count++;
	temp = temp->next;
    }
    for(i = 0;i < count;i++)
    {
	for(p = head->next,q = p->next;q != NULL;p = p->next,q = q->next)
	{
	    if(p->data > q->data)
	    {
		j = p->data;
		p->data = q->data;
		q->data = j;
	    }
	    
	}
    }
}

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

3.主函数

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

    //头插法
    printf("头插法插入函数\n");
    link_insert(head,1);
    link_insert(head,2);
    link_insert(head,3);
    link_insert(head,4);
    link_insert(head,5);
    printf("打印数据\n");
    link_show(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);
        
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值