- Define a node comprising two members—data and next. The data member is for storing integer values and the next member is a pointer to link the nodes as follows:
struct node
{
int data;
struct node *next;
};
- Specify the number of elements in the linked list. The value entered will be assigned to the n variable as follows:
printf("How many elements are there in the linked list ?");
scanf("%d",&n);
- Execute a for loop for n number of times. Within the for loop, a node is created by the name newNode. When asked, enter an integer value to be assigned to the data member of newNode as follows:
newNode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newNode->data);
- Two pointers, startList and temp1, are set to point at the first node. The startList pointer will keep pointing at the first node of the linked list. The temp1 pointer will be used to link the nodes as follows:
startList = newNode;
temp1=startList;
- To connect the newly created nodes, the following two tasks are performed:
- The next member of temp1 is set to point at the newly created node.
- The temp1 pointer is shifted to point at the newly created node as follows:
temp1->next = newNode;
temp1=newNode;
- When the for loop gets over, we will have a singly linked list with its first node pointed at by startList, and the next pointer of the last node pointing at NULL. This linked list is ready to undergo the sorting procedure. Set a for loop to execute from 0 until n-2 that is equal to n-1 iterations as follows:
for(i=n-2;i>=0;i--)
- Within the for loop, to compare values, use two pointers, temp1 and temp2. Initially, temp1 and temp2 will be set to point at the first two nodes of the linked list, as shown in the following code snippet:
temp1=startList;
temp2=temp1->next;
- Compare the nodes pointed at by temp1 and temp2 in the following code:
if(temp1->data > temp2->data)
- After comparing the first two nodes, the temp1 and temp2 pointers will be set to point at the second and third nodes, and so on:
temp1=temp2;
temp2=temp2->next;
- The linked list has to be arranged in ascending order, so the data member of temp1 must be smaller than the data member of temp2. In case the data member of temp1 is larger than the data member of temp2, the interchanging of the values of the data members will be done with the help of a temporary variable, k, as follows:
k=temp1->data;
temp1->data=temp2->data;
temp2->data=k;
- After n-1 performing iterations of comparing and interchanging consecutive values, if the first value in the pair is larger than the second, all the nodes in the linked list will be arranged in ascending order. To traverse the linked list and to display the values in ascending order, a temporary t pointer is set to point at the node pointed at by startList, that is, at the first node of the linked list, as follows:
t=startList;
- A while loop is executed until the t pointer reaches NULL. Recall that the next pointer of the last node is set to NULL, so the while loop will execute until all the nodes of the linked list are traversed as follows:
while(t!=NULL)
- Within the while loop, the following two tasks will be performed:
- The data member of the node pointed to by the t pointer is displayed.
- The t pointer is moved further to point at its next node:
printf("%d\t",t->data);
t=t->next;
The sortlinkedlist.c program for creating a singly linked list, followed by sorting it in ascending order, is as follows:
/* Sort the linked list by bubble sort */
#include<stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *temp1,*temp2, *t,*newNode, *startList;
int n,k,i,j;
startList=NULL;
printf("How many elements are there in the linked list ?");
scanf("%d",&n);
printf("Enter elements in the linked list\n");
for(i=1;i<=n;i++)
{
if(startList==NULL)
{
newNode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newNode->data);
newNode->next=NULL;
startList = newNode;
temp1=startList;
}
else
{
newNode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newNode->data);
newNode->next=NULL;
temp1->next = newNode;
temp1=newNode;
}
}
for(i=n-2;i>=0;i--)
{
temp1=startList;
temp2=temp1->next;
for(j=0;j<=i;j++)
{
if(temp1->data > temp2->data)
{
k=temp1->data;
temp1->data=temp2->data;
temp2->data=k;
}
temp1=temp2;
temp2=temp2->next;
}
}
printf("Sorted order is: \n");
t=startList;
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->next;
}
}
Now, let's go behind the scenes.