Stacks and queues (array-backed) · Tumpukan dan antrean (berbasis array)
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.
Dua cara menyimpan item
- Stack dan queue keduanya menyimpan barisan item, tetapi berbeda dalam item mana yang keluar selanjutnya.
- Stack bersifat LIFO: Last In, First Out — seperti tumpukan piring.
- Queue bersifat FIFO: First In, First Out — seperti antrian orang.
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.
Stack adalah LIFO
- push menambah item di atas. pop menghapus dan mengembalikan item atas. peek melihat item atas tanpa menghapusnya.
- Item terakhir yang Anda push adalah item pertama yang Anda pop.
- Kita menyimpan item-item tersebut dalam array
datadan indekstopyang menghitung berapa banyak yang ada di dalamnya.
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".)
Stack berbasis array
- Dengan
topsebagai penghitung, item atas berada didata[top - 1]. - push: tulis di
data[top], lalutop++. pop:top--, lalu kembalikandata[top]. - (Untuk tugas-tugas ini array cukup besar; Anda tidak perlu memeriksa "penuh").
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).
Queue adalah FIFO
- enqueue menambah item di belakang. dequeue menghapus dan mengembalikan item di depan.
- Item pertama yang Anda enqueue adalah item pertama yang Anda dequeue.
- Kita menyimpan dua indeks:
front(berikutnya yang akan keluar) danback(slot bebas berikutnya).
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".)
Queue berbasis array
- enqueue: tulis di
data[back], laluback++. dequeue: bacadata[front], lalufront++, dan kembalikan. frontmengejarbackseiring item masuk dan keluar.- (Untuk tugas-tugas ini array cukup besar; Anda tidak perlu melingkari atau memeriksa "kosong").
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.
Kesalahan umum
- Stack adalah last-in-first-out; queue adalah first-in-first-out.
- Periksa apakah strukturnya tidak kosong sebelum melakukan pop atau 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.
Sekarang Anda coba
- Struktur
StackdanQueuedisediakan dalam starter. Gunakans->top,q->front, danq->back. - Jangan tulis
main— checker menyediakannya.
Stacks and queues · Tumpukan dan antrean
A stack is LIFO; a queue is FIFO. Step through the operations. · Tumpukan bersifat LIFO; antrean bersifat FIFO. Ikuti langkah-langkah operasi.
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. · Struktur Stack (dengan data dan jumlah top) diberikan. Lengkapi void push(Stack *s, int v) dan int pop(Stack *s) agar tumpukan menjadi LIFO. Jangan tulis sebuah main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Struktur Stack diberikan. Lengkapi int peek(const Stack *s) agar mengembalikan item teratas tanpa menghapusnya. Asumsikan tumpukan tidak kosong. Jangan tulis sebuah main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Struktur Queue (dengan data, front, dan back) diberikan. Lengkapi void enqueue(Queue *q, int v) dan int dequeue(Queue *q) agar antrean menjadi FIFO. Jangan tulis sebuah main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.