Stack with Arrays in C – Dead Handy, Innit

stack with arrays in c – dead handy, innit

intro

Oi, you lot! New to C and fancy summat a bit nifty? We’re gonna sling together a lil program to make a stack using an array—ya know, that pile where ya chuck stuff on top and yank it off the same way, like stacking pints at the bar. It’s dead simple but proper useful—like when I was tryna pile up my empties without toppling ‘em (failed, 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 stack size, innit

int stack[MAX],top=-1; // stack and pointer

void push(int num) { // chuck stuff in
if(top==MAX-1) {
printf("oi, stack’s full!\n");
return;
}
top++; // move up
stack[top]=num; // shove it in
printf("%d chucked in\n",num);
}

void pop() { // yank stuff out
if(top==-1) {
printf("oi, stack’s empty!\n");
return;
}
printf("%d yanked out\n",stack[top]);
top--; // move down
}

void showStack() { // peek at the pile
if(top==-1) {
printf("oi, stack’s empty!\n");
return;
}
printf("stack’s got: ");
for(int i=0;i<=top;i++) {
printf("%d ",stack[i]);
}
printf("\n");
}

int main() { // nearly did void, ha!
push(10); // chuck some in
push(20);
push(30);
showStack(); // show it
pop(); // yank one
showStack(); // show again
push(40); // chuck another
showStack(); // 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
stack’s got: 10 20 30
30 yanked out
stack’s got: 10 20
40 chucked in
stack’s got: 10 20 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 stack size—5 slots, plenty for a quick go. Like saying “only five pints fit on this table”!

3. boxes: `int stack[MAX],top=-1;`
These are my lil buckets. `stack[MAX]` is the array—room for 5 numbers. `top=-1` is my pointer—starts at -1 coz it’s empty, moves up as we chuck stuff in.

4. `push(int num)` funtion
`push` chucks a number in. `if(top==MAX-1)` checks if it’s full—yells “stack’s full!” if so. Else, `top++` moves up, `stack[top]=num` shoves it in, and `printf` brags about it. Like piling another pint on!

5. `pop()` funtion
`pop` yanks the top off. `if(top==-1)` checks if it’s empty—yells “stack’s empty!” if so. Else, `printf` shows what’s popped, and `top--` drops down. Like knocking a pint off the pile!

6. `showStack()` funtion
`showStack` peeks at the pile. `if(top==-1)` says “empty!” if nowt’s there. Else, a `for` loop from 0 to `top` prints each `stack[i]` with a space, `\n` tidies it. Like showing off yer stack of empties!

7. `int main()` and chucking it out
`main` kicks it off—`push(10)`, `push(20)`, `push(30)` piles some in, `showStack()` shows it, `pop()` yanks one, `showStack()` again, then `push(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 stack with arrays. Faff with it—chuck in daft numbers, tweak it to say “pile’s up” if ya fancy. First time I ran this, I pushed 6 and it yelled at me—fair enough, eh! You’re a star already, mate—keep smashing it! 😊