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

来源:互联网 发布:mysql修改参数为自增 编辑:程序博客网 时间:2024/06/16 07:12
方法一(有返回值):#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
原创粉丝点击