Queue with Arrays in C – Dead Useful, Innit

queue with arrays in c – dead useful, innit

intro

Oi, you lot! New to C and fancy summat a bit handy? We’re gonna sling together a lil program to make a queue using an array—ya know, that line-up where ya chuck stuff in at the back and yank it out the front, like queuing for the bar. It’s dead simple but proper cracking—like when I was tryna line up my mates for a round and kept losing the front (too smashed). Great for getting your head round arrays in C without a right faff. Let’s have a butcher’s!

code bit

Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total shambles on your blog. take a gander:

#include
#define MAX 5 // max queue size, innit

int queue[MAX],front=-1,rear=-1; // queue and pointers

void enqueue(int num) { // chuck stuff in
if(rear==MAX-1) {
printf("oi, queue’s full!\n");
return;
}
if(front==-1) { // first one?
front=0; // start here
}
rear++; // move back
queue[rear]=num; // shove it in
printf("%d chucked in\n",num);
}

void dequeue() { // yank stuff out
if(front==-1||front>rear) {
printf("oi, queue’s empty!\n");
return;
}
printf("%d yanked out\n",queue[front]);
front++; // move up
if(front>rear) { // all gone?
front=rear=-1; // reset it
}
}

void showQueue() { // peek at the line
if(front==-1||front>rear) {
printf("oi, queue’s empty!\n");
return;
}
printf("queue’s got: ");
for(int i=front;i<=rear;i++) {
printf("%d ",queue[i]);
}
printf("\n");
}

int main() { // nearly did void, ha!
enqueue(10); // chuck some in
enqueue(20);
enqueue(30);
showQueue(); // show it
dequeue(); // yank one
showQueue(); // show again
enqueue(40); // chuck another
showQueue(); // forgot ; once, ugh!

return 0;
}

what ya get

Here’s what it chucks out when ya run it. I hard-coded a few moves—worked bang on, mate!

10 chucked in
20 chucked in
30 chucked in
queue’s got: 10 20 30
10 yanked out
queue’s got: 20 30
40 chucked in
queue’s got: 20 30 40

how it works, kinda

Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!

1. that `#include ` gubbins
This is me nicking my toolkit. ``—fancy pants “standard input-output”—lets me use `printf`. No extra fluff here, just the basics! Forgot it once, and my code just sat there like a plonker. Total mug move!

2. `#define MAX 5` malarky
`MAX 5` sets the queue size—5 slots, plenty for a quick go. Like saying “only five mates fit in this line”!

3. boxes: `int queue[MAX],front=-1,rear=-1;`
These are my lil buckets. `queue[MAX]` is the array—room for 5 numbers. `front=-1` and `rear=-1` are pointers—both at -1 coz it’s empty, `front` tracks the start, `rear` the end.

4. `enqueue(int num)` funtion
`enqueue` chucks a number in the back. `if(rear==MAX-1)` checks if it’s full—yells “queue’s full!” if so. `if(front==-1)` sets `front=0` for the first one. Then `rear++` moves back, `queue[rear]=num` shoves it in, and `printf` brags about it. Like adding a mate to the bar queue!

5. `dequeue()` funtion
`dequeue` yanks the front off. `if(front==-1||front>rear)` checks if it’s empty—yells “queue’s empty!” if so. Else, `printf` shows what’s out, `front++` moves up. If `front>rear`, resets `front=rear=-1` coz it’s all gone. Like serving the first in line!

6. `showQueue()` funtion
`showQueue` peeks at the line. `if(front==-1||front>rear)` says “empty!” if nowt’s there. Else, a `for` loop from `front` to `rear` prints each `queue[i]` with a space, `\n` tidies it. Like showing yer queue for pints!

7. `int main()` and chucking it out
`main` kicks it off—`enqueue(10)`, `enqueue(20)`, `enqueue(30)` lines some up, `showQueue()` shows it, `dequeue()` yanks one, `showQueue()` again, then `enqueue(40)` and another peek. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

8. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.

And that’s yer lot! A cracking lil program to make a queue with arrays. Faff with it—chuck in daft numbers, tweak it to say “line’s up” if ya fancy. First time I ran this, I yanked too many and it yelled at me—fair cop, eh! You’re a star already, mate—keep smashing it! 😊