Skip to content

Instantly share code, notes, and snippets.

@in1yan
Last active April 12, 2025 16:18
Show Gist options
  • Save in1yan/d5e5d68b02609649532d4a9429f7b806 to your computer and use it in GitHub Desktop.
Save in1yan/d5e5d68b02609649532d4a9429f7b806 to your computer and use it in GitHub Desktop.
menu driven list management
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
typedef struct Node{
int data;
struct Node *next;
} node;
node* create_node(int data){
node *new_node = (node *)malloc(sizeof(node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void append(node **head, int data){
node * new_node = create_node(data);
if(*head == NULL){
*head = new_node;
return;
}
node *tmp = *head;
while(tmp -> next != NULL){
tmp = tmp->next;
}
tmp->next = new_node;
}
void push(node **head, int data){
node *new_node = create_node(data);
new_node->next = *head;
*head = new_node;
}
void print_list(node *head){
if(head == NULL){
printf("The list is empty\n");
return;
}
while(head != NULL){
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
void insert_before(node **head, int target, int data){
node *new_node = create_node(data);
if(*head == NULL){
printf("The list is empty\n");
return;
}
if((*head)->data == target){
push(&(*head), data);
return;
}
node *prev = NULL, *curr = *head;
while(curr != NULL && curr->data != target){
prev = curr;
curr = curr->next;
}
if(curr == NULL){
printf("Value not found in the list\n");
printf("The linked list after insertion before a value is:\n");
print_list(*head);
free(new_node);
return;
}
prev -> next = new_node;
new_node->next = curr;
printf("The linked list after insertion before a value is:\n");
print_list(*head);
}
void insert_after(node **head, int target, int data){
node *tmp = *head;
while(tmp != NULL && tmp->data != target)
tmp = tmp->next;
if (tmp != NULL) {
node *new_node = create_node(data);
new_node->next = tmp->next;
tmp->next = new_node;
}else {
printf("Value not found in the list\n");
printf("The linked list after insertion after a value is:\n");
print_list(*head);
return;
}
printf("The linked list after insertion after a value is:\n");
print_list(*head);
}
void delete_front(node **head){
if (*head == NULL) {
printf("List is empty\n");
return;
}
node *tmp = *head;
(*head) = (*head)->next;
free(tmp);
printf("The linked list after deletion from the beginning is:\n");
print_list(*head);
}
void delete_end(node **head){
if (*head == NULL) {
printf("List is empty\n");
return;
}
if((*head)->next == NULL){
free(*head);
*head = NULL;
return;
}
node *tmp = *head, *prev = NULL;
while(tmp->next != NULL){
prev = tmp;
tmp = tmp->next;
}
prev->next = NULL;
free(tmp);
printf("The linked list after deletion from the end is:\n");
print_list(*head);
}
void delete_before(node **head, int target){
if (*head == NULL || (*head)->next == NULL || (*head)->next->next == NULL) {
printf("Value not found in the list\n");
printf("The linked list after deletion before a value is:\n");
print_list(*head);
return;
}
node *thirdLast = NULL, *secondLast = *head, *last = secondLast->next, *curr = last->next;
while (curr != NULL && curr->data != target) {
thirdLast = secondLast;
secondLast = last;
last = curr;
curr = curr->next;
}
if (curr == NULL) {
printf("Value not found in the list\n");
printf("The linked list after deletion before a value is:\n");
print_list(*head);
return;
}
if(thirdLast == NULL){
*head = secondLast->next;
free(secondLast);
}else{
thirdLast->next = last;
free(secondLast);
}
printf("The linked list after deletion before a value is:\n");
print_list(*head);
}
void delete_after(node **head, int target){
if (*head == NULL) {
printf("Value not found in the list\n");
printf("The linked list after deletion after a value is:\n");
print_list(*head);
return;
}
node *tmp = *head;
while(tmp != NULL && tmp->data != target)
tmp = tmp->next;
if (tmp == NULL || tmp->next == NULL) {
printf("Value not found in the list\n");
printf("The linked list after deletion after a value is:\n");
print_list(*head);
return;
}
node *to_delete = tmp->next;
tmp->next = tmp->next->next;
free(to_delete);
printf("The linked list after deletion after a value is:\n");
print_list(*head);
}
int main(){
node *nums = NULL;
int ch, target, value;
do {
scanf("%d", &ch);
switch (ch) {
case 1:
int n = 0;
while (true) {
scanf("%d", &n);
if(n == -1)
break;
append(&nums, n);
}
printf("LINKED LIST CREATED\n");
break;
case 2:
print_list(nums);
break;
case 3:
scanf("%d", &value);
push(&nums, value);
printf("The linked list after insertion at the beginning is:\n");
print_list(nums);
break;
case 4:
scanf("%d", &value);
append(&nums, value);
printf("The linked list after insertion at the end is:\n");
print_list(nums);
break;
case 5:
scanf("%d", &target);
scanf("%d", &value);
insert_before(&nums, target, value);
break;
case 6:
scanf("%d", &target);
scanf("%d", &value);
insert_after(&nums, target, value);
break;
case 7:
delete_front(&nums);
break;
case 8:
delete_end(&nums);
break;
case 9:
scanf("%d", &target);
delete_before(&nums, target);
break;
case 10:
scanf("%d", &target);
delete_after(&nums, target);
break;
case 11:
free(nums);
break;
default:
printf("Invalid option! Please try again\n");
}
}while (ch != 11);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment