Sitemap
Linking

Linking

Member-only story

Advanced Techniques in C Programming — 3

14 min readOct 27, 2024
Press enter or click to view image in full size
Photo by Jakub Żerdzicki on Unsplash

45. Implementing a Singly Linked List

This example demonstrates how to implement a singly linked list with basic operations like insertion and traversal.

c
Copy code
#include <stdio.h>
#include <stdlib.h>
// Define a structure for linked list nodes
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end of the list
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Function to display the linked list
void displayList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;

--

--