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日

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站