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 an element to insert at end of list:");
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("LinkedList is empty");
}
else{
printf("Elements of Linked List: \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.Insert at end of Linked 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(){
printf("**C Program to Delete a node in linked list From Beginning**");
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:
**C Program to Delete a node in linked list From Beginning**
1.Insert at end of Linked List
2.Delete node at Beginning
3.Travesre/View List
4.Exit
Please enter your choice: 1
Enter an element to insert at end of list:4
1.Insert at end of Linked List
2.Delete node at Beginning
3.Travesre/View List
4.Exit
Please enter your choice: 1
Enter an element to insert at end of list:7
1.Insert at end of Linked List
2.Delete node at Beginning
3.Travesre/View List
4.Exit
Please enter your choice: 1
Enter an element to insert at end of list:2
1.Insert at end of Linked List
2.Delete node at Beginning
3.Travesre/View List
4.Exit
Please enter your choice: 3
Elements of Linked List:
4 7 2
1.Insert at end of Linked List
2.Delete node at Beginning
3.Travesre/View List
4.Exit
Please enter your choice: 2
Successfully Deleted 4 from Beginning
1.Insert at end of Linked List
2.Delete node at Beginning
3.Travesre/View List
4.Exit
Please enter your choice: 3
Elements of Linked List:
7 2
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 an element to insert in list: ");
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\n");
return;
}
temp = head;
secondLast = head;
while (temp->next != NULL) {
secondLast = temp;
temp = temp->next;
}
printf("Successfully deleted %d from end\n", temp->data);
if (temp == head) {
head = NULL;
} else {
secondLast->next = NULL;
}
free(temp);
}
void viewList() {
struct node* temp = head;
if (temp == NULL) {
printf("List is empty\n");
} else {
printf("Elements of Linked List:\n");
while (temp->next != NULL) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("%d\t", temp->data);
}
}
int menu() {
int choice;
printf("\n1. Insert at end of Linked List");
printf("\n2. Delete node at End");
printf("\n3. Traverse/View List");
printf("\n4. Exit");
printf("\nPlease enter your choice:\t");
scanf("%d", &choice);
return choice;
}
void main() {
printf("**C Program to Delete a node in linked list From End**");
while (1) {
switch (menu()) {
case 1:
insertNode();
break;
case 2:
deleteNodeAtEnd();
break;
case 3:
viewList();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
getch();
}
}
Output:
**C Program to Delete a node in linked list From End**
1. Insert at end of Linked List
2. Delete node at End
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter an element to insert in list: 4
1. Insert at end of Linked List
2. Delete node at End
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter an element to insert in list: 3
1. Insert at end of Linked List
2. Delete node at End
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter an element to insert in list: 2
1. Insert at end of Linked List
2. Delete node at End
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter an element to insert in list: 7
1. Insert at end of Linked List
2. Delete node at End
3. Traverse/View List
4. Exit
Please enter your choice: 3
Elements of Linked List:
4 3 2 7
1. Insert at end of Linked List
2. Delete node at End
3. Traverse/View List
4. Exit
Please enter your choice: 2
Successfully deleted 7 from end
1. Insert at end of Linked List
2. Delete node at End
3. Traverse/View List
4. Exit
Please enter your choice: 3
Elements of Linked List:
4 3 2
1. Insert at end of Linked List
2. Delete node at End
3. Traverse/View List
4. Exit
Please enter your choice:
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 an element to insert in list: ");
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 = head, *temp2;
int loc, i;
if (head == NULL) {
printf("List is empty\n");
return;
}
printf("Enter the node position you want to delete: ");
scanf("%d", &loc);
if (loc == 1) {
head = head->next;
printf("Successfully Deleted %d from location 1\n", temp1->data);
free(temp1);
return;
}
for (i = 0; i < loc - 1; i++) {
temp2 = temp1;
temp1 = temp1->next;
if (temp1 == NULL) {
printf("\nThere are fewer elements in the list\n");
return;
}
}
temp2->next = temp1->next;
printf("Successfully Deleted %d from location %d\n", temp1->data, loc);
free(temp1);
}
void viewList() {
struct node* temp = head;
if (temp == NULL) {
printf("List is empty\n");
} else {
printf("Elements of Linked List:");
while (temp != NULL) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
}
int menu() {
int choice;
printf("\n1. Insert at end of Linked List");
printf("\n2. Delete node at given position in a linked list");
printf("\n3. Traverse/View List");
printf("\n4. Exit");
printf("\nPlease enter your choice:\t");
scanf("%d", &choice);
return choice;
}
void main() {
printf("**C Program to Delete a node in linked list at Given location**");
while (1) {
switch (menu()) {
case 1:
insertNode();
break;
case 2:
deleteAtLocation();
break;
case 3:
viewList();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
getch();
}
}
Output:
**C Program to Delete a node in linked list at Given location**
1. Insert at end of Linked List
2. Delete node at given position in a linked list
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter an element to insert in list: 4
1. Insert at end of Linked List
2. Delete node at given position in a linked list
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter an element to insert in list: 3
1. Insert at end of Linked List
2. Delete node at given position in a linked list
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter an element to insert in list: 8
1. Insert at end of Linked List
2. Delete node at given position in a linked list
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter an element to insert in list: 7
1. Insert at end of Linked List
2. Delete node at given position in a linked list
3. Traverse/View List
4. Exit
Please enter your choice: 3
Elements of Linked List:4 3 8 7
1. Insert at end of Linked List
2. Delete node at given position in a linked list
3. Traverse/View List
4. Exit
Please enter your choice: 2
Enter the node position you want to delete: 2
Successfully Deleted 3 from location 2
1. Insert at end of Linked List
2. Delete node at given position in a linked list
3. Traverse/View List
4. Exit
Please enter your choice: 3
Elements of Linked List:4 8 7
What did you think?