Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views2 pages

Program 1

The document contains a C program that implements a singly linked list with functionalities to create, display, and delete nodes. It prompts the user to enter the number of nodes and their data, displays the list, and allows the deletion of a node at a specified position. The program includes functions for creating the list, displaying its contents, and deleting a node while managing memory allocation and deallocation.

Uploaded by

dairyone371
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Program 1

The document contains a C program that implements a singly linked list with functionalities to create, display, and delete nodes. It prompts the user to enter the number of nodes and their data, displays the list, and allows the deletion of a node at a specified position. The program includes functions for creating the list, displaying its contents, and deleting a node while managing memory allocation and deallocation.

Uploaded by

dairyone371
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

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;

struct node *newNode,*temp;


temp = head;
for(count=2;count<=n;count++)
{
newNode = (struct node*)malloc(sizeof(struct node));
printf("\n enter data in the node %d :",count);
scanf("%d",&newNode‐>data);

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‐‐‐‐‐‐‐‐‐‐‐‐");
}

You might also like