- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Sorted Circularly Singly Linked List
In data structure, Linked List is a linear collection of data elements. Each element or node of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to null. Into a linked list the entry point is called the head of the list.
Each node in the list stores the contents and a pointer or reference to the next node in the list, in a singly linked list. Singly linked list does not store any pointer or reference to the previous node.
As it is a sorted singly linked list, so data items in the linked list will remain sorted always.
Here is a C++ Program to implement Sorted Circularly Singly Linked List
Algorithm
Begin function createnode() to insert node in the list: It checks whether the list is empty or not. If the list is empty put the node as first element and update head. If list is not empty, It creates a newnode and inserts the number in the data field of the newnode. Now the newnode will be inserted in such a way that linked list will remain sorted. If it gets inserted at the last, then the newnode points to the head. If the newnode inserted at the first, then the linked list starts from there. End Begin function display() to print the list content having n number of nodes: Initialize c = 0. Initialize pointer variable with the start address while (c <= n) Print the node info Update pointer variable Increment c. End
Example Code
#include<iostream> using namespace std; struct nod { int d; nod *n; } *p = NULL, *head = NULL, *q = NULL, *np = NULL; int c = 0; void createnode(int n) { np = new nod; np->d = n; np->n = NULL; if (c == 0) { head = np; p = head; p->n = head; c++; } else if (c == 1) { p = head; q = p; if (np->d < p->d) { np->n = p; head = np; p->n = np; } else if (np->d > p->d) { p->n = np; np->n = head; } c++; } else { p = head; q = p; if (np->d < p->d) { np->n = p; head = np; do { p = p->n; } while (p->n != q); p->n = head; } else if (np->d > p->d) { while (p->n != head && q->d < np->d) { q = p; p = p->n; if (p->n == head) { p->n = np; np->n = head; } else if (np->d< p->d) { q->n = np; np->n = p; break; } } } } } void display(int i) { nod *t = head; int c = 0; while (c <= i ) { cout<<t->d<<"\t"; t = t->n; c++; } } int main() { int i = 0, n, a; cout<<"enter the no of nodes\n"; cin>>n; while (i < n) { cout<<"\nenter value of node\n"; cin>>a; createnode(a); i++; } cout<<"sorted circularly singly link list"<<endl; display(n); }
Output
enter the no of nodes 5 enter value of node 6 enter value of node 4 enter value of node 7 enter value of node 3 enter value of node 2 sorted circularly singly link list 2 3 4 6 7 2
- Related Articles
- C++ Program to Implement Sorted Circularly Doubly Linked List
- C++ Program to Implement Sorted Singly Linked List
- C++ Program to Implement Singly Linked List
- C++ Program to Implement Circular Singly Linked List
- C++ Program to Implement Sorted Doubly 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
- Golang Program to define a singly linked list.
- C Program to reverse each node value in Singly Linked List
- Convert singly linked list into circular linked list in C++
- Convert singly linked list into XOR linked list in C++
- C++ Program to Implement Stack using linked list
- C++ Program to Implement Queue using Linked List
- C++ Program to Implement Circular Doubly Linked List

Advertisements