当前位置:网站首页>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;
};
三、运行结果

边栏推荐
猜你喜欢

Using ngrok on Raspberry Pi (Extra 2)

pytorch相关知识点总结

Loadrunner结合Fiddler实现脚本的录制

日文翻译-在线免费日文翻译软件

字节输入流(InputStream)与字节输出流(OutputStream)

如何在群晖系统中安装cpolar(群晖6.X版)

Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules

全文翻译:EDPB 基于设计和默认的数据保护指南

MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor

史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
随机推荐
NPDP改版前最后一次考试!请注意
Latex example reference
JDBC technology (1) - a simple JDBC test
软件开发之我的一点想法
如何在群晖系统中安装cpolar(群晖6.X版)
基于机器学习之模型树短期负荷预测(Matlab代码实现)
《LC刷题总结》——贪心
电磁辐射安全标准及检测方法
d初化模板构造器
final
JDBC technology (3) - use Druid database connection pool test
Go-11-流程控制
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
Go-8-Gin框架
数据恢复软件EasyRecovery支持恢复所有类型的文件
Go-12-Structure
2020.10.13开发日志
PostMan import certificate add certificate
虹科技术|如何阻止供应链攻击?
[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached