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

编写程序实现单向链表的建立、读取和删除函数(语言-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日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法