// In this program we are going to perform insert data, delete data and display the data as well.
#include<stdio.h>
#include<stdlib.h>
struct list{
int data;
struct list*next;
}*head=NULL;
void insertdata(int value);
void display();
void deletedata();
int main(){
int choice,num;
while(1){ //All ways true condition
printf("1.Insert 2.Delete 3.display 4.exit\n");
printf("Enter yur choice: "); // Select the option from the above given
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter a number: "); //To push the number into the list
scanf("%d",&num);
insertdata(num);
break;
case 2:
deletedata();
break;
case 3:
display();
break;
case 4:
exit(1); // Exit from the while loop
default:
printf("Invalid option!\n\n");
exit(1); //come out from the while loop
}
}
return 0;
}
void insertdata(int value){ //Inserting the value at the beginning
struct list*newnode;
newnode=(struct list*)malloc(sizeof(struct list));
newnode->data=value; //assigning the value
if(head==NULL){
head=newnode;
newnode->next=NULL;
}else{
newnode->next=head;
head=newnode;
}
printf("Node is added to the list\n\n");
}
void deletedata(){ // Delete at beginning
struct list*temp=head;
if(head==NULL){
printf("List is empty\n");
}else{
head=temp->next;
free(temp); //deleting the first node in the list
}
printf("One node is deleted\n\n");
}
void display(){
struct list*temp=head;
printf("The data in the list is: \n");
while(temp->next!=NULL){
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d\n\n",temp->data); //Last node printing
}