zoukankan      html  css  js  c++  java
  • 删除元素 不存在 NO 存在 输出余下元素

    #include<stdio.h>
    #include<stdlib.h>
    #define N 5
    #define NULL 0
    #define OK 1
    #define ERROR 0
    typedef struct LNode
    {
        int data;
        struct LNode *next;
    }LNode,*list;
    
    void creatList(list &l,int n)
    {
        list p,q;
        l=(list)malloc(sizeof(LNode));
        p=l;
        for (int i=0;i<n;i++)
        {
            q=(list)malloc(sizeof(LNode));
            scanf("%d",&q->data);
            p->next=q;
            p=q;
        }
        p->next=NULL;
    }
    int insertDeleteList(list l,int e)
    {
        list p,q;
        p=l->next;
        q=l;
        while (p)
        {
            if (p->data==e)
            {
                while (q->next!=p) q=q->next;
                q->next=p->next;
                free(p);
                return OK;
    
            }
            p=p->next;
        }
        return ERROR;
    }
    void printList(list l)
    {
        list p;
        p=l->next;
        while (p)
        {
            printf("%d ",p->data);
            p=p->next;
        }
    }
    int main()
    {
        list l;
        int e;
        printf("创建%d个元素的链表,请输入%d个元素:
    ",N,N);
        creatList(l,N);
        printf("请输入要判断的元素");
        scanf("%d",&e);
        if (!insertDeleteList(l,e))
            printf("NO ");
        else
            printList(l);
    }

    #include<stdio.h>

    #include<stdlib.h>

    #define N 5

    #define NULL 0

    #define OK 1

    #define ERROR 0

    typedef struct LNode {    

    int data;

        struct LNode *next;

    }LNode,*list;

    void creatList(list &l,int n)

    {     list p,q;

        l=(list)malloc(sizeof(LNode));

        p=l;

        for (int i=0;i<n;i++)

        {         q=(list)malloc(sizeof(LNode));  

           scanf("%d",&q->data);  

           p->next=q;

            p=q;    

         }   

      p->next=NULL;   

    }

    int insertDeleteList(list l,int e)

    {     list p,q;  

       p=l->next;

        q=l;

        while (p)

        {         if (p->data==e)

            {             while (q->next!=p)

           q=q->next;    

             q->next=p->next;

                free(p);  

               return OK;

            }

            p=p->next;

        }

        return ERROR;

    }

    void printList(list l)

    {     list p;

        p=l->next;

        while (p)

        {         printf("%d ",p->data);

            p=p->next;     }

    }

    int main()

    {     list l;

        int e;  

       printf("创建%d个元素的链表,请输入%d个元素: ",N,N);   

      creatList(l,N);   

      printf("请输入要判断的元素");    

    scanf("%d",&e);

        if (!insertDeleteList(l,e))         printf("NO ");

        else         printList(l);

    }

  • 相关阅读:
    [转]linux top 命令
    [转]Linux下实用的查看内存和多核CPU状态命令
    Totem ring protocal
    qemu qemusystemx86_64 qemukvm kvm 四个命令
    jsp>过滤器 小强斋
    jsp>Tomcat 6.0数据源配置记录 小强斋
    jsp>过滤器 小强斋
    jsp>监听器 小强斋
    jsp>Tomcat 6.0数据源配置记录 小强斋
    jsp>jsp执行过程 小强斋
  • 原文地址:https://www.cnblogs.com/fantasy12436109/p/3970958.html
Copyright © 2011-2022 走看看