How To Create A Linked List In Cpp
C++ Program to Implement Singly Linked List
Singly linked list is a type of data structure that is made up of nodes that are created using self referential structures. Each of these nodes contain two parts, namely the data and the reference to the next list node. Only the reference to the first list node is required to access the whole linked list. This is known as the head. The last node in the list points to nothing so it stores NULL in that part.
A program to implement singly linked list is given as follows.
Example
Live Demo
#include <iostream> using namespace std; struct Node { int data; struct Node *next; }; struct Node* head = NULL; void insert(int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = head; head = new_node; } void display() { struct Node* ptr; ptr = head; while (ptr != NULL) { cout<< ptr->data <<" "; ptr = ptr->next; } } int main() { insert(3); insert(1); insert(7); insert(2); insert(9); cout<<"The linked list is: "; display(); return 0; } Output
The linked list is: 9 2 7 1 3
In the above program, the structure Node forms the linked list node. It contains the data and a pointer to the next linked list node. This is given as follows.
struct Node { int data; struct Node *next; }; The function insert() inserts the data into the beginning of the linked list. It creates a new_node and inserts the number in the data field of the new_node. Then the new_node points to the head. Finally the head is the new_node i.e. the linked list starts from there. This is given below.
void insert(int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = head; head = new_node; } The function display() displays the whole linked list. First ptr points to head. Then it is continuously forwarded to the next node until all the data values of the nodes are printed. This is given below.
void display() { struct Node* ptr; ptr = head; while (ptr != NULL) { cout<< ptr->data <<" "; ptr = ptr->next; } } In the function main(), first various values are inserted into the linked list by calling insert(). Then the linked list is displayed. This is given below.
int main() { insert(3); insert(1); insert(7); insert(2); insert(9); cout<<"The linked list is: "; display(); return 0; }
Published on 09-Oct-2018 07:06:14
- Related Questions & Answers
- C++ Program to Implement Circular Singly Linked List
- C++ Program to Implement Sorted Singly Linked List
- C++ Program to Implement Sorted Circularly Singly Linked List
- How to implement Traversal in Singly Linked List using C#?
- C++ Program to Implement Hash Tables chaining with Singly Linked Lists
- C++ Program to Implement Doubly Linked List
- Convert singly linked list into circular linked list in C++
- Convert singly linked list into XOR linked list in C++
- Golang Program to define a singly linked list.
- C++ Program to Implement Stack using linked list
- C++ Program to Implement Queue using Linked List
- C++ Program to Implement Circular Doubly Linked List
- C++ Program to Implement Sorted Doubly Linked List
- C Program to reverse each node value in Singly Linked List
- Binary Search on Singly Linked List in C++
How To Create A Linked List In Cpp
Source: https://www.tutorialspoint.com/cplusplus-program-to-implement-singly-linked-list
Posted by: garciajacessid.blogspot.com

0 Response to "How To Create A Linked List In Cpp"
Post a Comment