C program for creating, inserting and traversing nodes in singly linked list explained.
Singly linked list using C
The text given in blue are comments
//Program for creating, inserting, and traversing nodes in a singly linked list
#
include <stdio.h>
#
include <stdlib.h>
struct
slinklist //Defining a structure slinklist for
handling node
{
int
data; //To store data in the node
struct
slinklist *next; //To store the pointer that points
to the next node or NULL
};
typedef
struct slinklist node;
node
*start = NULL; // declaring a start[a head] node
that points to NULL
node*
createNode() //Function to create a new node
{
node
* newnode; //Creates a pointer variable newnode as
a pointer to structure
node
*temp;
newnode
= (node *) malloc(sizeof(node)); //Allocates memory
for the new node
printf("\n
Enter data: ");
scanf("%d",
&newnode -> data); //Reads the value for
data part of the new node from user
newnode
-> next = NULL; //Sets the next pointer of the
new node to NULL as it is the last node
/*If the node is inserted for the first time, the head pointer
has to point the new node. Hence, we set the start pointer to point the new
node. This part of the program executed if the start node points to NULL value
*/
if(start
== NULL)
/*start is assigned with the value of newnode, ie, start will be
assigned with the address stored in the newnode*/
start = newnode;
}
void
insert_at_beg()
{
node
*newnode; //Declares a new node
newnode
= createNode(); //Calling the createNode() function
to create a new node
newnode
-> next = start; //Assign the address stored in
start to newnode.next
start
= newnode; //Assign the address stored in newnode
to start. Start points to inserted node
}
void
insert_at_end()
{
node
*newnode, *temp; //Declares newnode and temp
pointers to sturcture
newnode
= createNode(); //Call the createNode() to create a
new node
temp
= start; //address stored in start is assigned to
temp.
while(temp
-> next != NULL) //This loop executed until
reach the last node in the linked list
temp
= temp -> next; //on each iteration, temp is
assigned with the address stored in temp->next
//if the last node is found, the following statement assigns temp->next
with address of newnode.
temp
-> next = newnode;
}
void
traverse()
{
node
*temp; //Declare a new temp node pointer
temp
= start; //assigns the address stored in start (head)
node to temp
printf("\n
The contents of List (Left to Right): \n");
if(start
== NULL) //if start ==Null, there is no nodes
available. Hence, print empty list
{
printf("\n
Empty List");
return;
}
Else
//if linked list is not empty
{
while(temp != NULL) //this loop iterates through all nodes from first node and displays
data
{
printf("%d-->",
temp -> data); //prints the data store in temp
/*move the temp to next node. Following statement assigns the
address stored in temp->next to temp*/
temp
= temp -> next;
}
}
printf("
X ");
}
int
main(void)
{
int
ch, n;
while(1)
{
printf("\n
1.Create a list/node ");
printf("\n
2.Insert a node at beginning ");
printf("\n
3.Insert a node at end");
printf("\n
4.Traverse the list (Left to Right)");
printf("\n
0.Exit\n\n");
scanf("%d",
&ch);
switch(ch)
{
case
1:
if(start
== NULL)
{
createNode();
printf("\n
List created..\n");
}
else
printf("\n
List is already created..\n");
break;
case
2:
if (start == NULL)
{
printf("\n No list is
found. First create a list..\n");
break;
}
insert_at_beg();
break;
case
3:
if
(start == NULL)
{
printf("\n No list is
found. First create a list..\n");
break;
}
insert_at_end();
break;
case
4:
traverse();
break;
case
0:
exit(0);
}
}
getch();
}