当前位置:网站首页>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;
};
三、运行结果
边栏推荐
- 增额终身寿险哪家最好呢?真的安全吗?
- final
- JDBC technology (1) - a simple JDBC test
- ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...
- Use of torchversion.transforms
- MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor
- 33. 分别谈谈联合索引生效和失效的条件
- KQL和Lucene的区别
- [深入研究4G/5G/6G专题-55]: L3信令控制-4-软件功能与流程的切分-CU网元的信令
- MT4/MQL4入门到精通EA课程第二课-常用的功能函数
猜你喜欢
MT4/MQL4 entry to proficient foreign exchange EA tutorial Lesson 1 Getting to know MetaEditor
LeetCode每日两题01:有序数组的平方 (均1200道)方法:双指针
HCIP-R&S By Wakin自用笔记(2)OSPF之OSPF回顾、虚连接
Observer pattern
企业里Foxmail邮箱问题解决方法汇总
在树莓派上使用cpolar(番外篇2)
电磁辐射安全标准及检测方法
力扣刷题记录9.1-----24. 两两交换链表中的节点
Etcd realize large-scale application service management of actual combat
JDBC technology (1) - a simple JDBC test
随机推荐
2022眼康品牌加盟展,北京视力保健展,中国眼科医学技术峰会
Codeforces Round #809 (Div. 2)A~D1
class path resource [bean.xml] cannot be opened because it does not 错误解决方案
Go-10-模块与包
D. Tournament Countdown
Wireshark packet capture tool
字节输入流(InputStream)与字节输出流(OutputStream)
webrtc 编译
Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules
谷歌翻译软件-免费谷歌翻译
The Best Open Source Web Application Firewall to Protect Your Web Applications
Likou Brush Question Record 8.1-----206. Reverse linked list
力扣刷题记录2.1-----27. 移除元素
spdlog日志库的封装使用
Proe/Creo智能硬件产品结构设计要点「干货分享」
[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
How js implements array deduplication (7 kinds)
Go-11-流程控制
力扣刷题记录1.5-----367. 有效的完全平方数
OpenMLDB + Jupyter Notebook:快速搭建机器学习应用