23-5-22--链表--单向链表的建立

发布时间 2023-06-01 15:23:45作者: Daniel350
#include <stdio.h>
#include <stdlib.h>

    struct link *AppendNode(struct link *head);
    void DisplyNode(struct link *head);
    void DeleteMemory(struct link *head);
    struct link *DeleteNode(struct link *head,int nodeDate);
    struct link *InsertNode(struct link *head,int nodeDate);
    
    struct link{
        int date;
        struct link *next;
    };
    
    int main()
    {
        int i=0;
        struct link *head=NULL;
        char c;
        printf("DO YOU WANT TO APPEND A NEW CODE(Y/ N)?");
        scanf(" %c",&c);
    
        while(c=='Y'||c=='y')
        {
            head=AppendNode(head);
            DisplyNode(head);
            printf("DO YOU WANT TO APPEND A NEW CODE(Y/N)?");
            scanf(" %c",&c);
            i++;
        }
        
        printf(" %d次 \n",i);
        printf("do you want insert date(y/n)?");
        scanf(" %c",&c);
        if(c=='y')
        {
            int x;
            scanf("%d",&x);
            InsertNode(head,x);
            DisplyNode(head); 
        }
        
        DeleteMemory(head);
        
        
    }
    struct link *AppendNode(struct link *head)
    {
        struct link *p=NULL,*pr=head;
        int date;
        p=(struct link*)malloc(sizeof(struct link));
        if(p==NULL)
        {
            printf("NO ENOUGH TO MOLLOC");
            exit(0);
        }
        if(head==NULL)
        {
             head=p;
        }else{
            while(pr->next != NULL )
            {
                pr=pr->next ;
            }
            pr->next = p;
        }
        p->next =NULL;
        scanf("%d",&p->date );
        return head;
    }
    
    void DisplyNode(struct link *head)
    {
        struct link *p=head;
        while(p!=NULL)
        {
            printf("%d ",p->date );
            p=p->next ;
        }
    }
    
    void DeleteMemory(struct link *head)
    {
        struct link *p=head,*pr=NULL;
        while(p != NULL)
        {
            pr=p;
            p=p->next ;
            free(pr);
        }
    }
    struct link *DeleteNode(struct link *head,int nodeDate)
    {
        struct link *p=head,*pr=NULL;
        if(head==NULL)
        {
            printf("空表");
            return head; 
        }
        if(head->date ==nodeDate)
        {
            head=head->next ;
        }else{
            p=p->next;
        }
        while(p->next !=NULL&&p->date != nodeDate)
        {
            pr=p;
            p=p->next ;
        }
        if(p==NULL)
        {
            printf("未找到\n");
            
        }else{
            pr->next = p->next ;
            free(p);
        }
        return head;
    }
    
    struct link *InsertNode(struct link *head,int nodeDate)
    {
        struct link *p=NULL,*pr=head,*temp=NULL;
        p=(struct link* )malloc(sizeof(struct link));
        if(head==NULL)
        {
            head=p;
            p->date = nodeDate;
        }else{
            while(pr->date <= nodeDate&&pr->next != NULL )
            {
                temp=pr;
                pr=pr->next ;
                
            }
            if(pr->next ==NULL)
            {
                pr->next =p;
                p->date = nodeDate;
            }else{
                temp->next =p;
                p->next =pr;
                p->date =nodeDate;
                
            }
            return head;
        }
        
        
        
        
    }