Csdn_tyblll 2022-04-22 09:12 采纳率: 100%
浏览 295
已结题

编写程序实现单向链表的建立、读取和删除函数(语言-c语言)

链表节点类型如下: typedef char datatype; struct node { datatype data; struct node *next; };
编写程序实现单向链表的建立、读取和删除函数,在主函数中选择执行。
输入用例:
5abcde
输出用例:
The content of list:abcde
The content of list:null

  • 写回答

3条回答 默认 最新

  • CSDN专家-link 2022-04-22 09:31
    关注

    读取就是链表内容显示?

    #include <stdio.h>
    typedef char datatype; 
    struct node 
    { 
        datatype data; 
        struct node *next; 
    };
    struct node * listCreate(int n)
    {
        struct node * head, *p,*q;
        int i;
        head = (struct node*) malloc(sizeof(struct node));
        head->next = NULL;
        p = head;
        for(i=0;i<n;i++)
        {
            q = (struct node*) malloc(sizeof(struct node));
            scanf("%c",&q->data);
            q->next = NULL;
            p->next = q;
            p = q;
        }
        return head;
    }
    
    void showNode(struct node *head)
    {
        printf("The content of list:");
        if(head == NULL || head->next == NULL)
        {
            printf("NULL\n");
            return;
        }
        while(head->next != NULL)
        {
            printf("%c",head->next->data);
            head = head->next;
        }
        printf("\n");
    }
    
    void clearList(struct node *head)
    {
        struct node *p = head,*q;
        while(p->next != NULL)
        {
            q = p->next;
            p->next = p->next->next;
            free(q);
        }
    }
    
    int main()
    {
        int n;
        struct node * head;
        scanf("%d",&n);
        head = listCreate(n);
        showNode(head);
        clearList(head);
        showNode(head);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月30日
  • 已采纳回答 4月22日
  • 创建了问题 4月22日

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同