查找单链表中的某几个相同的数,并把它们组成新的链表,原链表不变

方法一(有返回值):

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

typedef struct person
{
    int age;
    struct person *next;
}per;

per *tail_list(per *one, int num)
{
    per *temp = (per *)malloc(sizeof(per));

    temp->age = num;
    if(NULL == one)
    {
        return temp;
    }
    per *head = one;
    while(one->next)
    {
        one = one->next;
    }
    one->next = temp;

    return head;
}

void show(per *head)
{
    if(NULL == head)
    {
        return;
    }
    while(head)
    {
        printf("age is %d\n",head->age);
        head = head->next;
    }
}

per *get_put(per *head, int n)
{
    per *new_list = NULL;
    per *curr = head;   //声明两个指针指向原链表的头
    per *past = head;

    if(NULL == head)
    {
        return NULL;
    }
    while(past->next)
    {
        if(n == past->age)  //如果要找的数在第一个
        {
            curr = past;
            head = head->next;
            past = head;
            curr->next = new_list;  //组成新的链表
            new_list = curr;
        }
        else
            if(n == past->next->age)    //如果要找的数在最后一个
            {
                curr = past->next;
                past->next = curr->next;    //取出最后一个数
                curr->next = new_list;
                new_list = curr;
            }
            else
            {
                past = past->next;  //取下一个
            }
    }
    return new_list;
}

int main()
{
    per *head = NULL;

    head = tail_list(head,12);
    head = tail_list(head,12);
    head = tail_list(head,10);
    head = tail_list(head,12);
    head = tail_list(head,10);
    head = tail_list(head,12);
    head = tail_list(head,12);

    printf("============old list==================\n");
    show(head);
    printf("=========after find new_list==========\n");
    per *new_list = get_put(head,12);
    show(new_list);
    return 0;
}

方法二(无返回值):

(1)无头头插单链表:

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

typedef struct person
{
    int age;
    struct person *next;
}per;

void head_list(per **one, int num)
{
    per *temp = (per *)malloc(sizeof(per));

    temp->age = num;
    temp->next = (*one);
    (*one) = temp;
}

void show(per *head)
{
    if(NULL == head)
    {
        return;
    }
    while(head)
    {
        printf("age is %d\n",head->age);
        head = head->next;
    }
}

per *get_put(per *head,int n)
{
    per *new_list = NULL;

    if(NULL == head)
    {
        return NULL;
    }
    while(head)
    {
        if(n == head->age)
        {
            head_list(&new_list,n);
        }
        head = head->next;
    }
    return new_list;
}

int main()
{
    per *head = NULL;

    head_list(&head,10);
    head_list(&head,12);
    head_list(&head,10);
    head_list(&head,12);
    head_list(&head,10);
    head_list(&head,12);
    show(head);
    printf("========get new_list========\n");
    per *new = get_put(head,12);
    show(new);

    return 0;
}
(2)无头尾插单链表:

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

typedef struct person
{
    int age;
    struct person *next;
}per;

void *tail_list(per **one, int num)
{
    per *temp = (per *)malloc(sizeof(per));

    temp->age = num;
    temp->next = NULL;
    while(*one)
    {
        one = &((*one)->next);
    }
    (*one)= temp;
}

void show(per *head)
{
    if(NULL == head)
    {
        return;
    }
    while(head)
    {
        printf("age is %d\n",head->age);
        head = head->next;
    }
}

per *get_put(per *head, int n)
{
    per *new_list = NULL;

    while(head)
    {
        if(n == head->age)
        {
            tail_list(&new_list,n);
        }
        head = head->next;
    }
    return new_list;
}

int main()
{
    per *head = NULL;
    
    tail_list(&head,10);
    tail_list(&head,12);
    tail_list(&head,10);
    tail_list(&head,12);
    tail_list(&head,10);
    tail_list(&head,12);
    show(head);
    puts("=======after new_list===========");
    per *new = get_put(head,12);
    show(new);

    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值