C语言程序例程017

链表相关操作,增删改查

链表的创建 链表的遍历 链表的释放 链表节点的查找 链表节点的删除 链表中插入一个节点 链表排序

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


struct DATE{
    int year;
    int month;
    int day;
};


typedef struct STU{
    char name[18];
    struct DATE birthday;
    struct STU *next;
}*STU;
/*链表的创建 链表的遍历 链表的释放 链表节点的查找 链表节点的删除 链表中插入一个节点 链表排序 */
int help()
{
    printf("%s\n",__FUNCTION__);
    int i;
    printf("***********************************\n");
    printf("***********insert:1****************\n");
    printf("***********delete:2****************\n");
    printf("***********quiry:3*****************\n");
    printf("***********print:4*****************\n");
    printf("***********order:5*****************\n");
    printf("***********quit:6******************\n");    
    printf("***********************************\n");
    do{
        printf("please input cmd:\n");
        printf("->");
        scanf("%d",&i);
        getchar();
        
    }while(i < 0||i > 6);
    
    
    return i;
}


STU creat_list_head_node()
{
    STU head;
    if((head = malloc(sizeof(struct STU)))==NULL)
    {
        perror("creat head node err\n");
        exit(-1);
    }
    head->next = NULL;
    return head;
    
}
int judge(STU head,char *p)
{
    printf("%s\n",__FUNCTION__);
    if(head == NULL)
    {
        perror("judge error\n");
        exit(-1);
    }
    
    STU tmp = head->next;
    if(tmp == NULL)
        return 1;
    while(strcmp(tmp->name,p) != 0)
    {
        tmp = tmp->next;
        if(tmp == NULL)
            return 1;
    }
    return 0;
}
    
void inset_node(STU head)
{
    printf("%s\n",__FUNCTION__);
    int ret;
    char ch;
    if(head == NULL)
    {
        perror("insert_node head error\n");
        exit(-1);
    }
    STU newnode;
    char buf[18];
    if((newnode = malloc(sizeof(struct STU)))==NULL)
    {
        perror("creat head node err\n");
        exit(-1);
    }
loop1:    
    printf("please input name:\n");
    scanf("%s",buf);
    getchar();
    ret = judge(head,buf);
    if(ret == 0)
    {
        printf("name you input exit!\n");
        printf("Do you want to continue?Y/N:");
        scanf("%c",&ch);
        getchar();
        if(ch == 'Y'||ch == 'y')
        {
            goto loop2;
        }
        else
        {
            goto loop1;
        }
    }
loop2:
    strcpy(newnode->name,buf);
    printf("please input birthday(year month day):\n");    
    scanf("%d%d%d",&(newnode->birthday.year),&(newnode->birthday.month),&(newnode->birthday.day));
    /*printf("%d\n",newnode->birthday.year);
    printf("%d\n",newnode->birthday.month);
    printf("%d\n",newnode->birthday.day);*/
    getchar();
    while(head->next != NULL)
        head = head->next;
    head->next = newnode;
    newnode->next = NULL;
    printf("done!\n");     
    
}
void delete_node(STU head)
{
    printf("%s\n",__FUNCTION__);
    if(head == NULL)
    {
        perror("delete head error\n");
        exit(-1);
    }
    if(head->next == NULL)
    {
        printf("the list is empty\n");
        return ;
    }
    char buf[18];
    printf("please input the name to delete:\n");
    scanf("%s",buf);
    STU tmp = head->next;
    while(tmp)
    {
        if(strcmp(tmp->name,buf) == 0)
        {
            while(head->next != tmp)
                head = head->next;
            head->next = tmp->next;
            free(tmp);
            tmp = NULL;
            printf("done!\n");
            return ;
        }
        tmp = tmp->next;
    }
    printf("the name no exit in the list!\n");
    return ;
}
void print(STU head)
{
    printf("%s\n",__FUNCTION__);
    if(head == NULL)
    {
        perror("print head error\n");
        exit(-1);
    }
    if(head->next == NULL)
    {
        printf("the list is empty\n");
        return ;
    }
    STU tmp = head->next;
    while(tmp !=NULL)
    {
        printf("name:%s birthday:%d:%d:%d\n",tmp->name,tmp->birthday.year,tmp->birthday.month,tmp->birthday.day);
        tmp = tmp->next;
    }
    return ;
}
void quiry(STU head)
{
    printf("%s\n",__FUNCTION__);
    if(head == NULL)
    {
        perror("quiry head error\n");
        exit(-1);
    }
    if(head->next == NULL)
    {
        printf("the list is empty\n");
        return ;
    }
    char buf[18];
    printf("please input the name to quiry:\n");
    scanf("%s",buf);
    getchar();
    STU tmp = head->next;
    while(tmp)
    {
        if(strcmp(tmp->name,buf) == 0)
        {
            printf("***************quiry  done***************\n");
            printf("name:%s birthday:%d:%d:%d\n",tmp->name,tmp->birthday.year,tmp->birthday.month,tmp->birthday.day);
            printf("*****************************************\n");
            return ;
        }
        tmp = tmp->next;
    }
    printf("the name no exit in the list!\n");
    return ;
}
void order(STU head)
{
    printf("%s\n",__FUNCTION__);
    if(head == NULL)
    {
        perror("order head error\n");
        exit(-1);
    }
    if(head->next == NULL)
    {
        printf("the list is empty\n");
        return ;
    }
    printf("order the list...\n");
    STU buf;
    buf = (STU)malloc(sizeof(struct STU ));
    if(buf == NULL)
    {
        perror("order malloc buff err\n");
        exit(-1);
    }
    STU tmp = head->next;
    STU pcom = tmp->next;
    for(tmp = head->next;tmp->next != NULL;tmp = tmp->next)
    {
        for(pcom = tmp->next;pcom != NULL;pcom = pcom->next)
        {
            if(tmp->birthday.year > pcom->birthday.year)
            {
                buf->birthday = tmp->birthday;
                strcpy(buf->name,tmp->name);
                tmp->birthday = pcom->birthday;
                strcpy(tmp->name,pcom->name);
                pcom->birthday = buf->birthday;
                strcpy(pcom->name,buf->name);
                
            }    
            else if(tmp->birthday.year == pcom->birthday.year)
            {
                if(tmp->birthday.month > pcom->birthday.month)
                {
                    buf->birthday = tmp->birthday;
                    strcpy(buf->name,tmp->name);
                    tmp->birthday = pcom->birthday;
                    strcpy(tmp->name,pcom->name);
                    pcom->birthday = buf->birthday;
                    strcpy(pcom->name,buf->name);
                }
                else if(tmp->birthday.month == pcom->birthday.month)
                {
                    if(tmp->birthday.day > pcom->birthday.day)
                    {
                        buf->birthday = tmp->birthday;
                        strcpy(buf->name,tmp->name);
                        tmp->birthday = pcom->birthday;
                        strcpy(tmp->name,pcom->name);
                        pcom->birthday = buf->birthday;
                        strcpy(pcom->name,buf->name);
                    }
                }
            }
            //printf("%s %d:%d:%d",buf->name,buf->birthday.year,buf->birthday.month,buf->birthday.day);
        }
    }
    printf("done!\n");
    return ;
    
}
void free_list(STU head)
{
    printf("%s\n",__FUNCTION__);
    if(head == NULL)
    {
        perror("free error\n");
        exit(-1);
    }
    STU tmp = head->next;
    while(tmp)
    {
        head->next = tmp->next;
        free(tmp);
        tmp = head->next;
    }
    free(head);
    head = NULL;
    printf("done!\n");
    return ;
}
int main()
{
    STU head;
    head = creat_list_head_node();
    int ret;
    while(1)
    {
        ret = help();
    
        switch(ret)
        {
            case 1:
                inset_node(head);
                break;
            case 2:
                delete_node(head);
                break;
            case 3:
                quiry(head);
                break;
            case 4:
                print(head);
                break;
            case 5:
                order(head);
                break;
            case 6:
                free_list(head);
                head = NULL;
                exit(1);
                break;
            default:
                break;
        }
    }
    
    return 0;    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值