find middle of a linked list in c – dead nifty, innit
intro
Oi, you lot! New to C and fancy summat a bit cheeky? We’re gonna sling together a lil program to nab the middle fella in a linked list—ya know, them chains of boxes where we wanna find the one bang in the middle. It’s dead clever but proper simple—like when I was tryna find the middle of my mate’s conga line at the pub (kept miscounting, too smashed). Great for getting your head round pointers 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
struct Node { // lil box, innit
int data;
struct Node* next;
};
void showMiddle(struct Node* head) { // nab the middle
struct Node *slow=head,*fast=head; // two mates
if(head==NULL) {
printf("oi, list’s empty!\n");
return;
}
while(fast!=NULL&&fast->next!=NULL) { // fast runs twice as quick
slow=slow->next; // plod along
fast=fast->next->next; // zip ahead
}
printf("middle fella is %d\n",slow->data);
}
int main() { // nearly did void, ha!
struct Node* head=NULL; // start with nowt
struct Node* second=NULL;
struct Node* third=NULL; // three lil pals
head=(struct Node*)malloc(sizeof(struct Node)); // nab space
second=(struct Node*)malloc(sizeof(struct Node));
third=(struct Node*)malloc(sizeof(struct Node));
head->data=10; // chuck numbers in
head->next=second;
second->data=20;
second->next=third;
third->data=30;
third->next=NULL; // end it
printf("here’s yer middle: ");
showMiddle(head); // nab it
free(head); // tidy up
free(second);
free(third); // forgot ; once, ugh!
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I hard-coded a few numbers—worked bang on, mate!
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. them `#include` bits
This is me nicking my toolkit. `
2. `struct Node` malarky
`struct Node` is my lil box—`int data` for the number, `struct Node* next` for the pointer to the next box. Like chaining mates in a line, innit!
3. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!
4. boxes: `struct Node* head=NULL;`
These are my lil pals—`head`, `second`, `third`—all pointers to `Node`s, starting at `NULL` coz we ain’t got nowt yet. `head`’s the start of the chain!
5. making the chain: `malloc` and stuff
`head=(struct Node*)malloc(sizeof(struct Node))` nabs memory for a `Node`—like renting a lil box. Fill it with `head->data=10` and link it to `second` with `head->next=second`. Same for `second` and `third`, til `third->next=NULL` ends it. Like chaining beer cans!
6. finding middle: `void showMiddle(struct Node* head)`
Here’s the guts! `showMiddle` uses two mates: `slow` and `fast`, both starting at `head`. If `head==NULL`, “list’s empty!” Else, `while(fast!=NULL&&fast->next!=NULL)` loops—`fast` zips two steps with `fast->next->next`, `slow` plods one with `slow->next`. When `fast` hits the end, `slow`’s in the middle—clever, eh! Then `printf` blurts `slow->data`.
7. chucking it out: `printf` and `showMiddle`
`printf("here’s yer middle: ")` sets it up, then `showMiddle(head)` does the job. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!
8. `free` and `return 0` to scarper
`free(head)` and pals tidy up—don’t wanna leave rubbish about! Then `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 nab the middle of a linked list. Faff with it—chuck in more numbers, tweak it to say “middle lad” if ya fancy. First time I ran this, I had four nodes and got confused—turns out it picks the second middle if it’s even! You’re a star already, mate—keep smashing it! 😊