Code: Select all
#include <stdio.h> // for standard IO
#include <stdlib.h> // for some standard library functions.
struct node // structure of the element in the linked list
{
int num; // just some data, this is where you will add what ever you want.
node *next; // this is the key to linked lists. this is a pointer to a node.
}; // think of it like a box in a box infinitly times over, but the boxes never get smaller.
void show_All(node *head) // this function takes the head node of a linked list
// and prints all the elements in order
{ // nore that the very first element is not printed.
// i never use that element, its sole perpous is to keep the head of the list
node* temp = head->next; // this makes a temp node and point it to the second link,
//essensially where out data starts.
while(temp != NULL) // loop while the node is not null
{
printf("node data : %d\n",temp->num); // if its not null we know it has data, print the data.
temp = temp->next; // move to the next link in the list.
}
}
void append_Node(node *head, int val) // this appends val to the end of list
{
node *temp = head; // point to the head.
while(temp->next != NULL) // loop till you get to the end.
{
temp = temp->next;
}
temp->next = (node*)malloc(sizeof(node)); // allocate memory to the next link.
temp = temp->next; // move to that link.
temp->next = NULL; // make sure its next link is null.
temp->num = val; // set the data.
}
int list_Size(node *head) // this returns the number of data nodes in the list.
{
int t = 0; // instatiat a counter
node *temp = head; // temp node pointing to head of list
while(temp->next != NULL) // loop through till the next node is NULL
{
t++; // incriment counter.
temp = temp->next; // move pointer to next link in list.
}
return t; // return the count
}
void clear_list(node *head) // clears the list
{
head->next = (node*)malloc(sizeof(node)); // allocate memory for the first link in the list.
head->next = NULL; // make that link NULL.
}