码迷,mamicode.com
首页 > 其他好文 > 详细

删除元素 不存在 NO 存在 输出余下元素

时间:2014-09-14 14:05:17      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   div   sp   log   c   

#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,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,N);   

 

  creatList(l,N);   

 

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

scanf("%d",&e);

 

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

 

    else         printList(l);

}

 

删除元素 不存在 NO 存在 输出余下元素

标签:style   blog   color   io   for   div   sp   log   c   

原文地址:http://www.cnblogs.com/fantasy12436109/p/3970958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!