【删除结点】--创建链表储存学生的信息,再根据某名学生的学号将该名学生的信息删除

创建链表存储学生信息,一个结点存储一个学生的信息,包括姓名、年龄、性别、学号。要求手动输入创建链表的长度,然后创建动态链表,并通过键盘给每一个结点赋值,最后将整个链表输出。再根据学生的学号将该学号的学生信息删除,并输出删除结点后的整个链表。

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

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

struct NODE * CreateLink(void) ;
void Intput( struct NODE * );
void OutputLink ( struct NODE *);
void DeleteNode ( 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();
            Intput(head);
            OutputLink(head);
            
            break ;
        }
        else if ( ( 'N' == ch ) || ( 'n' == ch ) )
        {
    
    
            return 0 ;
        }
        else
        {
    
    
            printf("请重新输入(Y/N):");
        }
    }
    
    /*-------------------------------删除结点---------------------------------*/
    
    printf("是否要删除结点(Y/N):");
    ch = '\0' ;
    while(1)
    {
    
    
        scanf("%c",&ch);
        getchar();
        
        if ( ( 'Y' == ch ) || ( 'y' == ch ) )
        {
    
    
            DeleteNode(head);
            OutputLink(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 Intput( 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("[^]");
}

void DeleteNode ( struct NODE * head )
{
    
    
    char num[20] = "\0" ;
    struct NODE * save ;
    
    printf("请输入您想要删除的学生的学号:");
    
    while ( 1 )
    {
    
    
        struct NODE * move = head ;
        
        scanf("%s",num);
        getchar();
        
        while( NULL != move->next )
        {
    
    
            if ( 0 == strcmp ( num , move->next->num ) )
            {
    
    
                save = move->next ;
                move->next = move->next->next ;
                free(save);
                
                save = NULL ;
                
                return ;
            }
            
            move = move->next ;
        }
        printf("无该结点,请重新输入:\n");
    }
}

是否创建链表(Y/N):y
请输入学僧你的数量:3
请输入地1个学生的姓名、年龄、性别、学号:周琴琴 25 F Z1207035
请输入地2个学生的姓名、年龄、性别、学号:杜娇洋 19 F Z1207041
请输入地3个学生的姓名、年龄、性别、学号:欧阳丽 24 F Z1207038
[姓名:周琴琴,年龄:25,性别:F,学号:Z1207035]->[姓名:杜娇洋,年龄:19,性别:F,学号:Z1207041]->[姓名:欧阳丽,年龄:24,性别:F,学号:Z1207038]->[^]是否要删除结点(Y/N):y
请输入您想要删除的学生的学号:Z1207041
[姓名:周琴琴,年龄:25,性别:F,学号:Z1207035]->[姓名:欧阳丽,年龄:24,性别:F,学号:Z1207038]->[^]Program ended with exit code: 0

猜你喜欢

转载自blog.csdn.net/m0_74282485/article/details/128537506