Stacks and queues (array-backed) · الكدوس وال queues (مدعومة بالمصفوفات)
Two ways to hold items
- A stack and a queue both hold a line of items, but they differ in which item comes out next.
- A stack is LIFO: Last In, First Out — like a pile of plates.
- A queue is FIFO: First In, First Out — like a line of people.
طريقتان لحفظ العناصر
- المكدس والطابور كلاهما يحفظان صفًا من العناصر، لكنهما يختلفان في أي عنصر سيخرج أولاً.
- المكدس هو LIFO: أخير دخول، أول خروج — مثل رصة أطباق.
- الطابور هو FIFO: أول دخول، أول خروج — مثل طابور أشخاص.
A stack is LIFO
- push adds an item on top. pop removes and returns the top item. peek looks at the top without removing it.
- The last item you pushed is the first one you pop.
- We store the items in an array
dataand an indextopthat counts how many are in.
المكدس LIFO
- push يضيف عنصراً في الأعلى. pop يزيل ويُرجع العنصر الأعلى. peek ينظر إلى الأعلى دون إزالته.
- آخر عنصر أضفته هو الأول الذي ستزيله.
- نخزن العناصر في مصفوفة
dataومؤشرtopيعدّ كم عدد العناصر فيها.
Array-backed stack
- With
topas the count, the top item is atdata[top - 1]. - push: write at
data[top], thentop++. pop:top--, then returndata[top]. - (For these tasks the array is big enough; you do not need to check for "full".)
مكدس مدعوم بمصفوفة
- مع
topكعداد، العنصر العلوي موجود عندdata[top - 1]. - push: اكتب عند
data[top]، ثم زِدtop++. pop: نزلtop--، ثم أجبdata[top]. - (لهذه المهام المصفوفة كبيرة بما يكفي؛ لا تحتاج للتحقق من "امتلاء".)
A queue is FIFO
- enqueue adds an item at the back. dequeue removes and returns the item at the front.
- The first item you enqueue is the first one you dequeue.
- We keep two indexes:
front(next to leave) andback(next free slot).
الطابور FIFO
- enqueue يضيف عنصراً في الخلف. dequeue يزيل ويُرجع العنصر في الأمام.
- أول عنصر تضيفه هو الأول الذي ستزيله.
- نحافظ على مؤشرين:
front(الذي سيغادر بعد ذلك) وback(الشقّة الحرة التالية).
Array-backed queue
- enqueue: write at
data[back], thenback++. dequeue: readdata[front], thenfront++, and return it. frontchasesbackas items come and go.- (For these tasks the array is big enough; you do not need to wrap around or check for "empty".)
طابور مدعوم بمصفوفة
- enqueue: اكتب عند
data[back]، ثم زِدback++. dequeue: اقرأdata[front]، ثم زِدfront++، وأجب عنه. frontيتابعbackبينما تدخل العناصر وتخرج.- (لهذه المهام المصفوفة كبيرة بما يكفي؛ لا تحتاج للتفاف أو التحقق من "الفراغ".)
Common mistakes
- A stack is last-in-first-out; a queue is first-in-first-out.
- Check the structure is not empty before you pop or dequeue.
أخطاء شائعة
- المكدس أخير دخول أول خروج؛ الطابور أول دخول أول خروج.
- تحقق من أن البنية ليست فارغة قبل pop أو dequeue.
Now you try
- The
StackandQueuestructs are given in the starter. Uses->top,q->front, andq->back. - Do not write a
main— the checker provides one.
الآن جرب بنفسك
- هياكل
StackوQueueمعطاة في ملف البدء. استخدمs->top،q->front، وq->back. - لا تكتب
main— لأن الفاحص يوفر واحداً تلقائياً.
Stacks and queues · الكدوس وال queues
A stack is LIFO; a queue is FIFO. Step through the operations. · الكادوس LIFO؛ الـ queue FIFO. مرّ عبر العمليات.
The Stack struct (with data and a count top) is given. Complete void push(Stack *s, int v) and int pop(Stack *s) so the stack is LIFO. Do not write a main. · هيكل Stack (مع data وعدد عداد top) معطى. أتمم void push(Stack *s, int v) وint pop(Stack *s) ليكون المكدس LIFO. لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
The Stack struct is given. Complete int peek(const Stack *s) so it returns the top item without removing it. Assume the stack is not empty. Do not write a main. · مطلوب الـ Stack مُعطى. أكمل int peek(const Stack *s) لإرجاع العنصر العلوي بدون إزالته. افترض أن المكدس ليس فارغاً. لا تكتب a main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
The Queue struct (with data, front, and back) is given. Complete void enqueue(Queue *q, int v) and int dequeue(Queue *q) so the queue is FIFO. Do not write a main. · مطلوب الـ Queue (مع data، front، وback) مُعطى. أكمل void enqueue(Queue *q, int v) وint dequeue(Queue *q) لتكون الطابور FIFO. لا تكتب a main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.