38-12

发布时间 2023-10-06 20:11:27作者: 正确选项

在一个递增有序的单链表中,有数值相同的元素存在。去掉数值相同的元素,是表中不再有重复的元素。

双指针解决链表的百分之60的问题,三指针解决百分之80的问题,四指针解决百分之99的问题

脑子不够,指针来凑

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int data;
    struct node *next;
}LNode,*LinkList;

void TailCreate(LinkList &L)
{
    L=(LinkList)malloc(sizeof(LNode));
    L->next=NULL;
    LNode *p,*r=L;
    int x;
    scanf("%d",&x);
    while(x!=999)
    {
        p=(LNode*)malloc(sizeof(LNode));
        p->data=x;
        p->next=NULL;
        r->next=p;
        r=p;
        scanf("%d",&x);
    }
}

void displayList(LinkList L)
{
    LNode *p=L->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
}

void Delete(LinkList &L)
{
    LNode *p=L->next,*r=p->next,*s;
    while(r)
    {
        if(p->data==r->data)
        {
            p->next=r->next;
            free(r);
            r=p->next;
        }
        else
        {
            p=r;
            r=r->next;    
        }    
    }    
} 

int main()
{
    LinkList L;
    TailCreate(L);
    displayList(L);
    printf("\n");
    Delete(L);
    displayList(L);
    printf("\n");
    return 0;
}