Last active
May 18, 2024 03:43
-
-
Save kulothunganug/34795ddca3a3b7d4c81a9b0290c4a6e4 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> | |
struct node { | |
int data; | |
struct node *next; | |
} *head = NULL; | |
void insert_beg(int val); | |
void create_node(int val); | |
void insert_end(int val); | |
void insert_after(struct node *q, int val); | |
void traversal(); | |
void deletion(struct node *p); | |
struct node *find(int val); | |
int main() { | |
struct node *p, *q; | |
int choice, val, ele; | |
do { | |
printf("\n--------------------------------"); | |
printf("\nOpertation on singly linked list"); | |
printf("\n--------------------------------"); | |
printf("\n0. Creation of a node"); | |
printf("\n1. Insert node at first"); | |
printf("\n2. Insert node at last"); | |
printf("\n3. Insert node at position"); | |
printf("\n4. Delete node from any position"); | |
printf("\n5. Search element in the linked list"); | |
printf("\n6. Display list"); | |
printf("\n7. Exit"); | |
printf("\nEnter your choice: "); | |
scanf("%d", &choice); | |
switch (choice) { | |
case 0: | |
printf("\nEnter the value to be inserted: "); | |
scanf("%d", &val); | |
create_node(val); | |
traversal(); | |
break; | |
case 1: | |
printf("\nEnter the value to be inserted: "); | |
scanf("%d", &val); | |
insert_beg(val); | |
traversal(); | |
break; | |
case 2: | |
printf("\nEnter the value to be inserted: "); | |
scanf("%d", &val); | |
insert_end(val); | |
traversal(); | |
break; | |
case 3: | |
printf("\nEnter the value to be inserted: "); | |
scanf("%d", &val); | |
printf("\nEnter the element after which value to be inserted: "); | |
scanf("%d", &ele); | |
q = find(ele); | |
if (q != NULL) { | |
insert_after(q, val); | |
traversal(); | |
} else { | |
printf("\nNot found"); | |
} | |
break; | |
case 4: | |
printf("\nEnter the element to be deleted: "); | |
scanf("%d", &val); | |
p = find(val); | |
if (p != NULL) { | |
deletion(p); | |
printf("\nElements after deletion"); | |
traversal(); | |
} else { | |
printf("\nElement not found"); | |
} | |
break; | |
case 5: | |
printf("\nEnter the value to be searched: "); | |
scanf("%d", &val); | |
p = find(val); | |
if (p != NULL) | |
printf("\nElement found"); | |
else | |
printf("\nElement not found"); | |
break; | |
case 6: | |
traversal(); | |
break; | |
case 7: | |
printf("\nExiting"); | |
break; | |
default: | |
printf("\nInvalid input"); | |
break; | |
} | |
} while (choice < 7); | |
return 0; | |
} | |
void create_node(int val) { | |
struct node *p; | |
p = malloc(sizeof(struct node)); | |
p->data = val; | |
p->next = NULL; | |
head = p; | |
} | |
void insert_beg(int val) { | |
struct node *p; | |
p = malloc(sizeof(struct node)); | |
p->data = val; | |
p->next = head; | |
head = p; | |
} | |
void insert_end(int val) { | |
struct node *p, *q; | |
p = malloc(sizeof(struct node)); | |
p->data = val; | |
p->next = NULL; | |
q = head; | |
while (q->next != NULL) | |
q = q->next; | |
q->next = p; | |
} | |
void insert_after(struct node *q, int val) { | |
struct node *p; | |
p = malloc(sizeof(struct node)); | |
p->data = val; | |
p->next = q->next; | |
q->next = p; | |
} | |
void deletion(struct node *p) { | |
struct node *q; | |
if (p == head) { | |
head = p->next; | |
free(p); | |
} else { | |
q = head; | |
while (q->next->data != p->data) | |
q = q->next; | |
q->next = p->next; | |
free(p); | |
} | |
} | |
struct node *find(int val) { | |
struct node *q; | |
q = head; | |
while (q->data != val) | |
q = q->next; | |
return q; | |
} | |
void traversal() { | |
struct node *q; | |
q = head; | |
printf("\n-----List Start------\n"); | |
while (q != NULL) { | |
if (q->next == NULL) | |
printf("%d", q->data); | |
else | |
printf("%d-->", q->data); | |
q = q->next; | |
} | |
printf("\n-----List End------\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment