Singly Linked List

Implement List data structure singly linked list.

Source Code:
#include<stdio.h>
#include<stdlib.h>

void create();
void insert();
void ins_begin();
void ins_end();
void ins_atgiven();
void delet();
void del_begin();
void del_end();
void del_givennode();
void traverse();

struct node{
  int data;
  struct node *next;
};
struct node *head=NULL;

int main(){
  int opt;
  printf("::Single Linked List Operations::\n1.Creation\n2.Insertion\n3.Deletion\n4.Traversing\n5.Exit");
  do{
    printf("\nSelect operation:\n");
    scanf("%d",&opt);
    switch(opt){
      case 1:
        create();
        break;
      case 2:
        insert();
        break;
      case 3:
        delet();
        break;
      case 4:
        traverse();
        break;
      case 5:
        exit(0);
        break;
      default:
        printf("Invalid operation.\n");
    }
  }while(opt!=5);
  return 0;
}

void create(){
  int ch;
  struct node *newNode,*lastNode;
  lastNode=(struct node*)malloc(sizeof(struct node*));
  do{
    newNode=(struct node*)malloc(sizeof(struct node*));
    printf("Enter node value:\n");
    scanf("%d",&newNode->data);
    if(head==NULL)
      head=newNode;
    lastNode->next=newNode;
    lastNode=newNode;
    printf("To continue press 1 otherwise 0:\n");
    scanf("%d",&ch);
  }while(ch==1);
  lastNode->next=NULL;
}

void insert(){
  int iopt;
  printf("::Insertion Options::\n1. Insert at Begin\n2. Insert at End\n3. Insert at Given Node\n");
  printf("Where you want to insert:\n");
  scanf("%d",&iopt);
  switch(iopt){
    case 1:
      ins_begin();
      break;
    case 2:
      ins_end();
      break;
    case 3:
      ins_atgiven();
      break;
    default:
      printf("Wrong insert option.\n");
  }
}

void ins_begin(){
  struct node *newNode;
  if(head==NULL){
    printf("List is empty.\n");
  }else{
    newNode=(struct node*)malloc(sizeof(struct node));
    printf("Enter new node:\n");
    scanf("%d",&newNode->data);
    newNode->next=head;
    head=newNode;
  }
}

void ins_end(){
  struct node *newNode,*temp;
  if(head==NULL){
    printf("List is empty.\n");
  }else{
    newNode=(struct node*)malloc(sizeof(struct node));
    printf("Enter new node:\n");
    scanf("%d",&newNode->data);
    temp=head;
    while(temp->next!=NULL)
      temp=temp->next;
    temp->next=newNode;
    newNode->next=NULL;
  }
}

void ins_atgiven(){
  int nodeval;
  struct node *newNode,*temp;
  if(head==NULL){
    printf("List is empty.\n");
  }else{
    printf("Enter node value:\n");
    scanf("%d",&nodeval);
    newNode=(struct node*)malloc(sizeof(struct node));
    printf("Enter new node:\n");
    scanf("%d",&newNode->data);
    temp=head;
    while(temp->data!=nodeval)
      temp=temp->next;
    if(temp->data==nodeval){
      newNode->next=temp->next;
      temp->next=newNode;
    }
  }
}

void delet(){
  int dopt;
  printf("::Deletion Option::\n1. Delete at Begin\n2. Delete at End\n3. Delete the Given Node\n");
  printf("Where you want to delete:\n");
  scanf("%d",&dopt);
  switch(dopt){
    case 1:
      del_begin();
      break;
    case 2:
      del_end();
      break;
    case 3:
      del_givennode();
      break;
    default:
      printf("Wrong delete option.\n");
  }
}

void del_begin(){
  struct node *temp;
  if(head==NULL){
    printf("List is empty.\n");
  }else{
    temp=head;
    head=head->next;
    free(temp);
  }
}

void del_end(){
  struct node *ptr1,*ptr2;
  if(head==NULL){
    printf("List is empty.\n");
  }else{
    ptr2=head;
    while(ptr2->next!=NULL){
      ptr1=ptr2;
      ptr2=ptr2->next;
    }
    free(ptr2);
    ptr1->next=NULL;
  }
}

void del_givennode(){
  int nodeval;
  struct node *ptr1,*ptr2;
  if(head==NULL){
    printf("List is empty.");
  }else{
    printf("Enter node value:\n");
    scanf("%d",&nodeval);
    ptr2=head;
    while(ptr2->next!=NULL && ptr2->data!=nodeval){
      ptr1=ptr2;
      ptr2=ptr2->next;
    }
    if(ptr2->data==nodeval){
      ptr1->next=ptr2->next;
      free(ptr2);
    }else{
      printf("Node value not found in list.\n");
    }
  }
}

void traverse(){
  struct node *ptr;
  if(head==NULL){
    printf("List is empty.\n");
  }else{
    ptr=head;
    while(ptr!=NULL){
      printf("%d->",ptr->data);
      ptr=ptr->next;
    }
    printf("NULL");
  }
}
::Single Linked List Operations::
1.Creation
2.Insertion
3.Deletion
4.Traversing
5.Exit
Select operation:
1
Enter node value:
5
To continue press 1 otherwise 0:
1
Enter node value:
10
To continue press 1 otherwise 0:
1
Enter node value:
15
To continue press 1 otherwise 0:
0

Select operation:
4
5->10->15->NULL
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
1
Enter new node:
1

Select operation:
4
1->5->10->15->NULL
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
2
Enter new node:
20

Select operation:
4
1->5->10->15->20->NULL
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
3
Enter node value:
10
Enter new node:
12

Select operation:
4
1->5->10->12->15->20->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
1

Select operation:
4
5->10->12->15->20->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
2

Select operation:
4
5->10->12->15->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
3
Enter node value:
12

Select operation:
4
5->10->15->NULL
Select operation:
5