Deleting a node in linked list: Beginning, End & Given location

Deleting a node in linked list From Beginning

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
	int data;
	struct node *next;
	};
struct node *head=NULL;
struct node* createNode(){
	struct node *newNode = (struct node *)malloc(sizeof(struct node));
	
return (newNode);
	}

void insertNode(){
	struct node *temp,*ptr;
	temp=createNode();
	printf("enter the data you want to insert:");
	scanf("%d",&temp->data);
	temp->next=NULL;
	if(head==NULL)
		head=temp;
	else{
		ptr=head;
		while(ptr->next!=NULL){
			ptr=ptr->next;
		}
	       ptr->next=temp;
	     }
    }

void deleteNodeAtBegin(){
	if(head==NULL){
		printf("list is empty");
	}
	else{
		struct node* temp=head;
		head=head->next;
		printf("Successfully Deleted %d from Beginning",temp->data);
		free(temp);
	    }
	}
void viewList(){
	struct node* temp=head;
	if(temp==NULL){
		printf("list is empty");
	}
	else{
	    printf("List is: \n");
		while(temp->next!=NULL)
		{
			printf("%d\t",temp->data);
			temp=temp->next;
		}
		printf("%d \t",temp->data);
	}
}

int menu(){
	int choice;
	printf("\n 1.Add value to the list");
	printf("\n 2.Delete node at Begining");
	printf("\n 3.Travesre/View List");
	printf("\n 4. exit");
	printf("\n Please enter your choice: \t");
	scanf("%d",&choice);
	return(choice);
}
void main(){
	while(1){
		switch(menu()){
			case 1:
				insertNode();
				break;
			case 2:
				deleteNodeAtBegin();
				break;
			case 3:
				viewList();
				break;
			case 4:
				exit(0);
			default:
				printf("invalid choice");
		}
		getch();
	}

}

Output:

Deleting a node in linked list From Beginning

Deleting a node in linked list From End

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
	int data;
	struct node *next;
	};
struct node *head=NULL;
struct node* createNode(){
	struct node *newNode = (struct node *)malloc(sizeof(struct node));
    return (newNode);
}

void insertNode(){
	struct node *temp,*ptr;
	temp=createNode();
	printf("enter the data you want to insert:");
	scanf("%d",&temp->data);
	temp->next=NULL;
	if(head==NULL)
		head=temp;
	else{
		ptr=head;
		while(ptr->next!=NULL){
			ptr=ptr->next;
		}
	    ptr->next=temp;
	}
}

void deleteNodeAtEnd(){
	struct node *temp,*secondLast;
	if(head==NULL){
		printf("list is empty");
	}
	else{
	    printf("Successfully Deleted %d from End",temp->data);
		temp=head;
		secondLast=head;
		while(temp->next!=NULL){
			secondLast=temp;
			temp=temp->next;
		}
		if(temp==head){
			head=NULL;
		}
		else{
			secondLast->next=NULL;
		}
		free(temp);
	}
}
void viewList(){
	struct node* temp=head;
	if(temp==NULL){
		printf("list is empty");
	}
	else{
	    printf("List items are: \n");
		while(temp->next!=NULL)
		{
			printf("%d\t",temp->data);
			temp=temp->next;
		}
		printf("%d \t",temp->data);
	}
}

int menu(){
	int choice;
	printf("\n 1.Add value to the list");
	printf("\n 2.Delete node at End");
	printf("\n 3.Travesre/View List");
	printf("\n 4. exit");
	printf("\n Please enter your choice: \t");
	scanf("%d",&choice);
	return(choice);
}
void main(){
	while(1){
		switch(menu()){
			case 1:
				insertNode();
				break;
			case 2:
				deleteNodeAtEnd();
				break;
			case 3:
				viewList();
				break;
			case 4:
				exit(0);
			default:
				printf("invalid choice");
		}
		getch();
	}

}

Output:

Deleting a node in linked list From End

Delete node at given position in a linked list

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
	int data;
	struct node *next;
	};
struct node *head=NULL;
struct node* createNode(){
	struct node *newNode = (struct node *)malloc(sizeof(struct node));
    return (newNode);
}

void insertNode(){
	struct node *temp,*ptr;
	temp=createNode();
	printf("enter the data you want to insert:");
	scanf("%d",&temp->data);
	temp->next=NULL;
	if(head==NULL)
		head=temp;
	else{
		ptr=head;
		while(ptr->next!=NULL){
			ptr=ptr->next;
		}
	    ptr->next=temp;
	}
}

void deleteAtLocation(){
	struct node *temp1, *temp2;
	int loc,i;
	printf("enter the node position you want to delete:");
	scanf("%d",&loc);
	temp1=head;
	for(i=0;i<loc-1;i++){
		temp2=temp1;
		temp1=temp1->next;
		if(temp1==NULL)
		{
			printf("\n There are less elements in List");
			return;
		}
	}
	temp2->next = temp1->next;
	printf("Successfully Deleted %d from location %d",temp1->data,loc);
	free(temp1);
	printf("\n deleted %d node",loc);
}
void viewList(){
	struct node* temp=head;
	if(temp==NULL){
		printf("list is empty");
	}
	else{
	    printf("List items are: \n");
		while(temp->next!=NULL)
		{
			printf("%d\t",temp->data);
			temp=temp->next;
		}
		printf("%d \t",temp->data);
	}
}

int menu(){
	int choice;
	printf("\n 1.Add value to the list");
	printf("\n 2.Delete node at given position in a linked list");
	printf("\n 3.Travesre/View List");
	printf("\n 4. exit");
	printf("\n Please enter your choice: \t");
	scanf("%d",&choice);
	return(choice);
}
void main(){
	while(1){
		switch(menu()){
			case 1:
				insertNode();
				break;
			case 2:
				deleteAtLocation();
				break;
			case 3:
				viewList();
				break;
			case 4:
				exit(0);
			default:
				printf("invalid choice");
		}
		getch();
	}

}

Output:

delete node at given position in a linked list