0% found this document useful (0 votes)
5 views6 pages

EXP1

Experiment 1 aktu DS

Uploaded by

sahilakash2264
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)
5 views6 pages

EXP1

Experiment 1 aktu DS

Uploaded by

sahilakash2264
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/ 6

EXPERIMENT NO-01 Implementing of Singly Linked List with their

operations: Insert,

Display, Delete, Search, Count

EXPERIMENT NO -1 Implementing of Singly


Linked List with their operations: Insert, Display, Delete,
Search,Count

SOURCE CODE:

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

void insert(Node** head, int data);

void display(Node* head);

void delete(Node** head, int data);

Node* search(Node* head, int data);

int count(Node* head);

int main() {

Node* head = NULL;

int choice, value;

while (1) {

printf("\nSingly Linked List Operations:\n");

printf("1. Insert\n");

printf("2. Display\n");

printf("3. Delete\n");

printf("4. Search\n");
EXPERIMENT NO-01 Implementing of Singly Linked List with their
operations: Insert,

Display, Delete, Search, Count

printf("5. Count\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to insert: ");

scanf("%d", &value);

insert(&head, value);

break;

case 2:

display(head);

break;

case 3:

printf("Enter the value to delete: ");

scanf("%d", &value);

delete(&head, value);

break;

case 4:

printf("Enter the value to search: ");

scanf("%d", &value);

Node* result = search(head, value);

if (result != NULL)

printf("Value %d found in the list.\n", value);

else

printf("Value %d not found in the list.\n", value);

break;
EXPERIMENT NO-01 Implementing of Singly Linked List with their
operations: Insert,

Display, Delete, Search, Count

case 5:

printf("Total nodes in the list: %d\n", count(head));

break;

case 6:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

return 0;

void insert(Node** head, int data) {

Node* new_node = (Node*)malloc(sizeof(Node));

new_node->data = data;

new_node->next = NULL;

if (*head == NULL) {

*head = new_node;

} else {

Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = new_node;

printf("Value %d inserted successfully.\n", data);


EXPERIMENT NO-01 Implementing of Singly Linked List with their
operations: Insert,

Display, Delete, Search, Count

void display(Node* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

printf("List elements: ");

while (head != NULL) {

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

head = head->next;

printf("NULL\n");

void delete(Node** head, int data) {

if (*head == NULL) {

printf("List is empty. Nothing to delete.\n");

return;

Node* temp = *head;

Node* prev = NULL;

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

*head = temp->next;

free(temp);

printf("Value %d deleted successfully.\n", data);


EXPERIMENT NO-01 Implementing of Singly Linked List with their
operations: Insert,

Display, Delete, Search, Count

return;

while (temp != NULL && temp->data != data) {

prev = temp;

temp = temp->next;

if (temp == NULL) {

printf("Value %d not found in the list.\n", data);

return;

prev->next = temp->next;

free(temp);

printf("Value %d deleted successfully.\n", data);

Node* search(Node* head, int data) {

while (head != NULL) {

if (head->data == data)

return head;

head = head->next;

return NULL;

int count(Node* head) {


EXPERIMENT NO-01 Implementing of Singly Linked List with their
operations: Insert,

Display, Delete, Search, Count

int count = 0;

while (head != NULL) {

count++;

head = head->next;

return count;

OUTPUT:

You might also like