【Destroy the linked list】

#include<stdio.h>
#include<stdlib.h>

struct NODE
{
    
    
    char name[20] ;
    int age ;
    char sex ;
    char num[20] ;
    
    struct NODE * next ;
};

struct NODE * CreateLink(void);
void IntputLink( struct  NODE * ) ;
void OutputLink( struct NODE * ) ;
void DestroyLink( struct NODE * ) ;

int main ()
{
    
    
    char ch = '\0' ;
    struct NODE * head = NULL ;
    
    /*-----------------------------创建链表------------------------------*/
    
    printf("是否创建链表(Y/N):");
    while ( 1 )
    {
    
    
        scanf("%c",&ch);
        getchar() ;
        if ( ( 'Y' == ch ) || ( 'y' == ch ) )
        {
    
    
            head = CreateLink();
            IntputLink(head);
            OutputLink(head);
            
            break ;
        }
        else if ( ( 'N' == ch ) || ( 'n' == ch ) )
        {
    
    
            return 0 ;
        }
        else
        {
    
    
            printf("请重新输入:\n");
        }
    }
    /*------------------------------销毁链表--------------------------------*/
    
    printf("是否销毁链表(Y/N):");
    ch = '\0' ;
    while ( 1 )
    {
    
    
        scanf("%c",&ch);
        getchar () ;
        if ( ( 'Y' == ch ) || ( 'y' == ch ) )
        {
    
    
            DestroyLink(head) ;
            break ;
        }
        else if ( ( 'N' == ch ) || ( 'n' == ch ) )
        {
    
    
            break ;
        }
        else
        {
    
    
            printf("请重新输入(Y/N):");
        }
    }
    return 0 ;
}

struct NODE * CreateLink (void)
{
    
    
    int i = 0 ;
    int cnt = 0 ;
    struct NODE * head = malloc( sizeof * head ) ;
    struct NODE * move ;
    
    if ( NULL == head )
    {
    
    
        printf("内存分配失败,程序终止!\n");
        exit (-1);
    }
    
    move = head ;
    move->next = NULL ;
    
    printf("请输入学生的数量:");
    scanf("%d",&cnt);
    getchar();
    
    for ( i = 1 ; i <= cnt ; i ++ )
    {
    
    
        struct NODE * fresh = malloc ( sizeof * fresh ) ;
        if ( NULL == fresh )
        {
    
    
            printf("内存分配失败,程序终止!\n");
            exit(-1);
        }
        
        //结点连接三部曲
        move->next = fresh ;
        fresh->next = NULL ;
        move = fresh ;
    }
    return head ;
}

void IntputLink ( struct NODE * head )
{
    
    
    int i = 1 ;
    struct NODE * move = head->next ;
    
    while ( NULL != move )
    {
    
    
        printf("请输入第%d个学生的信息:",i);
        scanf("%s%d %c%s",move->name , &move->age , &move->sex , move->num ) ;
        getchar();
        
        move = move->next ;
        i++ ;
    }
    return ;
}

void OutputLink ( struct NODE * head )
{
    
    
    struct NODE * move = head ;
    if ( NULL == move )
    {
    
    
        printf("为创建链表\n");
        return ;
    }
    
    if ( NULL == move->next )
    {
    
    
        printf("链表为空\n");
    }
    
    while ( NULL != move->next )
    {
    
    
        printf("【姓名:%s,年龄:%d,性别:%c,学号:%s】->",move->next->name , move->next->age , move->next->sex , move->next->num );
        move = move->next ;
    }
    printf("[^]\n");
}

void DestroyLink ( struct NODE * head )
{
    
    
    struct NODE * save = head ;
    
    while ( NULL != head )
    {
    
    
        save = head->next ;
        free(head);
        head = save ;
    }
    
    if ( NULL == head )
    {
    
    
        printf("链表以销毁!\n");
    }
}

是否创建链表(Y/N):y
请输入学生的数量:3
请输入第1个学生的信息:欧阳丽 24 F Z1207038
请输入第2个学生的信息:冯小莉 20 F Z1207025
请输入第3个学生的信息:陈商清 23 F Z1207024
【姓名:欧阳丽,年龄:24,性别:F,学号:Z1207038】->【姓名:冯小莉,年龄:20,性别:F,学号:Z1207025】->【姓名:陈商清,年龄:23,性别:F,学号:Z1207024】->[^]
是否销毁链表(Y/N):y
链表以销毁!
Program ended with exit code: 0

Guess you like

Origin blog.csdn.net/m0_74282485/article/details/128537943