Week 4 programs to implement singly linked-list list operations

Source Code

#include <stdio.h>

#include<stdlib.h>

struct node {
  int data;
  struct node * next;
}* head, * p, * q;

int main() {
  int ch;
  creation();
  print(head);
  printf("=============================enter your choice=============================");
  printf("\\n 1. Insertion at Beginning\\n 2. Insertion at Mid\\n 3. Insertion at the End\\n 4. Deletion at Start\\n 5. Deletion at Mid\\n 6. Deletion at End\\n");
  scanf("%d", & ch);
  switch (ch) {
  case 1:
    insertion_beg(head);
    break;
  case 2:
    insertion_mid(head);
    break;
  case 3:
    insertion_last(head);
    break;
  case 4:
    del_beg(head);

    break;
  case 5:
    del_mid(head);
    break;
  case 6:
    del_end(head);
    break;
  }
  printf("=================================================================== ========");
}
void creation() {
  int choice;
  printf("=====================Linked List Creation=====================\\n");
  printf("enter the number of elements you want to add ");
  scanf("%d", & choice);
  for (int i = 0; i < choice; i++) {
    p = (struct node * ) malloc(sizeof(struct node));
    printf("enter the data\\t");
    scanf("%d", & p -> data);
    p -> next = NULL;
    if (head == NULL) {
      head = p;
      q = p;
    } else {
      q -> next = p;
      q = p;
    }
  }
  printf("=========================================================\\n");
}
void print(struct node * start) {
  printf("The linked list is:\\n");

  q = start;
  while (q != NULL) {
    printf("%d \\n", q -> data);
    q = q -> next;
  }
}
void insertion_mid(struct node * start) {
  struct node * newnode, * temp;
  int i = 1, pos;
  printf("enter the position where you want to add the data\\t");
  scanf("%d", & pos);
  newnode = (struct node * ) malloc(sizeof(struct node));
  temp = start;
  while (i < pos) {
    temp = temp -> next;
    i++;
  }
  printf("enter the data you want to insert ");
  scanf("%d", & newnode -> data);
  newnode -> next = temp -> next;
  temp -> next = newnode;
  print(start);
}
void insertion_beg(struct node * start) {
  struct node * newnode, * temp;
  newnode = (struct node * ) malloc(sizeof(struct node));
  printf("enter the data you want to add at the START");
  scanf("%d", & newnode -> data);
  newnode -> next = start;
  start = newnode;
  print(start);
}
void insertion_last(struct node * start) {
  struct node * newnode, * temp;
  newnode = (struct node * ) malloc(sizeof(struct node));

  printf("Enter the value you want to insert at the END ");
  scanf("%d ", & newnode -> data);
  newnode -> next = NULL;
  temp = start;
  if (start == NULL)
    start = newnode;
  else {
    temp -> next = newnode;
  }
  print(start);
}
void del_beg(struct node * start) {
  struct node * temp;
  temp = start;
  start = start -> next;
  free(temp);
  print(start);
}
void del_end(struct node * start) {
  struct node * prenode, * temp;
  temp = start;
  while (temp -> next != NULL) {
    prenode = temp;
    temp = temp -> next;
  }
  if (temp == start) head = NULL;
  else prenode -> next = NULL;
  free(temp);
  while (temp -> next != NULL) {
    temp = temp -> next;
  }

  print(start);
}
void del_mid(struct node * start) {
  struct node * temp, * nextnode;
  int pos, i;
  temp = start;
  printf("Enter the position ");
  scanf("%d", & pos);
  while (i < pos - 1) {
    temp = temp -> next;
    i++;
  }
  nextnode = temp -> next;
  temp -> next = nextnode -> next;
  free(nextnode);
  print(start);
}