Menu
CoddyTech

ختامي: نقل نموذج قائمة الانتظار

الدرس 48 من 48 في دورة PHP لمطوري C على Coddy.

challenge icon

التحدي

سهل

الإدخال هو [initial,operations]. قم بنقل البرنامج المرجعي المصدري المرفق في هذه المهمة. يخزن WorkQueue(initial array) عناصر نصية. يضيف push(string):void في النهاية؛ ويزيل pop():?string العنصر الأقدم ويعيده أو يعيد null عندما يكون فارغًا؛ ويحسب size():int العناصر المتبقية. العمليات هي [push,text] أو [pop]. يطبع المشغل القيم المسحوبة مفصولة بفواصل، مع استخدام EMPTY بدلاً من null، ثم |size. تحتوي عناصر الإدخال على أحرف ASCII فقط.

يقرأ السكربت الرئيسي المرفق قيمة JSON واحدة من الإدخال القياسي، ويفحص واجهات البرمجة العامة (APIs) المسمّاة، ويطبع النتيجة. قم بتنفيذ تصريحات الوحدة دون طباعة؛ لا تستبدل أداة التشغيل ولا تقرأ الإدخال من الوحدة. يلبي الإدخال الأنواع والحدود المذكورة.

قم بتعديل WorkQueue.php. main.php هو ملف الدخول المغلق. تستخدم التبعيات require_once __DIR__ مع اسم ملف نسبي.

برنامج C المرجعي

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
struct WorkQueue { char **items; size_t count; };
static int queue_push(struct WorkQueue *queue, const char *text) {
    size_t length = strlen(text);
    if (length == SIZE_MAX || queue->count >= SIZE_MAX / sizeof *queue->items) return 0;
    char *copy = malloc(length + 1);
    if (copy == NULL) return 0;
    memcpy(copy, text, length + 1);
    char **grown = realloc(queue->items, (queue->count + 1) * sizeof *queue->items);
    if (grown == NULL) { free(copy); return 0; }
    queue->items = grown;
    queue->items[queue->count++] = copy;
    return 1;
}
/* A successful pop transfers ownership to the caller; NULL means empty. */
static char *queue_pop(struct WorkQueue *queue) {
    if (queue->count == 0) return NULL;
    char *first = queue->items[0];
    --queue->count;
    memmove(queue->items, queue->items + 1, queue->count * sizeof *queue->items);
    return first;
}
static size_t queue_size(const struct WorkQueue *queue) { return queue->count; }
static void queue_destroy(struct WorkQueue *queue) {
    for (size_t i = 0; i < queue->count; ++i) free(queue->items[i]);
    free(queue->items);
    *queue = (struct WorkQueue){NULL, 0};
}
static int queue_init(struct WorkQueue *queue, const char *const *initial, size_t count) {
    *queue = (struct WorkQueue){NULL, 0};
    for (size_t i = 0; i < count; ++i) {
        if (!queue_push(queue, initial[i])) { queue_destroy(queue); return 0; }
    }
    return 1;
}
int main(void) {
    const char *initial[] = {"a", "b"};
    struct WorkQueue queue;
    if (!queue_init(&queue, initial, 2)) return 1;
    char *first = queue_pop(&queue);
    if (!queue_push(&queue, "c")) { free(first); queue_destroy(&queue); return 1; }
    char *second = queue_pop(&queue);
    printf("%s,%s|%zu\n", first ? first : "EMPTY", second ? second : "EMPTY", queue_size(&queue));
    free(first); free(second); queue_destroy(&queue);
    return ferror(stdout) ? 1 : 0;
}

جرّب بنفسك

<?php
declare(strict_types=1);
require_once __DIR__."/WorkQueue.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}
function queueReport(array $i):string{$q=new WorkQueue($i[0]);$out=[];foreach($i[1] as $op){if($op[0]==="push")$q->push($op[1]);else $out[]=$q->pop()??"EMPTY";}return implode(",",$out)."|".$q->size();}
$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
$a=new WorkQueue(["a"]);$b=new WorkQueue([]);ensure($a->pop()==="a" && $a->pop()===null && $b->size()===0,"queue independence");$a->push("z");ensure($a->size()===1 && $a->pop()==="z","queue API");
echo queueReport($input),"\n";

جميع دروس PHP لمطوري C

تدرّب بنفسك: مترجم PHP عبر الإنترنت