2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
29
Task/Priority-queue/Batch-File/priority-queue.bat
Normal file
29
Task/Priority-queue/Batch-File/priority-queue.bat
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
call :push 10 "item ten"
|
||||
call :push 2 "item two"
|
||||
call :push 100 "item one hundred"
|
||||
call :push 5 "item five"
|
||||
|
||||
call :pop & echo !order! !item!
|
||||
call :pop & echo !order! !item!
|
||||
call :pop & echo !order! !item!
|
||||
call :pop & echo !order! !item!
|
||||
call :pop & echo !order! !item!
|
||||
|
||||
goto:eof
|
||||
|
||||
|
||||
:push
|
||||
set temp=000%1
|
||||
set queu%temp:~-3%=%2
|
||||
goto:eof
|
||||
|
||||
:pop
|
||||
set queu >nul 2>nul
|
||||
if %errorlevel% equ 1 (set order=-1&set item=no more items & goto:eof)
|
||||
for /f "tokens=1,2 delims==" %%a in ('set queu') do set %%a=& set order=%%a& set item=%%~b& goto:next
|
||||
:next
|
||||
set order= %order:~-3%
|
||||
goto:eof
|
||||
72
Task/Priority-queue/C/priority-queue.c
Normal file
72
Task/Priority-queue/C/priority-queue.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
int priority;
|
||||
char *data;
|
||||
} node_t;
|
||||
|
||||
typedef struct {
|
||||
node_t *nodes;
|
||||
int len;
|
||||
int size;
|
||||
} heap_t;
|
||||
|
||||
void push (heap_t *h, int priority, char *data) {
|
||||
if (h->len + 1 >= h->size) {
|
||||
h->size = h->size ? h->size * 2 : 4;
|
||||
h->nodes = (node_t *)realloc(h->nodes, h->size * sizeof (node_t));
|
||||
}
|
||||
int i = h->len + 1;
|
||||
int j = i / 2;
|
||||
while (i > 1 && h->nodes[j].priority > priority) {
|
||||
h->nodes[i] = h->nodes[j];
|
||||
i = j;
|
||||
j = j / 2;
|
||||
}
|
||||
h->nodes[i].priority = priority;
|
||||
h->nodes[i].data = data;
|
||||
h->len++;
|
||||
}
|
||||
|
||||
char *pop (heap_t *h) {
|
||||
int i, j, k;
|
||||
if (!h->len) {
|
||||
return NULL;
|
||||
}
|
||||
char *data = h->nodes[1].data;
|
||||
h->nodes[1] = h->nodes[h->len];
|
||||
h->len--;
|
||||
i = 1;
|
||||
while (1) {
|
||||
k = i;
|
||||
j = 2 * i;
|
||||
if (j <= h->len && h->nodes[j].priority < h->nodes[k].priority) {
|
||||
k = j;
|
||||
}
|
||||
if (j + 1 <= h->len && h->nodes[j + 1].priority < h->nodes[k].priority) {
|
||||
k = j + 1;
|
||||
}
|
||||
if (k == i) {
|
||||
break;
|
||||
}
|
||||
h->nodes[i] = h->nodes[k];
|
||||
i = k;
|
||||
}
|
||||
h->nodes[i] = h->nodes[h->len + 1];
|
||||
return data;
|
||||
}
|
||||
|
||||
int main () {
|
||||
heap_t *h = (heap_t *)calloc(1, sizeof (heap_t));
|
||||
push(h, 3, "Clear drains");
|
||||
push(h, 4, "Feed cat");
|
||||
push(h, 5, "Make tea");
|
||||
push(h, 1, "Solve RC tasks");
|
||||
push(h, 2, "Tax return");
|
||||
int i;
|
||||
for (i = 0; i < 5; i++) {
|
||||
printf("%s\n", pop(h));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
30
Task/Priority-queue/Elixir/priority-queue.elixir
Normal file
30
Task/Priority-queue/Elixir/priority-queue.elixir
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
defmodule Priority do
|
||||
def create, do: :gb_trees.empty
|
||||
|
||||
def insert( element, priority, queue ), do: :gb_trees.enter( priority, element, queue )
|
||||
|
||||
def peek( queue ) do
|
||||
{_priority, element, _new_queue} = :gb_trees.take_smallest( queue )
|
||||
element
|
||||
end
|
||||
|
||||
def task do
|
||||
items = [{3, "Clear drains"}, {4, "Feed cat"}, {5, "Make tea"}, {1, "Solve RC tasks"}, {2, "Tax return"}]
|
||||
queue = Enum.reduce(items, create, fn({priority, element}, acc) -> insert( element, priority, acc ) end)
|
||||
IO.puts "peek priority: #{peek( queue )}"
|
||||
Enum.reduce(1..length(items), queue, fn(_n, q) -> write_top( q ) end)
|
||||
end
|
||||
|
||||
def top( queue ) do
|
||||
{_priority, element, new_queue} = :gb_trees.take_smallest( queue )
|
||||
{element, new_queue}
|
||||
end
|
||||
|
||||
defp write_top( q ) do
|
||||
{element, new_queue} = top( q )
|
||||
IO.puts "top priority: #{element}"
|
||||
new_queue
|
||||
end
|
||||
end
|
||||
|
||||
Priority.task
|
||||
|
|
@ -17,7 +17,7 @@ class Task implements Comparable<Task> {
|
|||
return priority < other.priority ? -1 : priority > other.priority ? 1 : 0;
|
||||
}
|
||||
|
||||
public static final void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
PriorityQueue<Task> pq = new PriorityQueue<Task>();
|
||||
pq.add(new Task(3, "Clear drains"));
|
||||
pq.add(new Task(4, "Feed cat"));
|
||||
|
|
|
|||
20
Task/Priority-queue/Kotlin/priority-queue.kotlin
Normal file
20
Task/Priority-queue/Kotlin/priority-queue.kotlin
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import java.util.PriorityQueue
|
||||
|
||||
internal data class Task(val priority: Int, val name: String) : Comparable<Task> {
|
||||
override fun compareTo(other: Task) = when {
|
||||
priority < other.priority -> -1
|
||||
priority > other.priority -> 1
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
|
||||
private infix fun String.priority(priority: Int) = Task(priority, this)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val q = PriorityQueue(listOf("Clear drains" priority 3,
|
||||
"Feed cat" priority 4,
|
||||
"Make tea" priority 5,
|
||||
"Solve RC tasks" priority 1,
|
||||
"Tax return" priority 2))
|
||||
while (q.any()) println(q.remove())
|
||||
}
|
||||
|
|
@ -1,19 +1,18 @@
|
|||
PriorityQueue <- function() {
|
||||
keys <- values <- NULL
|
||||
insert <- function(key, value) {
|
||||
temp <- c(keys, key)
|
||||
ord <- order(temp)
|
||||
keys <<- temp[ord]
|
||||
values <<- c(values, list(value))[ord]
|
||||
ord <- findInterval(key, keys)
|
||||
keys <<- append(keys, key, ord)
|
||||
values <<- append(values, value, ord)
|
||||
}
|
||||
pop <- function() {
|
||||
head <- values[[1]]
|
||||
head <- list(key=keys[1],value=values[[1]])
|
||||
values <<- values[-1]
|
||||
keys <<- keys[-1]
|
||||
return(head)
|
||||
}
|
||||
empty <- function() length(keys) == 0
|
||||
list(insert = insert, pop = pop, empty = empty)
|
||||
environment()
|
||||
}
|
||||
|
||||
pq <- PriorityQueue()
|
||||
|
|
@ -23,5 +22,5 @@ pq$insert(5, "Make tea")
|
|||
pq$insert(1, "Solve RC tasks")
|
||||
pq$insert(2, "Tax return")
|
||||
while(!pq$empty()) {
|
||||
print(pq$pop())
|
||||
with(pq$pop(), cat(key,":",value,"\n"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[1] "Solve RC tasks"
|
||||
[1] "Tax return"
|
||||
[1] "Clear drains"
|
||||
[1] "Feed cat"
|
||||
[1] "Make tea"
|
||||
1 : Solve RC tasks
|
||||
2 : Tax return
|
||||
3 : Clear drains
|
||||
4 : Feed cat
|
||||
5 : Make tea
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
PriorityQueue <-
|
||||
setRefClass("PriorityQueue",
|
||||
fields = list(keys = "numeric", values = "list"),
|
||||
methods = list(
|
||||
insert = function(key,value) {
|
||||
temp <- c(keys,key)
|
||||
ord <- order(temp)
|
||||
keys <<- temp[ord]
|
||||
values <<- c(values,list(value))[ord]
|
||||
},
|
||||
pop = function() {
|
||||
head <- values[[1]]
|
||||
keys <<- keys[-1]
|
||||
values <<- values[-1]
|
||||
return(head)
|
||||
},
|
||||
empty = function() length(keys) == 0
|
||||
setRefClass("PriorityQueue",
|
||||
fields = list(keys = "numeric", values = "list"),
|
||||
methods = list(
|
||||
insert = function(key,value) {
|
||||
insert.order <- findInterval(key, keys)
|
||||
keys <<- append(keys, key, insert.order)
|
||||
values <<- append(values, value, insert.order)
|
||||
},
|
||||
pop = function() {
|
||||
head <- list(key=keys[1],value=values[[1]])
|
||||
keys <<- keys[-1]
|
||||
values <<- values[-1]
|
||||
return(head)
|
||||
},
|
||||
empty = function() length(keys) == 0
|
||||
))
|
||||
|
|
|
|||
|
|
@ -1,31 +1,26 @@
|
|||
/*REXX pgm implements a priority queue; with insert/show/delete top task*/
|
||||
numeric digits 100; #=0; @.= /*big #, 0 tasks, null priority queue*/
|
||||
say '══════ inserting tasks.'; call .ins 3 'Clear drains'
|
||||
call .ins 4 'Feed cat'
|
||||
call .ins 5 'Make tea'
|
||||
call .ins 1 'Solve RC tasks'
|
||||
call .ins 2 'Tax return'
|
||||
call .ins 6 'Relax'
|
||||
call .ins 6 'Enjoy'
|
||||
/*REXX program implements a priority queue; with insert/display/delete the top task. */
|
||||
#=0; @.= /*0 tasks; nullify the priority queue.*/
|
||||
say '══════ inserting tasks.'; call .ins 3 "Clear drains"
|
||||
call .ins 4 "Feed cat"
|
||||
call .ins 5 "Make tea"
|
||||
call .ins 1 "Solve RC tasks"
|
||||
call .ins 2 "Tax return"
|
||||
call .ins 6 "Relax"
|
||||
call .ins 6 "Enjoy"
|
||||
say '══════ showing tasks.'; call .show
|
||||
say '══════ deletes top task.'; do # /*number of tasks. */
|
||||
say .del() /*delete top task. */
|
||||
end /* [↑] do top first.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────.INS subroutine─────────────────────*/
|
||||
.ins: procedure expose @. #; #=#+1; @.#=arg(1); return # /*entry,P,task.*/
|
||||
/*──────────────────────────────────.DEL subroutine─────────────────────*/
|
||||
.del: procedure expose @. #; parse arg p; if p=='' then p=.top()
|
||||
x=@.p; @.p= /*delete the top task entry. */
|
||||
return x /*return task that was deleted. */
|
||||
/*──────────────────────────────────.SHOW subroutine────────────────────*/
|
||||
.show: procedure expose @. #
|
||||
do j=1 for #; _=@.j; if _=='' then iterate; say _
|
||||
end /*j*/ /* [↑] show whole list or just 1.*/
|
||||
return
|
||||
/*──────────────────────────────────.TOP subroutine─────────────────────*/
|
||||
.top: procedure expose @. #; top=; top#=
|
||||
do j=1 for #; _=word(@.j,1); if _=='' then iterate
|
||||
if top=='' | _>top then do; top=_; top#=j; end
|
||||
end /*j*/
|
||||
return top#
|
||||
say '══════ deletes top task.'; say .del() /*delete the top task. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
.del: procedure expose @. #; parse arg p; if p=='' then p=.top(); x=@.p; @.p=; return x
|
||||
/*delete the top task entry. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
.ins: procedure expose @. #; #=#+1; @.#=arg(1); return # /*entry, P, task.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
.show: procedure expose @. #; do j=1 for #; _=@.j; if _=='' then iterate; say _; end
|
||||
return /* [↑] show whole list or just one. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
.top: procedure expose @. #; top=; top#=
|
||||
do j=1 for #; _=word(@.j,1); if _=='' then iterate
|
||||
if top=='' | _>top then do; top=_; top#=j; end
|
||||
end /*j*/
|
||||
return top#
|
||||
|
|
|
|||
48
Task/Priority-queue/Rust/priority-queue.rust
Normal file
48
Task/Priority-queue/Rust/priority-queue.rust
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use std::collections::BinaryHeap;
|
||||
use std::cmp::Ordering;
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct Item<'a> {
|
||||
priority: usize,
|
||||
task: Cow<'a, str>, // Takes either borrowed or owned string
|
||||
}
|
||||
|
||||
impl<'a> Item<'a> {
|
||||
fn new<T>(p: usize, t: T) -> Self
|
||||
where T: Into<Cow<'a, str>>
|
||||
{
|
||||
Item {
|
||||
priority: p,
|
||||
task: t.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Manually implpement Ord so we have a min heap
|
||||
impl<'a> Ord for Item<'a> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
other.priority.cmp(&self.priority)
|
||||
}
|
||||
}
|
||||
|
||||
// PartialOrd is required by Ord
|
||||
impl<'a> PartialOrd for Item<'a> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut queue = BinaryHeap::with_capacity(5);
|
||||
queue.push(Item::new(3, "Clear drains"));
|
||||
queue.push(Item::new(4, "Feed cat"));
|
||||
queue.push(Item::new(5, "Make tea"));
|
||||
queue.push(Item::new(1, "Solve RC tasks"));
|
||||
queue.push(Item::new(2, "Tax return"));
|
||||
|
||||
for item in queue {
|
||||
println!("{}", item.task);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue