链表的删除

给定一个元素,删除链表中所有这个元素

#include<stdio.h>  
#include<stdlib.h>  
typedef struct _Node  
{  
    int data;  
    struct _Node *next;  
}Node;  
  
typedef struct _list{  
    Node *head;  
}List;  
  
void add(List *pList,int number)  
{  
    Node *p=(Node*)malloc(sizeof(Node));  
    p->data=number;  
    p->next=NULL;  
    Node *last=pList->head;  
    if(last)  
    {  
        while(last->next)  
        {  
            last=last->next;  
        }  
        last->next=p;  
    }  
    else  
         pList->head=p;  
}  
  
  
void add2(List *pList,int number)  
{  
    Node *p=(Node*)malloc(sizeof(Node));  
    p->data=number;  
    p->next=pList->head;  
    pList->head=p;  
}  
void add3(List *pList,int number)  
{  
    Node *p=(Node*)malloc(sizeof(Node));  
    p->data=number;  
    p->next=NULL;  
    if(pList->head==NULL||p->data<pList->head->data)  
    {  
         p->next=pList->head;  
         pList->head=p;  
    }  
    else  
    {  
        Node *q=pList->head;  
        while(q->next&&p->data>q->next->data)  
        {  
            q=q->next;  
        }  
        if(q->next==NULL)  
        {  
            q->next=p;  
        }  
        else  
        {  
            p->next=q->next;  
            q->next=p;  
        }  
    }  
      
}
void deletel(List *pList,int m)
{
	if(pList->head->data==m)
	{
		pList->head=pList->head->next;
	}

	
		Node *p=pList->head;
	
		while(p->next)
		{
			if(p->next->data==m)
			{
				Node *q;
				q=p->next;
				p->next=q->next;
				free(q);
			}
			else
			{
				p=p->next;
			}
		}
	
}
void showL(List *pList)  
{  
    Node *p;  
    for(p=pList->head;p;p=p->next)  
    {
	    if(p->next==NULL)
		printf("%d\n",p->data);
		else  
        printf("%d ",p->data);  
    }  
      
}  
int count(List *pList)
{
	int t=0;
	 Node *p;  
    for(p=pList->head;p;p=p->next)  
    {  
      t++;
    }  
      return t;
}
  
int main()  
{  
    List L;  
    L.head=NULL;  
    int n,temp,t;
    scanf("%d",&n);  
    for(int i=0;i<n;i++)  
    {  
        scanf("%d",&temp);  
        add(&L,temp);  
    } 
    int m;  
    scanf("%d",&m); 
    t=count(&L);
    printf("%d\n",t);
    showL(&L);
    deletel(&L,m); 
    t=count(&L);
	printf("%d\n",t); 
    showL(&L);  
  
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值