
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert singly linked list into circular linked list in C++
In this tutorial, we will be discussing a program to convert a singly linked list into circular linked list.
For this we will be provided with a singly linked list. Our task is to take the elements of that list and get it converted into a circular linked list.
Example
#include <bits/stdc++.h> //node structure of linked list struct Node { int data; struct Node* next; }; //converting singly linked list //to circular linked list struct Node* circular(struct Node* head){ struct Node* start = head; while (head->next != NULL) head = head->next; //assigning start to the head->next node //if head->next points to NULL head->next = start; return start; } void push(struct Node** head, int data){ //creation of new node struct Node* newNode = (struct Node*)malloc (sizeof(struct Node)); //putting data in new node newNode->data = data; newNode->next = (*head); (*head) = newNode; } //displaying the elements of circular linked list void print_list(struct Node* node){ struct Node* start = node; while (node->next != start) { printf("%d ", node->data); node = node->next; } printf("%d ", node->data); } int main(){ struct Node* head = NULL; push(&head, 15); push(&head, 14); push(&head, 13); push(&head, 22); push(&head, 17); circular(head); printf("Display list: \n"); print_list(head); return 0; }
Output
Display list: 17 22 13 14 15
- Related Articles
- Convert singly linked list into XOR linked list in C++
- Singly Linked List as Circular in Javascript
- Python Program to Convert a given Singly Linked List to a Circular List
- C++ Program to Implement Circular Singly Linked List
- Difference between Singly linked list and Doubly linked list in Java
- Find minimum and maximum elements in singly Circular Linked List in C++
- Check if a linked list is Circular Linked List in C++
- Insert into a Sorted Circular Linked List in C++
- Python Circular Linked List Program
- C++ Program to Implement Singly Linked List
- Binary Search on Singly Linked List in C++
- Remove elements from singly linked list in JavaScript
- C++ Program to Implement Sorted Singly Linked List
- Golang Program to define a singly linked list.
- Convert an Array to a Circular Doubly Linked List in C++

Advertisements