Make a Linked List and Show It in C – Dead Wild, Innit

make a linked list and show it in c – dead wild, innit

intro

Oi, you lot! New to C and fancy summat a bit mental? We’re gonna sling together a lil program to make a linked list—ya know, them chains of boxes where each one points to the next—and then chuck its guts out on the screen. It’s dead messy but proper ace—like when I was tryna link up my mates in a conga line at the pub (fell apart after three steps). 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
#include

struct Node { // lil box, innit
int data;
struct Node* next;
};

void showList(struct Node* head) { // show the chain
struct Node* temp=head; // temp mate to wander
if(temp==NULL) {
printf("oi, list’s empty!\n");
return;
}
while(temp!=NULL) { // keep going til the end
printf("%d ",temp->data);
temp=temp->next; // hop to next
}
printf("\n");
}

int main() { // nearly did void, ha!
struct Node* head=NULL; // start with nowt
struct Node* second=NULL;
struct Node* third=NULL; // three lil mates

head=(struct Node*)malloc(sizeof(struct Node)); // nab some space
second=(struct Node*)malloc(sizeof(struct Node));
third=(struct Node*)malloc(sizeof(struct Node));

head->data=10; // chuck some numbers in
head->next=second;

second->data=20;
second->next=third;

third->data=30;
third->next=NULL; // end of the line

printf("here’s yer linked list: ");
showList(head); // show it off

free(head); // tidy up, innit
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!

here’s yer linked list: 10 20 30

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 crisps, 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. `` for `printf` and stuff, `` for `malloc` and `free`—fancy pants “memory grabbers”. Forgot the second one once, and my code just sat there like a plonker. Total mug move!

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 buddies—`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 some 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. showing it: `void showList(struct Node* head)`
`showList` takes `head` and wanders through. `struct Node* temp=head` is my lil walker—if it’s `NULL`, “list’s empty!” Else, `while(temp!=NULL)` loops, printing `temp->data` and hopping to `temp->next`. `\n` keeps it tidy. Like showing off yer conga line!

7. chucking it out: `printf` and `showList`
`printf("here’s yer linked list: ")` sets it up, then `showList(head)` does the heavy lifting. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

8. `free` and `return 0` to scarper
`free(head)` and pals tidy up the memory—don’t wanna leave rubbish lying 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 make a linked list and show it off. Faff with it—chuck in daft numbers, tweak it to say “chain gang” if ya fancy. First time I ran this, I forgot `free` and my mate said it’d leak—cheeky sod! You’re a star already, mate—keep smashing it! 😊