Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/* INSERTING AND DELETING NODES USING LINKED LIST *\
// Online C compiler to run C program online
#include<stdio.h>
#include<stdlib.h>
struct node //node creation
{
int data;
struct node *next;
};
int main()
{
int choice;
struct node *temp=NULL;
struct node *head; //node declaration
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
one = malloc(sizeof(struct node)); //node memory allocation
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
printf("\n Enter one->data value ="); //Ask the value from user
scanf("%d",&one->data );
printf("\n Enter two->data value =");
scanf("%d",&two->data );
printf("\n Enter three->data value =");
scanf("%d",&three->data );
one->next = two; //connecting the nodes
two->next = three;
three->next = NULL;
head = one;
printbala(head); //caller function
printf("\n");
//INSERTING NEW NODE
struct node *four = NULL;
four = malloc(sizeof(struct node));
printf("\n Enter one->data value =");
scanf("%d",&one->data );
printf("\n Enter two->data value =");
scanf("%d",&two->data );
printf("\n Enter three->data value =");
scanf("%d",&three->data );
printf("\n Enter four->data value =");
scanf("%d",&four->data );
four->next = NULL;
three->next = four;
printbala(head);
printf("\n");
//DELETING NODE FOR USER CHOICE
printf("\n 1.To delete first node,2.To delete second node,3.To delete third node,4.To delete end node");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=head;
head=two;
free(temp);
printbala(head);
break;
case 2:
temp=two;
one->next=three;
free(temp);
printbala(head);
break;
case 3:
temp=three;
two->next=four;
free(temp);
printbala(head);
break;
case 4:
temp=four;
three->next=NULL;
free(temp);
printbala(head);
break;
}
}
void printbala(struct node *a) //Called function
{
while (a!=NULL)
{
printf("%d",a->data);
a=a->next;
}
return 0;
}