美团编程
来源:互联网 发布:js面向对象教程 编辑:程序博客网 时间:2024/05/16 16:58
实现一个动态数组类,功能包括在位置 N 增加一个数据,在位置 N 删除一个数据,获取第 N 大的数据,获取第 N 小的数据,获取数组的第 N 个数据,其中, N 取任意值。
#include <iostream>#include <stdio.h>#include <algorithm>struct my_list{ my_list *next; int value; my_list() { next = NULL; value = 0; }};class dynamic{ my_list *head; int arr[10000]; void add_a_number(int n,int value)//在位置 n 添加一个数据,从 0 开始 { int cal = 0;//计数 my_list *it = new my_list; my_list *des = new my_list; des->value = value; my_list *_next = new my_list; for(it=head;cal<n;cal++)//it为代插入位置的上一个节点 it = it->next; _next = it->next; if(_next == NULL)//插入到末尾 it->next = des; else//在中间插入 { it->next = des; des->next = _next; } } void delete_a_number(int n)//在位置 n 删除一个数据 { int cal = 0;//计数 my_list *it = new my_list; my_list *des = new my_list; for(it=head;cal<n;cal++)//it为代插入位置的上一个节点 it = it->next; des = it->next;//待删除位置 if(des == NULL) printf("error:wrong index\n"); else if(des->next == NULL) { delete(des); it->next = NULL; } else { it->next = des->next; delete(des); } } int get_nth_min(int n)//获取第 n 小的数据,从 0 开始 { int cal = 0;//计数 my_list *it = new my_list; it = head; while(it->next)//存放到数组里 { arr[cal++] = it->next->value; it = it->next; } std::sort(arr,arr+n); return arr[n-1]; } int get_nth_number(int n)//获取第 n 个数据,从 0 开始 { int cal = 0;//计数 my_list *it = new my_list; it = head; while(cal<n)//遍历链表 { cal++; it = it->next; } return it->next->value; }};
阅读全文
0 0
- 美团编程
- 美团编程题
- 美团编程题
- 美团点评编程题
- 美团2017校园招聘编程题
- 美团笔试编程题-9.11
- 美团笔试之编程题
- 美团点评编程大赛题目疑问
- 美团编程大赛-优惠券 (set)
- CodeM美团点评编程大赛资格赛
- 美团Codem编程比赛资格赛:数码
- 美团CodeM编程大赛资格赛题解
- 美团上机编程之“”凑钱“”
- 《美团点评编程题》整数加法
- 美团点评编程笔试题
- 美团点评秋招编程题
- 美团2018内推编程题
- 美团2017秋招编程题
- docker常用命令
- 【转载】Azkaban源码分析
- 一步一步学多线程-synchronized
- Axure RP 8 教程
- canvas实现简单的画图功能
- 美团编程
- docker-1
- 深入浅出TensorFlow(二):TensorFlow解决MNIST问题入门
- web前端知识总结2
- 测试一个大数是否是素数
- RecyclerView第三方开源
- 计算分数加减表达式的值
- cookie 和session 的区别详解
- next数组求解详解