手机上搭建个人网站,成都网站优化常识,wordpress导入docx,公司邮箱签名模板【每日刷题】Day100 #x1f955;个人主页#xff1a;开敲#x1f349; #x1f525;所属专栏#xff1a;每日刷题#x1f34d; #x1f33c;文章目录#x1f33c;
1. 【模板】堆_牛客题霸_牛客网 (nowcoder.com)
2. 【模板】链表_牛客题霸_牛客网 (nowcoder.com)
3…【每日刷题】Day100 个人主页开敲 所属专栏每日刷题 文章目录
1. 【模板】堆_牛客题霸_牛客网 (nowcoder.com)
2. 【模板】链表_牛客题霸_牛客网 (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*21; while(child_size) { if(child1_size_arr[child1]_arr[child]) child; if(_arr[child]_arr[parents]) Swap(_arr[child],_arr[parents]); else break; parents child; child parents*21; } } //向上调整 void Adjustup(int child) { int parents (child-1)/2; while(child0) { if(_arr[child]_arr[parents]) Swap(_arr[child],_arr[parents]); else break; child parents; parents (child-1)/2; } } //入堆 void push(int val) { _arr.push_back(val); Adjustup(_size); _size; } //删除堆顶元素 void pop() { if(_size0) coutemptyendl; else { cout_arr[0]endl; Swap(_arr[0],_arr[_size-1]); _arr.pop_back(); _size--; AdjustDown(0); } } //获取堆顶元素 void top() { if(_size0) coutemptyendl; else cout_arr[0]endl; } private: vectorint _arr; size_t _size; }; int main() { Heap h; int n 0; cinn; while(n--) { int data 0; string s; cins; if(spush) { cindata; h.push(data); } if(stop) h.top(); if(spop) 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) { coutpmove-val ; pmove pmove-next; } } else coutNULL; } //在值为x的节点前插入值为y的节点 void Insert(int x,int y) { list_node* pnext head-next; list_node* prev head; while(pnextpnext-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(pmovepmove-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; cinn; while(n--) { int x,y; string s; cins; if(sinsert) { cinxy; l.Insert(x, y); } if(sdelete) { cinx; 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) couthead-next-valendl; else couterrorendl; } //队尾插入元素 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; coutpcur-valendl; if(pcurpmove) pmove head; head-next pnext; delete pcur; _size--; } else couterrorendl; } private: queue_node* head; queue_node* pmove; size_t _size; }; int main() { Queue q; int n; cinn; while(n--) { int data; string s; cins; if(spush) { cindata; q.Push(data); } if(spop) q.pop(); if(sfront) q.front(); } return 0; }