#include <stdio.h>
#include <stdlib.h>
//Run this program on https://[Log in to view URL] compiler
struct node{
int data;
struct node *next;
};
int traverseTheNode(struct node *head);
int main() {
int n,element,option;
printf("Linked list!\n");
struct node *head,*current,*temp;
printf("How many nodes you would like to create\n");
scanf("%d",&n);
printf("enter the head node data\n");
scanf("%d",&element);
printf("Total node given %d data %d\n",n ,element);
head = (struct node *)malloc(sizeof(struct node));
head->data = element;
head->next = NULL;
temp = head;
current = head;
for(int i = 1 ;i<n;i++){
printf("enter the node data\n");
scanf("%d",&element);
temp = (struct node *)malloc(sizeof(struct node));
temp->data = element;
temp->next = NULL;
current->next = temp;
current = current->next;
}
while(1){
printf("\n 1.Traverse the node \n2.Insert at the begining \n.3Insert at the specific position \n4.Insert at the last position \n5.Delete at the start \n6.Delete the position \n7.Delete at the last \n.8.Node reversal\n");
scanf("%d",&option);
switch(option){
case 1 :
printf("Traversing the node\n");
traverseTheNode(head);
break;
case 2 :
printf("Inserting the node at the begining\n");
insertTheNodeAttheBegining(head);
break;
case 3 :
printf("Inserting the node at the position\n");
insertTheNodeAtSpecificPos(head);
break;
case 4 :
printf("Inserting the node at the end\n");
insertTheNodeAtEnd(head);
break;
default:
exit(0);
break;
}
}
return 0;
}
int traverseTheNode(struct node *head){
while(head != NULL){
printf("[%d]->",head->data);
head = head->next;
}
return 0;
}
int insertTheNodeAttheBegining(struct node *head){
int data;
printf("Enter the data you want to insert at the begining\n");
scanf("%d",&data);
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->next = head;
head = temp;
traverseTheNode(head);
}
int insertTheNodeAtSpecificPos(struct node *head){
struct node *current,*current_oneplus;
current = head;
if(current_oneplus->next != NULL){
current_oneplus = head->next;
}
current_oneplus = head->next;
int data,pos,i=0;
printf("Enter the position to insert data\n");
scanf("%d",&pos);
printf("Enter the data you want to insert \n");
scanf("%d",&data);
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
while((i != pos) & (current->next != NULL)){
i++;
current = current->next;
current_oneplus = current_oneplus->next;
}
if(current->next == NULL){
printf("Wrong position given\n");
return 0;
}
current->next = temp;
temp->next = current_oneplus;
traverseTheNode(head);
}
int insertTheNodeAtEnd(struct node *head){
int data;
struct node *current,*temp ;
current = head;
printf("Enter the data you want to insert at the end\n");
scanf("%d",&data);
while(current->next!=NULL){
current = current->next;
}
temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
current->next = temp;
traverseTheNode(head);
}
To embed this program on your website, copy the following code and paste it into your website's HTML: