6-28 删除链表中的重复数据 (10 分)程序的功能是:逆序创建一个键值为整数的链表 L,编程实现将其中绝对值重复的键值结点删掉。即对每个键值 K,只有第一个绝对值等于 K 的结点被保留。同时,所

6-28 删除链表中的重复数据 (10 分)

程序的功能是:逆序创建一个键值为整数的链表 L,编程实现将其中绝对值重复的键值结点删掉。即对每个键值 K,只有第一个绝对值等于 K 的结点被保留。同时,所有被删除的结点须按照原来顺序保存在另一个链表中。例如给定 链表L的各键值为 21→-15→-15→-7→15,则输出去重后的链表: 21→-15→-7,以及被删除的结点链表: -15→15。

函数接口定义:

 

struct ListNode *Createlist(int n); struct ListNode *Del_absrepeat( struct ListNode **head ); void Printlist(struct ListNode *head);

其中 n 和 head 都是用户传入的参数。 n 的值不超过int的范围,表示创建的链表结点数; head 是链表的头指针。
输入时在第一行给出 L 的结点总数n。随后输入n个整数值,链表按输入数据的逆序建立。

裁判测试程序样例:

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

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *Createlist(int n);
struct ListNode *Del_absrepeat( struct ListNode **head );
void Printlist(struct ListNode *head);
int main()
{
    struct ListNode *head = NULL,*head2=NULL;
    int n;
    scanf("%d",&n);
    head = Createlist(n);
    printf("原始链表:");
    Printlist(head);
        
   head2=Del_absrepeat( &head );
   printf("删除重复结点的链表:");
   Printlist(head);
    
   printf("被删除的结点组成的链表:");
   Printlist(head2);
   return 0;
}

void Printlist(struct ListNode *head)
{
    struct ListNode *p;
    for ( p = head; p != NULL; p = p->next )
            printf("%d ", p->data);
        printf("\n");
}

/* 请在这里填写答案 */

输入样例:

5
21 -15 -15 -7 15

输出样例:

原始链表:15 -7 -15 -15 21 
删除重复结点的链表:15 -7 21 
被删除的结点组成的链表:-15 -15 

输入样例:

7
15 -15 -15 -15 15 -15 15 

输出样例:

原始链表:15 -15 15 -15 -15 -15 15 
删除重复结点的链表:15 
被删除的结点组成的链表:-15 15 -15 -15 -15 15 
struct ListNode *Createlist(int n)
{
    if(n==0)
        return NULL;
    else
    {
        struct ListNode *head=NULL,*p2=NULL,*p1,*old;
    int i,a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);        
    }
        for(i=0;i<n;i++)
    {
       
        p1=(struct ListNode *)malloc(sizeof(struct ListNode ));
        p1->data=a[i];
        if(head==NULL)
            head=p1;
        else
        {
            p2->next=p1;
        }
        p2=p1;
    }
    if(head)
        p2->next=NULL;
    old=head;
    //t=p1;news=p2;
    p2=NULL;
    while(old)
    {
        p1=old;
        old=old->next;
        p1->next=p2;
        
        p2=p1;
    }
    return p2;
    }
    
}
struct ListNode *Del_absrepeat( struct ListNode **head )
{
    struct ListNode *p1=*head,*head2=NULL,*p2,*p3,*p4;
    int i,j,f;
        *head=NULL;
    i=0;
    while(p1)
    {
        i++;
        if(i!=1)
        {
            p2=*head;
            f=0;
            for(j=1;j<i;j++)
            {
                if((p1->data==p2->data)||(p1->data+p2->data==0))
                {
                    if(head2==NULL)
                        head2=p1;
                    else
                    {
                        p3->next=p1;
                    }
                    p3=p1;
                    f=1;
                    break;
                }
                p2=p2->next;
            }
            if(f==0)
            {
                p4->next=p1;
                p4=p1;
            }
        }
        else
        {
            *head=p1;
            p4=p1;
        }
        p1=p1->next;
    }
    if(head2)
        p3->next=NULL;
    if(head)        
        p4->next=NULL;
        return head2;
}

struct ListNode *Createlist(int n)
{
    struct ListNode *p1,*p2,*head=NULL,*t;
    int i=0;
    while(i<n)
    {
        p1=(struct ListNode*)malloc(sizeof(struct ListNode));
        scanf("%d",&p1->data);
        if(head==NULL)
        {
            head=p1;
        }
        else
            p2->next=p1;
        p2=p1;
        i++;
    }
    if(head)
        p2->next=NULL;
    p1=head;
    p2=NULL;
    while(p1)
    {
        t=p1;
        p1=p1->next;
        t->next=p2;
        p2=t;
    }
    return p2;
}
struct ListNode *Del_absrepeat( struct ListNode **head )
{
    int i=0,j,f;
    struct ListNode*head2=NULL,*p1=*head,*p2,*p3,*p4;
    while(p1)
    {
        if(i==0)
        {
            *head=p1;
            p2=p1;
            i++;
        }
        else
        {
            f=1;
            p3=*head;
            for(j=0;j<i;j++)
            {
                if((p1->data==p3->data)||(p1->data+p3->data==0))
                {
                    f=0;
                    break;
                }
                p3=p3->next;
            }
            if(f==1)
            {
                p2=p1;
                i++;
            }
            else
            {
                p2->next=p1->next;     
                if(head2==NULL) 
                {
                    head2=p1;
                }
                else
                {
                    p4->next=p1;
                }
                p4=p1;
            }
        }
        p1=p1->next;
    }
    if(head2)
        p4->next=NULL;
    if(*head)
        p2->next=NULL;
    return head2;
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值