当前位置:网站首页>Force buckled brush problem record 7.1 -- -- -- -- -- 707. The design list
Force buckled brush problem record 7.1 -- -- -- -- -- 707. The design list
2022-08-09 02:05:00 【@baigui】
一、题目

二、代码
/** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList* obj = new MyLinkedList(); * int param_1 = obj->get(index); * obj->addAtHead(val); * obj->addAtTail(val); * obj->addAtIndex(index,val); * obj->deleteAtIndex(index); */
class MyLinkedList {
public:
struct LinkedNode //The first step in building a linked list function
{
//Regular linked list method definitions
int val; //构造函数定义
LinkedNode* next;
LinkedNode(int val):val(val),next(nullptr){
}
};
MyLinkedList() //自己定义构造函数
{
_dummyhead= new LinkedNode(0); //A virtual head node is defined Always point to speculation
_size =0;
}
int get(int index)
{
if(index>=0&&index<=_size-1)
{
//std::cout<<"in get _size "<<_size<<" "<< std::endl;
LinkedNode *process_node;
process_node=_dummyhead; //Construct a processing node
int count_index=0;
while(count_index!=index)
{
process_node=process_node->next;
count_index=count_index+1;
}
// LinkedNode *show_node;
// show_node=_dummyhead; //Construct a processing node
// while(show_node->next!=nullptr)
// {
// std::cout<<" "<<show_node->val<<" ";
// show_node=show_node->next;
// }
// std::cout<<" "<<show_node->val<<" "; //打印最后一个
// std::cout<<" "<< std::endl;
// std::cout<<"_size "<<_size<<" "<< std::endl;
return process_node->next->val;
}
else
{
return -1;
}
}
void addAtHead(int val) //Append an element to the head of the linked list
{
LinkedNode *temp;
temp=new LinkedNode(val); //构造该节点
temp->next=_dummyhead->next;
_dummyhead->next=temp;
_size+=1;
}
void addAtTail(int val) //在链表末尾追加元素
{
// LinkedNode *show_node;
// show_node=_dummyhead; //构造显示节点
// while(show_node->next!=nullptr)
// {
// std::cout<<" "<<show_node->val<<" ";
// show_node=show_node->next;
// }
// std::cout<<" "<<show_node->val<<" "; //打印最后一个
// std::cout<<" "<< std::endl;
// std::cout<<"in tail _size "<<_size<<" "<< std::endl;
LinkedNode *temp;
temp=new LinkedNode(val); //构造该节点
LinkedNode *process_node;
process_node=_dummyhead; //Construct a processing node
while(process_node->next!=nullptr) //先自增 increase all the way to the end point Then jump out at this timewhile
{
process_node=process_node->next;
}
process_node->next=temp; //Set its tail node to betemp 而temp本身指向nullptr 不用操作
_size+=1; //大小加1
// show_node=_dummyhead; //构造显示节点
// while(show_node->next!=nullptr)
// {
// std::cout<<" "<<show_node->val<<" ";
// show_node=show_node->next;
// }
// std::cout<<" "<<show_node->val<<" "; //打印最后一个
// std::cout<<" "<< std::endl;
// std::cout<<"out tail _size "<<_size<<" "<< std::endl;
}
void addAtIndex(int index, int val) //注意 操作之后_size会变大 Operations should be made mutually exclusive
{
int one_operation=0;
std::cout<<"in addAtIndex _size "<<_size<<" "<< std::endl;
LinkedNode *process_node;
process_node=_dummyhead; //Construct a processing node
LinkedNode *temp;
temp=new LinkedNode(val); //构造该节点
if(index<0&&one_operation==0) //如果索引值小于0
{
addAtHead(val);
one_operation=1;
}
if(index==_size&&one_operation==0) //如果index等于链表长度 just add at the end
{
addAtTail(val);
one_operation=1;
}
if(index>_size&&one_operation==0) //如果index大于链表长度 就不插入
{
//no operation
one_operation=1;
}
std::cout<<"in addAtIndex _size "<<_size<<" "<< std::endl;
if(index>=0&&index<_size&&one_operation==0)
{
if(index==0) addAtHead(val);
else
{
int count_index=0;
while(count_index!=index) //如果index等于1 Just insert before the first one 也就是第0Insert after 找到第0个就可以了
{
process_node=process_node->next;
count_index=count_index+1;
}
temp->next=process_node->next;
process_node->next=temp;
_size+=1; //大小加1
}
one_operation=1;
}
std::cout<<"out addAtIndex _size "<<_size<<" "<< std::endl;
}
void deleteAtIndex(int index)
{
if(index<=_size-1&&index>=0)
{
LinkedNode *process_node;
process_node=_dummyhead; //Construct a processing node
LinkedNode *temp;
temp=new LinkedNode(0); //构造该节点
int count_index=0;
while(count_index!=index)
{
process_node=process_node->next;
count_index=count_index+1;
}
temp=process_node->next;
process_node->next=process_node->next->next;
delete temp;
_size-=1;
}
}
//Don't forget to define private variables
private:
int _size;
LinkedNode* _dummyhead;
};
三、运行结果

边栏推荐
- Latex example reference
- 方法参数
- 力扣刷题记录4.1-----209. 长度最小的子数组
- [Signal denoising] Based on Sage-Husa adaptive Kalman filter to realize the suppression of ocean wave magnetic field noise and the generation of ocean wave magnetic field noise with matlab code
- js实现数组去重的方式(7种)
- eladmin容器部署超详细过程
- JDBC技术(一)——一个简单的JDBC测试
- LeetCode每日两题01:有序数组的平方 (均1200道)方法:双指针
- RF调试过程中现象一
- SEMRush如何寻找关键词用于投放广告
猜你喜欢

JDBC technology (3) - use Druid database connection pool test

PostMan import certificate add certificate

史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!

LeetCode每日两题02:轮转数组 (均1200道)
![[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached](/img/c3/77fba03e3615485fbbed43d1bc22be.png)
[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached

mysql 5.7 入坑

力扣刷题记录8.1-----206. 反转链表

typescript89-展示任务列表功能

企业里Foxmail邮箱问题解决方法汇总

德语翻译-德语在线批量翻译软件
随机推荐
Analysis of when AuthenticationSuccessHandler is called after UsernameAuthenticationFilter is authorized successfully
2020.10.13 Development log
项目经理VS产品经理,二者到底有何不同?
MT4/MQL4 entry to proficient foreign exchange EA tutorial Lesson 1 Getting to know MetaEditor
Go-7-RESTful API的设计
Go-12-结构体
Cmake 报错 Could not find a package configuration file provided by “OpenCV“
TP测试查询数据库字段为null或空的字段
gstreamer 记录
2020.12.4日志
LeetCode每日两题01:二分查找 (均1200道)
ONNX是什么?怎么用?[简明解读版]
ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...
LeetCode每日一题:搜索插入位置 (均1200道)方法:二分查找
面试秘籍 | 软件测试必备的mysql数据库技术
德语翻译-德语在线批量翻译软件
gstreamer 记录
数据库设计的总结
Significance Test--Study Notes
How SEMRush finds keywords for advertising