create account

Datenstrukturen in c++: Queue by ozelot47

View this thread on: hive.blogpeakd.comecency.com
· @ozelot47 ·
$0.70
Datenstrukturen in c++: Queue
Eine warteschlange (<b>Queue</b>) ist die Umkehrung eines Stacks. Es folgt dem Prinzip First-In-First-Out. Elemente die zuerst eingefügt werden, werden auch zuerst verarbeitet. Um eine Queue besser zu demonstrieren habe ich die Tasks, um eine Funktionalität erweitert. Nun können Tasks eine Priorität von 1,2 oder 3 haben. 3 bedeutet höchste Priorität und 1 niedrige Priorität. Nachdem die Priorität einer Task überprüft wird, wird diese entweder verarbeitet oder wieder an die Queue angehangen. In diesem Fall wird die Priorität von dem angehangenen Task um 1 erhöht. Mit diesem Verhalten kann man eine Prioritätswarteschlange (<b>Priority Queue</b>) simulieren.

Auch hier, wer mehr zur Theorie erfahren möchte: https://peakd.com/hive-196387/@ozelot47/datenstrukturen-queue-warteschlange


![cpp_queue.png](https://files.peakd.com/file/peakd-hive/ozelot47/23tHbqzhihQKuoPbvkkEZdJM278wLptBiEaWdL1GV6RBeSujBc37QpNFi7qR3gvcaMEDA.png)


```
#include <iostream>
#include <queue>

class Task {
public:
    int id;
    explicit Task(int id) : id(id) { }
    void doIt(){
        std::cout << "Task-ID: " << id << "\n";
    }
    void increasePriority(){
        id++;
    }
};

void queue(){
    /* the queue is the opposite from a stack. It follows
     * the FIFO princip (First In First Out) */
    std::queue<Task> container;

    /* insert tasks with different prioroties */
    container.push(Task(1));
    container.push(Task(1));
    container.push(Task(3));
    container.push(Task(2));
    container.push(Task(1));

    /* consume all urgent tasks (highest number) first. Increase priority level from
     * waiting tasks */
    while(!container.empty()){
        Task &task = container.front();
        /* very urgent */
        if ( task.id == 3) {
            task.doIt();
            container.pop();
        } else {
            task.increasePriority();
            container.pop();
            container.push(task);
        }
    }
}

int main(){
    queue();
    return 0;
}
```
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorozelot47
permlinkdatenstrukturen-in-c-queue
categoryhive-196387
json_metadata{"app":"peakd/2021.04.2","format":"markdown","tags":["hivestem","science","engineering","stem","deutsch","steemstem","palnet"],"users":["ozelot47"],"links":["https://peakd.com/hive-196387/@ozelot47/datenstrukturen-queue-warteschlange"],"image":["https://files.peakd.com/file/peakd-hive/ozelot47/23tHbqzhihQKuoPbvkkEZdJM278wLptBiEaWdL1GV6RBeSujBc37QpNFi7qR3gvcaMEDA.png"]}
created2021-04-20 11:00:57
last_update2021-04-20 11:00:57
depth0
children1
last_payout2021-04-27 11:00:57
cashout_time1969-12-31 23:59:59
total_payout_value0.406 HBD
curator_payout_value0.291 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,990
author_reputation144,256,113,009,208
root_title"Datenstrukturen in c++: Queue"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id103,162,212
net_rshares1,593,299,270,136
author_curate_reward""
vote details (32)
@zottone444 ·
Wow è il primo programma che trovo su Hive 
👍  , , , , ,
properties (23)
authorzottone444
permlinkre-ozelot47-2021420t1324245z
categoryhive-196387
json_metadata{"tags":["hivestem","science","engineering","stem","deutsch","steemstem","palnet"],"app":"ecency/3.0.17-mobile","format":"markdown+html"}
created2021-04-20 11:02:12
last_update2021-04-20 11:02:12
depth1
children0
last_payout2021-04-27 11:02:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length43
author_reputation138,540,134,548,046
root_title"Datenstrukturen in c++: Queue"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id103,162,221
net_rshares25,775,044,909
author_curate_reward""
vote details (6)