剑指offer--面试题18:删除链表的结点


#include<stdio.h>    
#include<malloc.h>      
    
typedef struct LNode    
{    
    int           data;    
    struct LNode *next;    
}*LinkList;    
    
LinkList Create_List_Tail(int length)    
{//建立链表    
    LNode *L,*s,*r;  //L指向头结点,r指向尾结点,s指向新添加结点    
    L=(LinkList)malloc(sizeof(LNode));    
    L->next =NULL;//头结点L->data不存东西    
    r=L;    
    for(int i=0;i<length;++i)    
    {    
        s=(LinkList)malloc(sizeof(LNode));    
        scanf("%d",&s->data);    
        r->next =s;    
        r =s;    
    }    
    r->next =NULL;    
    return L;  //返回链表    
}    
   
void PrintList(LinkList L)    
{      
    LNode* pNode = L->next ;    
    if(!pNode)    
        printf("空表!\n");    
    while(pNode)    
    {    
        printf("%d ", pNode->data);    
        pNode = pNode->next;    
    }    
    printf("\n");    
}    
LNode *Find(LinkList L,int data)
{
	LNode *p=L->next ;
	while(p&&p->data !=data)
		p=p->next ;
	if(!p)
		printf("不存在!\n");
	return p;
}
void DeleteNode(LinkList L,LNode *p)
{
	if(!L||!L->next!=NULL)
		return;
	// 要删除的结点不是尾结点
	if(p->next!=NULL)
	{
		LNode *q=p->next;
		p->data=q->data;
		p->next=q->next;
		free(q);
	}
	// 链表只有一个结点,删除头结点(也是尾结点)
	else if(L->next==p)
	{
		free(p);
		L->next=NULL;
	}
	// 链表中有多个结点,删除尾结点
	else
	{
		LNode *q=L->next;
		while(q->next!=p)
			q=q->next;
		q->next=NULL;
		free(p);
	}
}
void main()
{
	LinkList L;
	LNode *p;
	int length,data;
	printf("请输入元素个数:");
	scanf("%d",&length);

	L=Create_List_Tail(length);
	printf("打印链表:\n");
	PrintList(L);
	
	printf("请输入要删除的元素值:\n");
	scanf("%d",&data);
	
	p=Find(L,data);
	DeleteNode(L,p);

	printf("删除指定元素后的链表:\n");
	PrintList(L);


}

#include<stdio.h>  
#include<malloc.h>    
  
typedef struct LNode  
{  
    int           data;  
    struct LNode *next;  
}*LinkList;  
  
LinkList Create_List_Tail(int length)  
{//建立链表  
    LNode *L,*s,*r;  //L指向头结点,r指向尾结点,s指向新添加结点  
    L=(LinkList)malloc(sizeof(LNode));  
    L->next =NULL;//头结点L->data不存东西  
    r=L;  
    for(int i=0;i<length;++i)  
    {  
        s=(LinkList)malloc(sizeof(LNode));  
        scanf("%d",&s->data);  
        r->next =s;  
        r =s;  
    }  
    r->next =NULL;  
    return L;  //返回链表  
}  
 
void PrintList(LinkList L)  
{    
    LNode* pNode = L->next ;  
    if(!pNode)  
        printf("空表!\n");  
    while(pNode)  
    {  
        printf("%d  ", pNode->data);  
        pNode = pNode->next;  
    }  
    printf("\n");  
}  

void DeleteDuplication(LinkList &L)  
{  
    if(!L||!L->next)  
        return;   
    LNode* p1 = L;  
    LNode* p2 = L->next ;  
    while(p2 != NULL)  
    {  
        LNode *p3 = p2->next;    
        if(p3 != NULL && p3->data == p2->data)   
        {  
            int value = p2->data;  
            while(p2 != NULL && p2->data == value)  
            {  
                p3 = p2->next;  
                free(p2);
                p2 = p3;  
            }  
  
            p1->next =p3;
            p2 = p3;  
        }  
		else 
        {  
            p1 = p2;  
            p2 = p3 ;  
        }  
    }  
}  
void main()
{
	LinkList L;
	LNode *p;
	int length,data;

	printf("输入链表长度:");
	scanf("%d",&length);

	L=Create_List_Tail(length);
	printf("原始链表为:: \n");  
    PrintList(L);
	
    DeleteDuplication(L);
	printf("删除重复元素后链表为:: \n");  
	PrintList(L); 



}
  





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值