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

program1

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)
7 views2 pages

program1

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy