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
Example Program
/* Simple Singly(Single) Linked List Example Program in C++ */
/* Data Structure C++ Programs,C++ Linked List Examples,Structure Example in C++ */
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
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;
cout << "Singly(Single) Linked List Example - Basic (Structure Example)\n";
while (loop) {
cout << "\nEnter Element for Insert Linked List (-1 to Exit ) : \n";
cin>>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;
cout << "\nDisplay Linked List : \n";
while (temp_node != 0) {
cout << "# " << temp_node->value;
count++;
temp_node = temp_node -> next;
}
cout << "\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
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming