Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Linked List Code1

linked list code

Uploaded by

vinit.vph
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)
7 views

Linked List Code1

linked list code

Uploaded by

vinit.vph
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/ 2

#include <stdio.

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

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


struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}

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


struct Node* current = *head;
struct Node* previous = NULL;
while (current != NULL && current->data != data) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Node with data %d not found\n", data);
return;
}
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
}

void display(struct Node* head) {


while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
int choice, data;
while (1) {
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insert(&head, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
delete(&head, data);
break;
case 3:
printf("Linked list: ");
display(head);
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}

You might also like