链表(建立+插入+删除+输出)

本文介绍如何使用C语言实现动态链表的基本操作,包括删除节点和插入节点。通过实例演示了如何在有序数列中进行数据操作。
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int date;
    node *next;
}s[100];
int main()
{
    int n,i,t;
    while(~scanf("%d",&n))
    {
        node *root,*p;
        root = (node *)malloc(sizeof(node));
        root -> next = NULL;
        p = root;

        //建立
        for(i = 0 ; i < n ; i ++)
        {
            node *k = (node *)malloc(sizeof(node));
            scanf("%d",&t);
            k -> date = t;
            k -> next = NULL;
            p -> next = k;
            p = p -> next;
        }

        //删除
        int delete_num;
        printf("输入删除的数据:");
        scanf("%d",&delete_num);
        p = root;
        while(p -> next != NULL)
        {
            if(p -> next -> date == delete_num)
            {
                p -> next = p -> next -> next;
                break;
            }
            p = p -> next;
        }


        //插入
        //在有序数列中插入数据
        int insert_num;
        printf("插入的数据:");
        scanf("%d",&insert_num);
        p = root;
        while(p -> next != NULL)
        {
            if(p -> next -> date >= insert_num)
            {
                node *k = (node *)malloc(sizeof(node));
                k -> date = insert_num;
                k -> next = p -> next;
                p -> next = k;
                break;
            }
            p = p -> next;
        }

        //输出
        p = root;
        while(p -> next != NULL)
        {
            printf("%d ",p -> next ->date);
            p = p -> next;
        }
    }
    return 0;
}

练习:http://acm.hdu.edu.cn/showproblem.php?pid=2019

转载于:https://www.cnblogs.com/dyllove98/p/3162997.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值