当前位置:网站首页>Simple knapsack, queue and stack with deque
Simple knapsack, queue and stack with deque
2022-07-05 05:35:00 【Raise items】
List of articles
Algorithm
When solving a problem , it is to be noted that : understand and Definition problem , Control the problem Complexity , Put it decompose For sub problems that are easier to solve .
The steps to solve the problem Called algorithm .
In the field of Computer Science , We use it Algorithm This word describes a kind of Co., LTD. 、 determine 、 It works And suitable for A computer program To solve the problem .
Computing and storage resources of computers are limited , So it can only solve some mathematical theoretical problems .
To define an algorithm , We can use Natural language Describe the process of solving a problem , Or use a Program Design language to realize this process .
To calculate two nonnegative integers p and q Of greatest common divisor For example .
Natural language description
if q yes 0, Then the greatest common divisor is p;
otherwise , take p Divide q Get the remainder r,p and q The greatest common divisor of is q and r Maximum common divisor of .
C++ Realization
int gcd(int p, int q)
{
if (q == 0) {
return p;
}
return gcd(q, p % q);
}
Compared to natural language , The program is more about the algorithm accurate and Completely Description of .
meanwhile , data structure It is the by-product or result of the algorithm , Therefore, to understand the algorithm, we must learn the data structure .
data structure
Here are three data structures : knapsack 、 queue and Stack .
Generic and iteration (template<typename Item>Iterable<Item>) It can ensure that three data structures can be stored Any type The elements of , And the element is in a form that can be Traverse Preserved in the form of .
In this case , Use STL Medium deque Storage elements . knapsack 、 Both queues and stacks are right deque Encapsulation .
knapsack
Knapsack is a data structure that collects elements , Deleting elements... Is not supported .bag.h
#include <deque>
template <typename Item>
class Bag {
public:
Bag(); // Create an empty backpack
void Add(const Item &item); // Additive elements
bool IsEmpty(); // Whether the backpack is empty
int Size(); // The number of elements in the backpack
private:
std::deque<Item> dq_;
};
template <typename Item>
Bag<Item>::Bag() {
}
template<typename Item>
void Bag<Item>::Add(const Item &item)
{
dq_.push_back(item);
}
template <typename Item>
bool Bag<Item>::IsEmpty()
{
return dq_.empty();
}
template <typename Item>
int Bag<Item>::Size()
{
return dq_.size();
}
queue
Queue is based on fifo The set type of the strategy .queue.h
#include <deque>
template <typename Item>
class Queue {
public:
Queue(); // Create an empty queue
void EnQueue(const Item &item); // Additive elements
Item Dequeue(); // Delete the first added element
bool IsEmpty(); // Whether the queue is empty
int Size(); // The number of elements in the queue
private:
std::deque<Item> dq_;
};
template <typename Item>
Queue<Item>::Queue() {
}
template <typename Item>
void Queue<Item>::EnQueue(const Item &item)
{
dq_.push_back(item);
}
template <typename Item>
Item Queue<Item>::Dequeue()
{
auto item = dq_.front();
dq_.pop_front();
return item;
}
template <typename Item>
bool Queue<Item>::IsEmpty()
{
return dq_.empty();
}
template <typename Item>
int Queue<Item>::Size()
{
return dq_.size();
}
Stack
The stack is based on Last in, first out The set type of the strategy .stack.h
#include <deque>
template <typename Item>
class Stack {
public:
Stack(); // Create an empty stack
void Push(const Item &item); // Additive elements
Item Pop(); // Delete recently added elements
bool IsEmpty(); // Is the stack empty
int Size(); // The number of elements in the stack
private:
std::deque<Item> dq_;
};
template <typename Item>
Stack<Item>::Stack() {
}
template <typename Item>
void Stack<Item>::Push(const Item &item)
{
dq_.push_back(item);
}
template <typename Item>
Item Stack<Item>::Pop()
{
auto item = dq_.back();
dq_.pop_back();
return item;
}
template <typename Item>
bool Stack<Item>::IsEmpty()
{
return dq_.empty();
}
template <typename Item>
int Stack<Item>::Size()
{
return dq_.size();
}
A classic application of stack is computation Arithmetic expressions Value . for example :(1 + ((2 + 3) * (4 * 5)))
边栏推荐
- Introduction to memory layout of FVP and Juno platforms
- The number of enclaves
- Acwing 4300. Two operations
- 剑指 Offer 58 - II. 左旋转字符串
- [jailhouse article] performance measurements for hypervisors on embedded ARM processors
- ALU逻辑运算单元
- [binary search] 69 Square root of X
- Solution to the palindrome string (Luogu p5041 haoi2009)
- Sword finger offer 53 - ii Missing numbers from 0 to n-1
- YOLOv5添加注意力機制
猜你喜欢

YOLOv5添加注意力機制

Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade

全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析

CF1634 F. Fibonacci Additions

Educational Codeforces Round 116 (Rated for Div. 2) E. Arena

个人开发的渗透测试工具Satania v1.2更新

lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

【Jailhouse 文章】Look Mum, no VM Exits

Sword finger offer 05 Replace spaces

Reader writer model
随机推荐
Demonstration of using Solon auth authentication framework (simpler authentication framework)
How many checks does kubedm series-01-preflight have
Zzulioj 1673: b: clever characters???
Little known skills of Task Manager
[binary search] 69 Square root of X
Common optimization methods
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
[to be continued] [depth first search] 547 Number of provinces
kubeadm系列-01-preflight究竟有多少check
ssh免密登录设置及使用脚本进行ssh登录并执行指令
SAP method of modifying system table data
Introduction to tools in TF-A
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
object serialization
Haut OJ 1401: praise energy
CF1634E Fair Share
F - Two Exam(AtCoder Beginner Contest 238)
Gbase database helps the development of digital finance in the Bay Area
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
Mysql database (I)