【每日刷题】Day100

【每日刷题】Day100

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

[1. 【模板】堆_牛客题霸_牛客网 (nowcoder.com)](#1. 【模板】堆_牛客题霸_牛客网 (nowcoder.com))

[2. 【模板】链表_牛客题霸_牛客网 (nowcoder.com)](#2. 【模板】链表_牛客题霸_牛客网 (nowcoder.com))

[3. 【模板】队列_牛客题霸_牛客网 (nowcoder.com)](#3. 【模板】队列_牛客题霸_牛客网 (nowcoder.com))

1. 【模板】堆_牛客题霸_牛客网 (nowcoder.com)

//C++堆(大堆)的实现。

#include <iostream>

#include <vector>

using namespace std;

class Heap

{

public:

Heap()

:_size(0)

{}

void Swap(int& x,int& y)

{

int tmp = x;

x = y;

y = tmp;

}

//向下调整

void AdjustDown(int parents)

{

int child = parents*2+1;

while(child<_size)

{

if(child+1<_size&&_arrchild+1>_arrchild)

child++;

if(_arrchild>_arrparents)

Swap(_arrchild,_arrparents);

else

break;

parents = child;

child = parents*2+1;

}

}

//向上调整

void Adjustup(int child)

{

int parents = (child-1)/2;

while(child>0)

{

if(_arrchild>_arrparents)

Swap(_arrchild,_arrparents);

else

break;

child = parents;

parents = (child-1)/2;

}

}

//入堆

void push(int val)

{

_arr.push_back(val);

Adjustup(_size);

_size++;

}

//删除堆顶元素

void pop()

{

if(_size==0)

cout<<"empty"<<endl;

else

{

cout<<_arr0<<endl;

Swap(_arr0,_arr_size-1);

_arr.pop_back();

_size--;

AdjustDown(0);

}

}

//获取堆顶元素

void top()

{

if(_size==0)

cout<<"empty"<<endl;

else

cout<<_arr0<<endl;

}

private:

vector<int> _arr;

size_t _size;

};

int main()

{

Heap h;

int n = 0;

cin>>n;

while(n--)

{

int data = 0;

string s;

cin>>s;

if(s=="push")

{

cin>>data;

h.push(data);

}

if(s=="top")

h.top();

if(s=="pop")

h.pop();

}

return 0;

}

2. 【模板】链表_牛客题霸_牛客网 (nowcoder.com)

//C++链表模拟实现。

#include <iostream>

#include <list>

using namespace std;

class List

{

class list_node

{

public:

list_node(int y = -2147483648)

:next(nullptr)

,val(y)

{}

int val;

list_node* next;

};

public:

List()

:head(new list_node)

,_size(0)

{}

//打印链表

void printf()

{

if(_size)

{

list_node* pmove = head->next;

while(pmove)

{

cout<<pmove->val<<' ';

pmove = pmove->next;

}

}

else

cout<<"NULL";

}

//在值为x的节点前插入值为y的节点

void Insert(int x,int y)

{

list_node* pnext = head->next;

list_node* prev = head;

while(pnext&&pnext->val!=x)

{

prev = prev->next;

pnext = pnext->next;

}

list_node* newnode = new list_node(y);

prev->next = newnode;

newnode->next = pnext;

_size++;

}

//删除值为x的节点

void Delete(int x)

{

list_node* pmove = head->next;

list_node* prev = head;

while(pmove&&pmove->val!=x)

{

prev = prev->next;

pmove = pmove->next;

}

if(pmove)

{

prev->next = pmove->next;

_size--;

delete pmove;

}

}

private:

list_node* head;

size_t _size;

};

int main()

{

List l;

int n = 0;

cin>>n;

while(n--)

{

int x,y;

string s;

cin>>s;

if(s=="insert")

{

cin>>x>>y;

l.Insert(x, y);

}

if(s=="delete")

{

cin>>x;

l.Delete(x);

}

}

l.printf();

return 0;

}

3. 【模板】队列_牛客题霸_牛客网 (nowcoder.com)

//C++队列的实现。

#include <iostream>

#include <list>

#include <string>

using namespace std;

class Queue

{

class queue_node

{

public:

queue_node(int _val = -2147483648)

:next(nullptr)

,val(_val)

{}

int val;

queue_node* next;

};

public:

Queue()

:head(new queue_node)

,pmove(head)

,_size(0)

{}

//获取队头元素

void front()

{

if(_size)

cout<<head->next->val<<endl;

else

cout<<"error"<<endl;

}

//队尾插入元素

void Push(int data)

{

queue_node* newnode = new queue_node(data);

pmove->next = newnode;

pmove = pmove->next;

_size++;

}

//队头删除元素

void pop()

{

if(_size)

{

queue_node* pnext = head->next->next;

queue_node* pcur = head->next;

cout<<pcur->val<<endl;

if(pcur==pmove)

pmove = head;

head->next = pnext;

delete pcur;

_size--;

}

else

cout<<"error"<<endl;

}

private:

queue_node* head;

queue_node* pmove;

size_t _size;

};

int main()

{

Queue q;

int n;

cin>>n;

while(n--)

{

int data;

string s;

cin>>s;

if(s=="push")

{

cin>>data;

q.Push(data);

}

if(s=="pop")

q.pop();

if(s=="front")

q.front();

}

return 0;

}

相关推荐
oyguyteggytrrwwwrt1 小时前
自制交叉线路识别算法
算法
手写码匠1 小时前
华为云Flexus+DeepSeek征文|Dify 多智能体协同编排实战:R1 规划 + V3 执行,构建企业 Agent 团队
人工智能·深度学习·算法·aigc
晓天衡宇•评测社区2 小时前
FSR-Bench 前沿科学推理榜单发布:GPT-5.5 居首,“会推理”未必“能答对”
算法
程序喵大人3 小时前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法
hanlin034 小时前
动态规划专练:力扣第121、122题
笔记·算法·leetcode
To_OC4 小时前
LC 74 搜索二维矩阵:换皮的二分查找,我居然一开始没看出来
javascript·算法·leetcode
文心快码BaiduComate4 小时前
文心快码荣获“中国优秀软件产品”,实力再获国家级认可
算法·代码规范
玖玥拾4 小时前
LeetCode 189 轮转数组
算法·leetcode
ZhangShao06074 小时前
题解:CF2249B
c++·算法
小玮看世界4 小时前
[Python]线段树与二分法
数据结构·算法