Last active
February 22, 2026 07:07
-
-
Save raiyansarker/a838ea6a75891f5c11529e4ffae25b12 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| typedef struct Node { | |
| int data; | |
| struct Node *next; | |
| } Node; | |
| void insert_end(Node **head, int data); | |
| void insert_begin(Node **head, int data); | |
| void delete_end(Node **head); | |
| void delete_begin(Node **head); | |
| Node* find(Node *head, int data); | |
| void display(Node *head); | |
| int main() { | |
| Node *head = NULL; | |
| insert_end(&head, 1); | |
| insert_end(&head, 2); | |
| insert_end(&head, 3); | |
| insert_end(&head, 4); | |
| display(head); | |
| insert_begin(&head, 9); | |
| insert_begin(&head, 10); | |
| insert_begin(&head, 11); | |
| display(head); | |
| Node *s = find(head, 1); | |
| if (s == NULL) printf("Not found\n"); | |
| else printf("%d\n", s->data); | |
| delete_end(&head); | |
| delete_end(&head); | |
| delete_end(&head); | |
| display(head); | |
| delete_begin(&head); | |
| delete_begin(&head); | |
| delete_begin(&head); | |
| delete_begin(&head); | |
| display(head); | |
| return 0; | |
| } | |
| void insert_end(Node **head, int data) { | |
| Node *new = (Node*)malloc(sizeof(Node)); | |
| new->data = data; | |
| new->next = NULL; | |
| if (*head == NULL) { | |
| *head = new; | |
| return; | |
| } | |
| Node *curr = *head; | |
| while (curr->next != NULL) curr = curr->next; | |
| curr->next = new; | |
| } | |
| void insert_begin(Node **head, int data) { | |
| Node *new = (Node*)malloc(sizeof(Node)); | |
| new->data = data; | |
| new->next = *head; | |
| *head = new; | |
| } | |
| void delete_end(Node **head) { | |
| Node *curr = *head, *prev = NULL; | |
| if (curr == NULL) return; | |
| while (curr->next != NULL) { | |
| prev = curr; | |
| curr = curr->next; | |
| } | |
| /** | |
| * only a single node is there in the list | |
| */ | |
| if (prev == NULL) *head = NULL; | |
| else prev->next = NULL; | |
| free(curr); | |
| } | |
| void delete_begin(Node **head) { | |
| if (*head == NULL) return; | |
| Node *t = *head; | |
| *head = t->next; | |
| free(t); | |
| } | |
| Node* find(Node *head, int data) { | |
| while (head != NULL) { | |
| if (head->data == data) { | |
| return head; | |
| } | |
| head = head->next; | |
| } | |
| return NULL; | |
| } | |
| void display(Node *head) { | |
| while (head != NULL) { | |
| printf("%d ", head->data); | |
| head = head->next; | |
| } | |
| printf("\n"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment