Linked List

 

#include<stdio.h>
#include<stdlib.h>

#define red     "\x1b[31m"
#define RESET   "\x1b[0m"

struct node{
  int data;
  struct node *link;
};

char choice;
void quit(){
    printf("Back to menu? (y/n):");
    scanf(" %c",&choice);
}

// Count nodes
int count_of_node(struct node *head){
  int count = 0;
  if(head==NULL){
    printf("Linked List is empty\n");
    return 0;
  }
  struct node *ptr = head;
  while(ptr!=NULL){
    count++;
    ptr = ptr->link;
  }
  printf("Number of nodes: %d\n",count);
  // quit();
  return count;
}

// Traverse the linked list
void traverse(struct node *head){
  if(head==NULL){
    printf("Linked List is empty\n");
    return;
  }
  struct node *ptr = head;
  printf("Linked List elements: ");
  while(ptr!=NULL){
    printf("%d ",ptr->data);
    ptr = ptr->link;
  }
  printf("\n");
  quit();
 
}

// Append at the end
void append_end(struct node *head){
  struct node *ptr, *temp;
  ptr = head;
  temp = malloc(sizeof(struct node));
  printf("Enter the data: ");
  scanf("%d",&temp->data);
  temp->link = NULL;
  while(ptr->link != NULL){
    ptr = ptr->link;
  }
  ptr->link = temp;
  quit();

}

// Append at the start
void append_start(struct node **head){
  struct node *ptr = malloc(sizeof(struct node));
  printf("Enter the data: ");
  scanf("%d",&ptr->data);
  ptr->link = *head;
  *head = ptr;
  quit();

}

// Insert at specific location
void insert_at_nth(struct node *head, struct node **h){
  struct node *ptr = head, *temp;
  int n, cnt;
  temp = malloc(sizeof(struct node));
  printf("Desired index of the data: ");
  scanf("%d",&n);
  if(n == 0){
    append_start(h);
    return;
  }
  if(n > (count_of_node(head)) - 1){
    printf(red"!!Invalid Argument!!\n"RESET);
    return;
  }
  printf("Enter the data: ");
  scanf("%d",&temp->data);

  for(cnt = 1; cnt < n; cnt++){
    ptr = ptr->link;
  }
  temp->link = ptr->link;
  ptr->link = temp;
  quit();
}

// Delete at the end
void del_end(struct node *head){
  struct node *ptr = head;

  while((ptr->link->link) != NULL){
    ptr = ptr->link;
  }

  free(ptr->link);
  ptr->link = NULL;
  quit();
 
}

// Delete at start
void del_start(struct node **head){
  struct node *temp = *head;
  *head = (*head)->link;
  free(temp);
  quit();

}

// Deletion at specific location
void del_nth(struct node *head, struct node **h){
  struct node *ptr = head, *temp;
  int n;
  printf("Please enter an index to delete: ");
  scanf("%d", &n);

  if(n == 0){
    del_start(h);
    return;
  }
 
  if(n > (count_of_node(head)) - 1){
    printf(red"!!Invalid Argument!!\n"RESET);
    return;
  }

  for(int i = 1; i < n; i++){
    ptr = ptr->link;
  }
  temp = ptr->link;
  ptr->link = ptr->link->link;
  free(temp);
  quit();

}

int main(){
  struct node *head = NULL;
  head = malloc(sizeof(struct node));
  head->data = 45;
  head->link = NULL;

  struct node *current = malloc(sizeof(struct node));
  current->data = 98;
  current->link = NULL;
  head->link = current;

  current = malloc(sizeof(struct node));
  current->data = 3;
  current->link = NULL;
  head->link->link = current;

  int exit_flag = 0;

  do{
    printf("Menu:\n");
    printf("1. Traverse\n");
    printf("2. Append at the end\n");
    printf("3. Append at the start\n");
    printf("4. Insert at specific position\n");
    printf("5. Delete at the end\n");
    printf("6. Delete at the start\n");
    printf("7. Delete at specific position\n");
    printf("8. Count nodes\n");
    printf("9. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch(choice){
      case 1:
        traverse(head);
        break;
      case 2:
        if(head == NULL){
          head = malloc(sizeof(struct node));
          printf("Enter the data: ");
          scanf("%d", &head->data);
          head->link = NULL;
        } else {
          append_end(head);
        }
        break;
      case 3:
        append_start(&head);
        break;
      case 4:
        insert_at_nth(head, &head);
        break;
      case 5:
        if(head == NULL || head->link == NULL){
          free(head);
          head = NULL;
        } else {
          del_end(head);
        }
        break;
      case 6:
        if(head != NULL)
          del_start(&head);
        break;
      case 7:
        if(head != NULL)
          del_nth(head, &head);
        break;
      case 8:
        count_of_node(head);
        break;
      case 9:
        exit_flag = 1;
        break;
      default:
        printf("Invalid choice! Please try again.\n");
    }
  }
  while(choice == 'y' || choice == 'Y');

  return 0;
}

Comments