Simple Singly Linked List Example Program in C
Definition:
linked list is a linear collection of data elements, called nodes, each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration. More complex variants add additional links, allowing efficient insertion or removal from arbitrary element references
Simple Singly Linked List Example Program:
/* Simple Singly(Single) Linked List Example Program in C*/
/* Data Structure Programs,C Linked List Examples */
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
int main() {
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0;
int count = 0;
int loop = 1;
first_node = 0;
int data;
printf("Singly(Single) Linked List Example - Basic\n");
while (loop) {
printf("\nEnter Element for Insert Linked List (-1 to Exit ) : \n");
scanf("%d", &data);
if (data >= 0) {
temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
temp_node->value = data;
if (first_node == 0) {
first_node = temp_node;
} else {
head_node->next = temp_node;
}
head_node = temp_node;
fflush(stdin);
} else {
loop = 0;
temp_node->next = 0;
}
}
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d", count);
return 0;
}
Sample Output:
Singly(Single) Linked List Example - Basic
Enter Element for Insert Linked List (-1 to Exit ) :
500
Enter Element for Insert Linked List (-1 to Exit ) :
400
Enter Element for Insert Linked List (-1 to Exit ) :
300
Enter Element for Insert Linked List (-1 to Exit ) :
200
Enter Element for Insert Linked List (-1 to Exit ) :
100
Enter Element for Insert Linked List (-1 to Exit ) :
-1
Display Linked List :
# 500 # # 400 # # 300 # # 200 # # 100 #
No Of Items In Linked List : 5
------------------
(program exited with code: 0)
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )