program1
program1
h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}*head;
void createList(int);
void displayList();
void deleteNode();
int main()
{
int n;
printf("\n‐‐‐‐‐‐‐‐‐‐program to create and display a singly linked
list‐‐‐‐‐‐‐‐‐‐‐\n");
printf("\nEnter the number of nodes ? ");
scanf("%d",&n);
createList(n);
displayList();
deleteNode();
displayList();
return 0;
}
void createList(int n)
{
int count = 1;
head = (struct node*)malloc(sizeof(struct node));
printf("\n enter data in the node %d :",count);
scanf("%d",&head‐>data);
head‐>next=NULL;
temp‐>next=newNode;
newNode‐>next=NULL;
temp=temp‐>next;
}
void displayList()
{
struct node *temp;
temp = head;
if(head==NULL)
{
printf("\n Empty List");
}
else
{
int count = 1;
while(temp!=NULL)
{
printf("\n Node %d data : %d",count,temp‐>data);
temp=temp‐>next;
count++;
}
}
}
void deleteNode()
{
printf("\n\n‐‐‐‐‐‐‐‐‐‐‐‐deleteNode() function started‐‐‐‐‐‐‐‐‐‐‐‐");
int position;
printf("\nEnter the position of the node to be deleted ? ");
scanf("%d",&position);
printf("\nDeleting node at position %d ",position);
struct node *current,*prev;
current = head;
if(position==1)
{
head = head‐>next;
free(current);
}
else
{
int count=1;
while(count<position‐1)
{
current = current‐>next;
count++;
}
prev = current;
current=current‐>next;
prev‐>next=current‐>next;
free(current);
}
printf("\n‐‐‐‐‐‐‐‐‐‐‐‐deleteNode() function ended‐‐‐‐‐‐‐‐‐‐‐‐");
}