Simple Singly Linked List Example Program Using functions in C++
On this page (6sections)
About this program
This is an example program in linked list. Read the concept first: Pointer Example Program in C++, then study the code and output below.
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 using functions
/* Simple Singly(Single) Linked List Example Program Using Functions 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;
};
void insert(int);
void display();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0;
int main() {
int loop = 1;
int data;
first_node = 0;
cout << "Singly(Single) Linked List Example Using Functions - Basic (Structure Example)\n";
while (loop) {
cout << "\nEnter Element for Insert Linked List (-1 to Exit ) : \n";
cin>>data;
if (data >= 0) {
insert(data);
} else {
loop = 0;
temp_node->next = 0;
}
}
display();
return 0;
}
void insert(int data) {
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);
}
void display() {
int count = 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;
}
Sample Output
Singly(Single) Linked List Example - Using Functions
Enter Element for Insert Linked List (-1 to Exit ) :
555
Enter Element for Insert Linked List (-1 to Exit ) :
444
Enter Element for Insert Linked List (-1 to Exit ) :
333
Enter Element for Insert Linked List (-1 to Exit ) :
222
Enter Element for Insert Linked List (-1 to Exit ) :
111
Enter Element for Insert Linked List (-1 to Exit ) :
-1
Display Linked List :
# 555 # # 444 # # 333 # # 222 # # 111 #
No Of Items In Linked List : 5
------------------
(program exited with code: 0)
Related Pages
Learn the concept first, then study the code:
- Data Structures — Browse all Data Structures.
- Pointer Example Program in C++ — Concept — pointers link nodes in a list.
- Simple Singly Linked List Example Program in C++ — More in linked list.
- Singly Linked List Example Program in C++ — More in linked list.