0% found this document useful (0 votes)
16 views

2.single_linked_list

The document contains a C program that implements a singly linked list with functionalities to create nodes, insert nodes at the beginning and end, delete a node by value, print the list, and free the entire list. It includes a main function that demonstrates these operations by inserting elements, deleting a specific node, and printing the list before and after deletion. The program ensures proper memory management by freeing allocated memory for nodes.

Uploaded by

kodurusailalitya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

2.single_linked_list

The document contains a C program that implements a singly linked list with functionalities to create nodes, insert nodes at the beginning and end, delete a node by value, print the list, and free the entire list. It includes a main function that demonstrates these operations by inserting elements, deleting a specific node, and printing the list before and after deletion. The program ensures proper memory management by freeing allocated memory for nodes.

Uploaded by

kodurusailalitya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>

#include <stdlib.h>

// Define the structure for a node

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));

if (newNode == NULL) {

printf("Memory allocation failed!\n");

exit(1);

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to insert at beginning

struct Node* insertAtBeginning(struct Node* head, int data) {

struct Node* newNode = createNode(data);

newNode->next = head;

return newNode;

// Function to insert at end

struct Node* insertAtEnd(struct Node* head, int data) {

struct Node* newNode = createNode(data);


if (head == NULL) {

return newNode;

struct Node* current = head;

while (current->next != NULL) {

current = current->next;

current->next = newNode;

return head;

// Function to delete a node with given value

struct Node* deleteNode(struct Node* head, int data) {

if (head == NULL) return NULL;

if (head->data == data) {

struct Node* temp = head->next;

free(head);

return temp;

struct Node* current = head;

while (current->next != NULL && current->next->data != data) {

current = current->next;

if (current->next != NULL) {

struct Node* temp = current->next;

current->next = temp->next;

free(temp);

}
return head;

// Function to print the list

void printList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

printf("%d -> ", current->data);

current = current->next;

printf("NULL\n");

// Function to free the entire list

void freeList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

struct Node* temp = current;

current = current->next;

free(temp);

// Example usage

int main() {

struct Node* head = NULL;

// Insert some elements

head = insertAtEnd(head, 10);

head = insertAtEnd(head, 20);

head = insertAtBeginning(head, 5);

head = insertAtEnd(head, 30);


// Print the list

printf("Original list: ");

printList(head);

// Delete a node

head = deleteNode(head, 20);

printf("After deleting 20: ");

printList(head);

// Free the list

freeList(head);

return 0;

You might also like