【每日刷题】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;

}

相关推荐
暖阳华笺3 分钟前
【高频考点】回溯(暴力搜索)
数据结构·c++·算法·回溯法
hunterkkk(c++)3 分钟前
学习dijkstra算法(c++)
c++·学习·算法
Chensay.6 分钟前
链表的基本操作(C语言版)
数据结构·链表
lightqjx12 分钟前
【算法】数据结构_单调队列
数据结构·算法·单调队列
c++之路14 分钟前
Linux 下 C++ 开发环境搭建
linux·运维·c++
小四季豆21 分钟前
《数据结构与算法》-顺序表:算法落地的第一个线性结构
c语言·数据结构·算法
8Qi829 分钟前
LeetCode 96:不同的二叉搜索树(Unique Binary Search Trees)—— 题解 ✅
算法·leetcode·职场和发展·动态规划
1892280486129 分钟前
NV041固态MT29F16T08GSLCEM9-QBES:C
人工智能·算法·microsoft·缓存·性能优化
罗超驿43 分钟前
15.LeetCode 30. 串联所有单词的子串(Java):滑动窗口+哈希表详解
算法·leetcode
Marianne Qiqi43 分钟前
非hot100的力扣算法题
数据结构·算法·leetcode