0% found this document useful (0 votes)
3 views9 pages

Wa0047.

The document outlines the implementation of a singly linked list (SLL) in C, detailing the structure of a node and key operations such as creating a node, inserting at the head and end, deleting a node, and printing the list. It includes a complete program demonstrating these operations with sample output. The SLL allows for efficient dynamic memory management compared to static arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Wa0047.

The document outlines the implementation of a singly linked list (SLL) in C, detailing the structure of a node and key operations such as creating a node, inserting at the head and end, deleting a node, and printing the list. It includes a complete program demonstrating these operations with sample output. The SLL allows for efficient dynamic memory management compared to static arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment No: 8

AIM:- Implementation of singly linked


list
Theory:-
A Singly Linked List (SLL) is a dynamic
data structure in which elements,
called nodes, are linked together in a
linear sequence. Each node contains
two parts: data and a pointer (or link)
to the next node in the list. This
structure allows efficient insertion and
deletion operations compared to static
arrays.

1. Structure of a Node
Each node in a singly linked list is
composed of:

Data Field: Stores the actual data or


value of the node.

Next Pointer: A pointer that stores the


address of the next node in the list.

In C, the structure of a node can be


represented as:

struct Node {
int data; // Stores the data
struct Node* next; // Pointer to the
next node
};

2. Key Operations on Singly Linked List

1. Creating a Node:
A function is used to allocate memory
dynamically for a new node using
malloc() and initialize its data and next
pointer.

struct Node* createNode(int data) {


struct Node* newNode

PROGRAM:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtHead(struct Node** head,
int data) {
struct Node* newNode =
createNode(data);
newNode->next = *head;
*head = newNode;
}

void insertAtEnd(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;
}

void deleteNode(struct Node** head,


int key) {
struct Node* temp = *head;
struct Node* prev = NULL;

if (temp != NULL && temp->data ==


key) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data
!= key) {
prev = temp;
temp = temp->next;
}

if (temp == NULL) return;

prev->next = temp->next;
free(temp);
}

void printList(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;

insertAtEnd(&head, 40);
insertAtEnd(&head, 60);
insertAtEnd(&head, 80);
insertAtHead(&head, 5);
printf("Linked List: ");
printList(head);

printf("After deleting node 60:\n");


deleteNode(&head, 60);
printList(head);

return 0;
}
OUTPUT:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy