首页 > 其他分享 >九月十四号

九月十四号

时间:2023-09-14 21:56:19浏览次数:27  
标签:Node head curr int next newNode 十四号 九月

#include <iostream>
using namespace std;

struct Node
{
int data;
Node* next;
};

struct List
{
Node* head;
int length;
};

int main()
{
List L;
L.head = nullptr;
L.length = 0;

int n;
cin >> n;

Node *current = nullptr;

for (int i = 0; i < n; i++)
{
int data;
cin >> data;

Node *newNode = new Node;
newNode->data = data;
newNode->next = nullptr;

if (L.head == nullptr)
{
L.head = newNode;
current = newNode;
} else
{
current->next = newNode;
current = newNode;
}

L.length++;
}

int m;
cin >> m;

for (int i = 0; i < m; i++)
{
int op, k;
cin >> op >> k;

if (op == 0)
{
int d;
cin >> d;

if (k < 0 || k > L.length)
{
continue;
}

Node *newNode = new Node;
newNode->data = d;

if (k == 0)
{
newNode->next = L.head;
L.head = newNode;
}
else
{
Node *prev = L.head;
for (int j = 0; j < k - 1; j++)
{
prev = prev->next;
}
newNode->next = prev->next;
prev->next = newNode;
}

L.length++;
}
else if (op == 1)
{
if (k <= 0 || k > L.length)
{
continue;
}

Node *prev = nullptr;
Node *curr = L.head;

for (int j = 0; j < k - 1; j++)
{
prev = curr;
curr = curr->next;
}

if (prev == nullptr)
{
L.head = curr->next;
} else
{
prev->next = curr->next;
}

delete curr;
L.length--;
}
}

// 输出链表
Node *curr = L.head;
while (curr != nullptr)
{
cout << curr->data << " ";
curr = curr->next;
}

// 释放链表内存
curr = L.head;
while (curr != nullptr)
{
Node *temp = curr;
curr = curr->next;
delete temp;
}

return 0;
}

   

标签:Node,head,curr,int,next,newNode,十四号,九月
From: https://www.cnblogs.com/youxiandechilun/p/17703582.html

相关文章

  • 九月十三号
    #include<iostream>usingnamespacestd;longlongf(longlonga){longlongday=0; if(a<=5) { return1; } else { a=a-5; day+=1; while(1) { day+=a/4; if(a%4>0) { returnday+1; } if(a%4==0) { returnday; } } }}intm......
  • 每日打卡 周二 九月十二日
    今天下午没有课,中午睡觉起来先玩了一会儿,然后开始在网上学习javaweb的相关内容,先是看视频解决了severs的问题,有一个eclipse自带的,还可以找其他浏览器,例FireFox...然后根据视频讲解又更换了ecilpse的版本,现在是2022-12的。......
  • 每日打卡 周一 九月十一日
    今天下午的Java语言课堂小测试,我成为最后的几名,主要是定义Stringx,没有初始化,导致代码一直报错,这让我以后对此格外注意。晚上写作业的时候,有关结构体的题不会写,上网查找了相关知识,就算又学习一遍基础的结构体定义即应用。......
  • 九月十一日
    四则运算题目课堂上写的代码#include<iostream>#include<random>usingnamespacestd;intmain(){ random_devicerd;//用于获得随机种子 mt19937gen1(rd());//以随机种子初始化随机数生成器 mt19937gen2(rd()); mt19937gen3(rd()); uniform_int_distribution<int>di......
  • 九月做题记录(距 CSP 还有 1 个月)
    P3959[NOIP2017提高组]宝藏发现\(n\)是很小的,考虑状压。我们先记录下当前的树包含了哪些节点,然后因为转移时肯定会需要经过了多少边,也就是树的深度。我们记录\(\text{expand(i)}\)表示当前选的集合为\(i\)时,扩展一次后的集合。\(\text{road(i,j)}\)表示选的集合为......
  • 九月不努力,秋招徒伤悲
    本文首发自公粽hao「林行学长」,欢迎来撩,免费领取20个求职工具资源包。了解校招、分享校招知识的学长来了!这么快就到了传闻中的金九银十,不知道大家有何感受?也许有同学现在才开始准备投递岗位,这个九月意味着一个全新的开始。然而,如果你在秋招中不努力,可能会面临校招徒伤悲的局面。今......
  • 九月组队学习来了!
     Datawhale学习 开源贡献:Datawhale团队新学期新开始。本月组队学习,包含了机器学习、数据科学、深度学习、数理基础、青少年编程5个模块,共11个学习内容。华北电力大学 BPS大数据与哲学社会科学研究会将联合Datawhale展开线下学习,在华电的同学还可以线下参与。关于开源Datawhale......
  • 九月组队学习!
     Datawhale学习 开源贡献:Datawhale团队本次组队学习为数据挖掘专题学习,旨在理论结合实践,帮助学习者掌握数据挖掘相关知识,提升在实际场景中的数据分析、特征工程、建模调参和模型融合等技能。同时在天池开放了「数据挖掘-金融风控」学习赛,一起在实践中成长。报名方式:在后台回复关......
  • 九月份送书
    阅读文本大概需要2分钟。8月份这个福利持续了下来,而且数量也变多了,九月份的送书活动今天来了。大家都知道我送书的时候都要求出版社把书先寄给我,然后我再签名一个个的寄出去,实话说,我不是明星,我的签名也不值钱,但是我相信大部分读者关注我都是出于一种认可,可能还会有部分读者被我......
  • 就是个复述吧,去年九月份我确实找到工作了,今年5月底离职了,公司技术架构和项目太杂太老
    手写简易spring`packagecom.spring.utils;importcom.spring.BeanDefinition;importcom.spring.inteface.Autowried;importcom.spring.inteface.Component;importcom.spring.inteface.ComponentScan;importcom.spring.inteface.Scope;importcom.spring.service.UserS......