#include <stdio.h>
#include <stdlib.h>
/* Node structure */
struct node
{
int data; // Data
struct node *next; // Address
} *head;
/* Function declaration */
void createList(int n);
void deleteFirstByKey(int key);
void displayList();
/**
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = malloc(sizeof(struct node));
/*
* Unable to allocate memory, hence exit from app.
*/
if (head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
/* Input head node data from user */
printf("Enter data of node 1: ");
scanf("%d", &data);
head->data = data; // Link data field with data
head->next = NULL; // Link address field to NULL
temp = head;
/*
* Create n nodes and add to the list
*/
for (i = 2; i <= n; i++)
{
newNode = malloc(sizeof(struct node));
/* If memory is not allocated for newNode */
if (newNode == NULL)
{
printf("Unable to allocate memory");
exit(0);
}
printf("Enter data of node %d: ", i);
scanf("%d", &data);
newNode->data = data; // Link data field of newNode
newNode->next = NULL; // The newNode should point to nothing
temp->next = newNode; // Link previous node i.e. temp to the newNode
temp = temp->next;
}
}
/**
* Display entire list
*/
void displayList()
{
struct node *temp;
/*
* If the list is empty i.e. head = NULL,
* dont perform any action and return.
*/
if (head == NULL)
{
printf("List is empty.\n");
return;
}
temp = head;
while (temp != NULL)
{
printf(" %d ", temp->data);
temp = temp->next; // Move to next node
}
printf("\n");
}
/**
* Delete first node by key from the list.
*/
void deleteFirstByKey(int key)
{
struct node *prev, *cur;
/* Check if head node contains key */
while (head != NULL && head->data == key)
{
// Get reference of head node
prev = head;
// Adjust head node link
head = head->next;
// Delete prev since it contains reference to head node
free(prev);
// No need to delete further
return;
}
prev = NULL;
cur = head;
/* For each node in the list */
while (cur != NULL)
{
// Current node contains key
if (cur->data == key)
{
// Adjust links for previous node
if (prev != NULL)
prev->next = cur->next;
// Delete current node
free(cur);
// No need to delete further
return;
}
prev = cur;
cur = cur->next;
}
}
int main()
{
int n, key;
// Input node count to create
printf("Enter number of node to create: ");
scanf("%d", &n);
createList(n);
// Display list
printf("\nData in list before deletion\n");
displayList();
printf("\nEnter element to delete with key: ");
scanf("%d", &key);
// Call function to delete first element by key
deleteFirstByKey(key);
// Print final list
printf("\nData in list after deletion\n");
displayList();
return 0;
}
Output
Enter number of node to create: 5
Enter data of node 1: 10
Enter data of node 2: 20
Enter data of node 3: 30
Enter data of node 4: 40
Enter data of node 5: 50
Data in list before deletion
10 20 30 40 50
Enter element to delete with key: 40
Data in list after deletion
10 20 30 50
0 Comments