脾气好一点17 2021-09-25 14:36 采纳率: 75%
浏览 55
已结题

数据结构尾插法 求指点 完全不会

img

img

  • 写回答

2条回答 默认 最新

  • zerokingq 2021-09-25 17:37
    关注
    
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct node
    {
        char data;
        struct node* next;
    };
    
    struct node* tailinsert();
    void destroy(struct node* head);
    void traverse(struct node* head);
    
    
    void traverse(struct node* head)
    {
        if (head == NULL)
        {
            return;
        }
        else
        {
            struct node* tmp = head->next;
    
            while (tmp != NULL)
            {
                if (tmp->next != NULL)
                {
                    printf("%c ", tmp->data);
                }
                else
                {
                    printf("%c", tmp->data);
                }
                tmp = tmp->next;
            }
            printf("\n");
        }
    }
    
    
    void destroy(struct node* head)
    {
        if (head == NULL)
        {
            return;
        }
        else
        {
            struct node* P;
            struct node* tmp;
            P = head->next;
            head->next = NULL;
            while (P != NULL)
            {
                tmp = P->next;
                free(P);
                P = tmp;
            }
            free(head);
            head = NULL;
        }
    }
    
    struct node* tailinsert()
    {
        char arr[5];
        int i = 0;
        scanf("%s",arr);
        struct node* begin = (struct node*)malloc(sizeof(struct node));
        if (begin == NULL)
        {
            return;
        }
        struct node* end = begin;
        end->next = NULL;
        for (i = 0; i < 5; i++)
        {
            struct node* new = (struct node*)malloc(sizeof(struct node));
            if (new == NULL)
            {
                return;
            }
            new->data = arr[i];
            new->next = NULL;
            end->next = new;
            end = new;
        }
        return begin;
    }
    
    void main()
    {
        struct node* head = NULL;
        head = tailinsert();
        traverse(head);
        destroy(head);
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月3日
  • 已采纳回答 9月25日
  • 创建了问题 9月25日