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&amp;lt;conio.h&amp;gt;
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
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",&amp;amp;temp-&amp;gt;data);
	temp-&amp;gt;next=NULL;
	if(head==NULL)
		head=temp;
	else{
		ptr=head;
		while(ptr-&amp;gt;next!=NULL){
			ptr=ptr-&amp;gt;next;
		}
	    ptr-&amp;gt;next=temp;
	}
}

void deleteNodeAtEnd(){
	struct node *temp,*secondLast;
	if(head==NULL){
		printf("list is empty");
	}
	else{
	    printf("Successfully Deleted %d from End",temp-&amp;gt;data);
		temp=head;
		secondLast=head;
		while(temp-&amp;gt;next!=NULL){
			secondLast=temp;
			temp=temp-&amp;gt;next;
		}
		if(temp==head){
			head=NULL;
		}
		else{
			secondLast-&amp;gt;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-&amp;gt;next!=NULL)
		{
			printf("%d\t",temp-&amp;gt;data);
			temp=temp-&amp;gt;next;
		}
		printf("%d \t",temp-&amp;gt;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",&amp;amp;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&amp;lt;conio.h&amp;gt;
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
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",&amp;amp;temp-&amp;gt;data);
	temp-&amp;gt;next=NULL;
	if(head==NULL)
		head=temp;
	else{
		ptr=head;
		while(ptr-&amp;gt;next!=NULL){
			ptr=ptr-&amp;gt;next;
		}
	    ptr-&amp;gt;next=temp;
	}
}

void deleteAtLocation(){
	struct node *temp1, *temp2;
	int loc,i;
	printf("enter the node position you want to delete:");
	scanf("%d",&amp;amp;loc);
	temp1=head;
	for(i=0;i&amp;lt;loc-1;i++){
		temp2=temp1;
		temp1=temp1-&amp;gt;next;
		if(temp1==NULL)
		{
			printf("\n There are less elements in List");
			return;
		}
	}
	temp2-&amp;gt;next = temp1-&amp;gt;next;
	printf("Successfully Deleted %d from location %d",temp1-&amp;gt;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-&amp;gt;next!=NULL)
		{
			printf("%d\t",temp-&amp;gt;data);
			temp=temp-&amp;gt;next;
		}
		printf("%d \t",temp-&amp;gt;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",&amp;amp;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