查找单链表中的某几个相同的数,并把它们组成新的链表,原链表不变
来源:互联网 发布: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
- 查找单链表中的某几个相同的数,并把它们组成新的链表,原链表不变
- 面试题:有1、2、3、4四个数字,能组成多少个互不相同且一个数字中无重复数字的三位数,并把它们都输出。
- 获取href超链接中的参数并把它们展示到页面上的input输入框中
- 58.比较两个字符串并把ASCII值打或相等的字符组成一个新字符串
- 它们有相同的限制
- 提取数组中相同类别数据,组成新的数组
- 任意输入几个数,计算它们的平均分
- 删除数组中相同的数,统计两个数组中重复数个数并把不同数找出来
- 编程找出不同的四位平方数对,它们由相同的数字构成
- 取出字符串中的连续数字并把数字乘以10再返回新的字符串--js中replace的回调函数详解
- 删除ary数组中的和keyword中相同的元素,其余的不变并输出
- 一个数组中某个键的值相同则把相同的数组组成一个新数组
- 将多个list中相同位置的元素取出组成新的list
- Json中相同或者重复记录的值相加组成新的Json
- PHP让数组中有相同值的组成新的数组
- 删除数据库中表的某些相同的某些记录 OR 查找数据库的表中的相同记录
- 删除数据库中表的某些相同的某些记录 OR 查找数据库的表中的相同记录
- 在数组中查找两个数,使得它们的和正好是输入的那个数字
- DRY原则
- 用boost::lexical_cast用法解析
- python操作表格、txt文件、字典进行参数化(数据驱动)
- 公众号SEO:如何做好公众号和文章的搜索优化?
- vmware workstation 12 pro 安装Mac OS 10.12.4系统
- 查找单链表中的某几个相同的数,并把它们组成新的链表,原链表不变
- leetcode 20. Valid Parentheses
- Java线程池
- 9.static关键字
- 设计模式(11)--Flyweight(享元模式)--结构型
- UNIX环境编程学习笔记(2)——文件I/O之不带缓冲的 I/O
- 自定义函数
- html字符转义
- 写一个Java应用程序,从键盘输入一个整数,然后输出它的平方值立方值