You are reading the article Circular Linked List In C++ updated in December 2023 on the website Moimoishop.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Circular Linked List In C++
Introduction to Circular linked list in C++Web development, programming languages, Software testing & others
Syntax
Let’s see the declaration syntax of the circular linked list as follows,
struct Node { int data_; struct Node *next_; };To implementing the linked list to maintain the pointer “last”, this links to the last node in the circular linked list.
How Circular linked list works in C++?The circular linked list is a variant of a linked list, where nodes are linked within several formats to form a circular chain. In this, the next pointer of the last node is not null; it contains the addresses of the initial node to form a circular chain.
Insertion of the ListIn the circular linked list, there are of three insertion operations were performed; they are
Inserting_at_beginning
Inserting_at_end
Inserting_at_specificNode
Deletion of the ListIn the circular linked list, there are of three deletion operations were performed; they are
Deleting_at_beginning
Deleting_at_end
Deleting_at_specificNode
Traversing Circular Linked ListTraversing a list is nothing but displaying the elements of a circular linked list; the display process is as follows,
if (last_ == NULL) { cout << "Circular linked List is Empty." << endl; return; } do { cout<<"nn"; }
First to check whether the list is empty that is head = = NULL
If the list is empty, it displays as “List is Empty”, and it exits the function
If the list is not empty, it defines a pointer ‘temp’ and initializes the head node. It keeps displaying the data as temp data until temp reaches the last node.
Finally, it displays temp data with a pointer to head data.
Examples of Circular linked list in C++Let’s see the Circular linked list with several operations such as insertion, deletion, and traversal of the list programmatically as follows,
using namespace std; struct Node { int data; struct Node *next; }; struct Node *Inserting_At_Empty(struct Node *last_, int new_data) { if (last_ != NULL) return last_; struct Node *temp = new Node; last_ = temp; return last_; } struct Node *Inserting_At_Begin(struct Node *last_, int new_data) { if (last_ == NULL) return Inserting_At_Empty(last_, new_data); struct Node *temp = new Node; return last_; } struct Node *Inserting_At_End(struct Node *last_, int new_data) { if (last_ == NULL) return Inserting_At_Empty(last_, new_data); struct Node *temp = new Node; last_ = temp; return last_; } struct Node *Inserting_After(struct Node *last_, int new_data, int after_item) { if (last_ == NULL) return NULL; struct Node *temp, *p; do { { temp = new Node; if (p == last_) last_ = temp; return last_; } cout << “Node Data “<<after_item << ” is not present in the list.” << endl; return last_; } void Traversing_List(struct Node *last_) { struct Node *p; if (last_ == NULL) { cout << “Circular linked List is Empty.” << endl; return; } do { cout<<“nn”; } void Deleting_Node(Node** head, int key) { if (*head == NULL) return; free(*head); *head=NULL; } Node *last_=*head,*d; free(*head); } } cout<<“Deletion Processn”; cout<<“Node-Data ” <<key<<” Deleted from list”<<endl; free(d); cout<<endl; cout<<“Once deletion done, the Circular linked list “<<key<<” as follows:”<<endl; Traversing_List(last_); } else cout<<“Node-Data”<< key << ” not in the list”<<endl; } int main() { struct Node *last_ = NULL; last_ = Inserting_At_Empty(last_, 25); last_ = Inserting_At_Begin(last_, 55); last_ = Inserting_At_Begin(last_, 65); last_ = Inserting_At_End(last_, 15); last_ = Inserting_At_End(last_, 75); last_ = Inserting_After(last_, 55,75 ); cout<<“Circular Linked Listn”; cout<<“Circular Linked List is Created:n”<<endl; Traversing_List(last_); Deleting_Node(&last_,15); return 0; }
When a node is not present in the list
Recommended ArticlesThis is a guide to Circular linked list in C++. Here we discuss the Circular Linked List, which is a collection of nodes where every node links to form a circle. You may also look at the following articles to learn more –
You're reading Circular Linked List In C++
Doubly Linked Circular Lists In C++
Begin ptr := head while ptr := null, do done End displayBackward(): Begin ptr := last while ptr := null, do done End insertFirst(key, data): Begin create new node with key and data if list is empty, then last := node else end if head := node End insertLast (key, data): Begin create new node with key and data if list is empty, then last := node else end if last := node End insertAfter(key, newKey, data): Begin current := head if head is null, then return false while key of current is not key, do return false else current := next of current end if done create new node with newKey and data if current = last, then next of node := null last := node else next of node := next of current prev of next of current := node end if prev of node := current next of current := node return true; End deleteFirst(): Begin tempNode := head if next of head is null, then last := null else prev of next of head := null end if head := next of head return tempNode End deleteLast(): Begin tempNode := last if next of head is null, then head := null else next of prev of last := null end if last := prev of last return tempNode End deleteNode(key): Begin curr := head and prev := null if head is null, then return null end if while key of currr is not same as key, do if next of curr is null, then return null else prev := curr curr := next of curr end if done if curr is head, then head := next of head, otherwise next of prev of curr = next of curr if curr is last, then last := prev of curr, otherwise prev of next of curr = prev of curr return curr End
ExampleLive Demo
#include <iostream< using namespace std; class node { public: int data; int key; node *next; node *prev; }; node *head = NULL; node *last = NULL; node *current = NULL; bool isEmpty() { return head == NULL; } int length() { int length = 0; node *current; length++; } return length; } void displayForward() { //start from the beginning node *ptr = head; //navigate till the end of the list printf("n[ "); while(ptr != NULL) { } printf(" ]"); } void displayBackward() { //start from the last node *ptr = last; //navigate till the start of the list printf("n[ "); while(ptr != NULL) { } } void insertFirst(int key, int data) { //create a link node *link = new node(); if(isEmpty()) { last = link; } else { } //point it to old first link //point first to new first link head = link; } void insertLast(int key, int data) { //create a link node *link = new node(); if(isEmpty()) { last = link; } else { } //point last to new last node last = link; } node* deleteFirst() { //save reference to first link node *tempLink = head; //if only one link last = NULL; } else { } //return the deleted link return tempLink; } node* deleteLast() { //save reference to last link node *tempLink = last; //if only one link head = NULL; } else { } //return the deleted link return tempLink; } node* del(int key) { //start from the first link node* current = head; node* previous = NULL; //if list is empty if(head == NULL) { return NULL; } //navigate through list return NULL; } else { //store reference to current link previous = current; //move to next link } } //found a match, update the link if(current == head) { } else { } if(current == last) { } else { } return current; } bool insertAfter(int key, int newKey, int data) { //start from the first link node *current = head; //if list is empty if(head == NULL) { return false; } //navigate through list return false; } else { //move to next link } } //create a link node *newLink = new node(); if(current == last) { last = newLink; } else { } return true; } main() { insertFirst(1,10); insertFirst(2,20); insertFirst(3,30); insertFirst(4,1); insertFirst(5,40); insertFirst(6,56); printf("nList (First to Last): "); displayForward(); printf("n"); printf("nList (Last to first): "); displayBackward(); printf("nList , after deleting first record: "); deleteFirst(); displayForward(); printf("nList , after deleting last record: "); deleteLast(); displayForward(); printf("nList , insert after key(4) : "); insertAfter(4,7, 13); displayForward(); printf("nList , after delete key(4) : "); del(4); displayForward(); } Output List (First to Last): [ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ] List (Last to first): [ (1,10) (2,20) (3,30) (4,1) (5,40) (6,56) List , after deleting first record: [ (5,40) (4,1) (3,30) (2,20) (1,10) ] List , after deleting last record: [ (5,40) (4,1) (3,30) (2,20) ] List , insert after key(4) : [ (5,40) (4,1) (7,13) (3,30) (2,20) ] List , after delete key(4) : [ (5,40) (7,13) (3,30) (2,20) ]C Program To Insert A Node At Any Position Using Double Linked List
struct node { int num; struct node * preptr; struct node * nextptr; }*stnode, *ennode; void DlListcreation(int n); void DlLinsertNodeAtBeginning(int num); void DlLinsertNodeAtEnd(int num); void DlLinsertNodeAtAny(int num, int pos); void displayDlList(int a); int main(){ int n,num1,a,insPlc; stnode = NULL; ennode = NULL; printf(” Input the number of nodes : “); scanf(“%d”, &n); DlListcreation(n); a=1; displayDlList(a); printf(” Input the position ( 1 to %d ) to insert a new node : “,n+1); scanf(“%d”, &insPlc); printf(” Input data for the position %d : “, insPlc); scanf(“%d”, &num1); DlLinsertNodeAtAny(num1,insPlc); a=2; displayDlList(a); return 0; } void DlListcreation(int n){ int i, num; struct node *fnNode; stnode = (struct node *)malloc(sizeof(struct node)); if(stnode != NULL){ printf(” Input data for node 1 : “); scanf(“%d”, &num); ennode = stnode; for(i=2; i<=n; i++){ fnNode = (struct node *)malloc(sizeof(struct node)); if(fnNode != NULL){ printf(” Input data for node %d : “, i); scanf(“%d”, &num); ennode = fnNode; } else{ printf(” Memory can not be allocated.”); break; } } } else{ printf(” Memory can not be allocated.”); } } } void DlLinsertNodeAtAny(int num, int pos){ int i; struct node * newnode, *tmp; if(ennode == NULL){ } else{ tmp = stnode; i=1; while(i<pos-1 && tmp!=NULL){ i++; } if(pos == 1){ DlLinsertNodeAtBeginning(num); } else if(tmp == ennode){ DlLinsertNodeAtEnd(num); } else if(tmp!=NULL){ newnode = (struct node *)malloc(sizeof(struct node)); } } else{ } } } void DlLinsertNodeAtBeginning(int num){ struct node * newnode; if(stnode == NULL){ } else{ newnode = (struct node *)malloc(sizeof(struct node)); stnode = newnode; } } void DlLinsertNodeAtEnd(int num){ struct node * newnode; if(ennode == NULL){ } else{ newnode = (struct node *)malloc(sizeof(struct node)); ennode = newnode; } } void displayDlList(int m){ struct node * tmp; int n = 1; if(stnode == NULL) { printf(” No data found in the List yet.”); } else{ tmp = stnode; if (m==1) { } else{ } while(tmp != NULL){ n++; } } }
OutputWhen the above program is executed, it produces the following result −
Doubly Linked List : Insert node at any position: ----------------------------------------------------------------------------------- Input the number of nodes : 5 Input data for node 1 : 23 Input data for node 2 : 12 Input data for node 3 : 11 Input data for node 4 : 34 Input data for node 5 : 10 Data entered in the list are : node 1 : 23 node 2 : 12 node 3 : 11 node 4 : 34 node 5 : 10 Input the position ( 1 to 6 ) to insert a new node : 5 Input data for the position 5 : 78 After insertion the new list are : node 1 : 23 node 2 : 12 node 3 : 11 node 4 : 34 node 5 : 78 node 6 : 10Javascript Program For Removing Duplicates From An Unsorted Linked List
The linked list is a linear data structure that consists of nodes, and each node is stored in memory in a non-contiguous manner. Nodes are connected by storing the address of the next node. We are given a linked list that will contain some integers in a random manner and not in a sorted manner. The task is to find the elements of the linked list which are repeated and we have to remove the duplicated ones. We will see the proper code and explanation.
In this problem, we will keep the first copy of the elements of the linked list and remove the elements which are previously present in the linked list or which are not the first among the repeated set of the same elements.
For example −
In the given linked list only 5 elements that are 1, 5, 2, 7, and 6 are unique, and others are similar to them so we will remove the duplicate one.
Example: Naive ApproachIn this approach, we will use two loops to traverse over the given list.
class Node{ constructor(data){ this.value = data; chúng tôi = null; } } function print(head){ var temp = head; if(head == null){ console.log("The given linked list is empty"); } else { var ans = "" while(temp.next != null){ ans += temp.value; temp = temp.next } ans += temp.value } console.log(ans) } function add(data, head, tail){ return chúng tôi = new Node(data) } function removeDupli(head){ return head; } var temp = head.next; while(temp != null) { var temp2 = head; while(temp2 != temp && temp2.value != temp.value){ temp2 = temp2.next; } if(temp2 == temp) { temp = temp.next; } else { while(temp2.next != temp) { temp2 = temp2.next; } chúng tôi = temp.next; temp = temp.next; } } return head; } var head = new Node(1) var tail = head tail = add(5,head, tail) tail = add(5,head, tail) tail = add(2,head, tail) tail = add(7,head, tail) tail = add(1,head, tail) tail = add(2,head, tail) tail = add(6,head, tail) tail = add(5,head, tail) tail = add(7,head, tail) tail = add(7,head, tail) console.log("The given linked list is: ") print(head) head = removeDupli(head) console.log("The Linked list after the removal of duplicate integers is: ") print(head)The time complexity of above code is O(N*N) and space complexity is O(1).
Example: Using HashingIn this approach we will use hash set to find repeated elements
class Node{ constructor(data){ this.value = data; chúng tôi = null; } } function print(head){ var temp = head; if(head == null){ console.log("The given linked list is empty"); } else { var ans = "" while(temp.next != null){ ans += temp.value; temp = temp.next } ans += temp.value } console.log(ans) } function add(data, head, tail){ return chúng tôi = new Node(data) } function removeDupli(head){ return head; } var temp = head.next; var prev = head; var myset = new Set() myset.add(head.value); while(temp != null){ if(myset.has(temp.value)){ chúng tôi = temp.next; } else { prev= prev.next; myset.add(temp.value); } temp = temp.next; } return head; } var head = new Node(1) var tail = head tail = add(5,head, tail) tail = add(5,head, tail) tail = add(2,head, tail) tail = add(7,head, tail) tail = add(1,head, tail) tail = add(2,head, tail) tail = add(6,head, tail) tail = add(5,head, tail) tail = add(7,head, tail) tail = add(7,head, tail) console.log("The given linked list is: ") print(head) head = removeDupli(head) console.log("The Linked list after removal of duplicate integers is: ") print(head)The time complexity of above code is O(N*log(N)) and space complexity is O(1).
ConclusionIn this tutorial, we have implemented two pointers and hashing techniques to remove the duplicate elements from the given linked list. Time complexity of the Naïve or two pointers approach is quadratic or O(N*N), while the time complexity of the hashing is approx. linear or with the log factor that is O(N*log(N)).
How List Function Works In Php
Introduction to PHP list
PHP list function is an important function used for assigning values to a list of variables while performing single operation at one go. This function is not present in all the PHP versions and is introduced exclusively in PHP versions before 7.1 which works only for numerical arrays while assigning values to the list of variables. The values assigned to the variables is the return type once executed with the help of PHP list function. PHP list function is not actually a function like array rather it is considered a language construct for assigning values.
Start Your Free Software Development Course
Syntax
list(var_1, var_2, ...)The syntax flow is in a way where there is a list as function comprising of arguments passed from the function:
list: The function list() is declared.
var_1: The variable passed as an argument is required and is quite of mandatory in the sense this acts as the first variable to assign a value to the variable declared.
var_2: The second variable is optional and then this variable is used to assign values to the list followed by sequence.
This syntax when applied has a return type as assigned array which means whatever values are assigned to the array is the return type for that instance.
How list Function works in PHP?list() function is an inbuild function in PHP which is exclusively used for assigning values to more than one variable by performing a single operation at the time of execution.
Let’s see the actual flow for working of list function in PHP which is described as follows :
Initially list being an inbuild function doesn’t required to be written and doesn’t require any external function call it works just seamlessly without much intrusion.
The array gets assigned with the required values from the multiple values considered at the time of execution.
There is a misconception regarding the array declared as a variable for assigning values but it’s just a myth in actual it is just a language construct.
Everything gets executed in a single operation using list() function while assigning variable.
This function works seamlessly on the numerical arrays only, which represents the fact that the user will get interacted with the arrays using first variable which is var_1.
The second argument getting passed to the function is an optional argument which gets retrieved and worked once the first argument satisfies the condition.
One point needs to be kept in mind which is like the number of variables should not exceed length of the numerical array and in case it exceeds the array defined variable then it will give error with parameters types and there will be no return type at the time of execution.
There should be no exception introduced while executing this list function otherwise the requirement and the return type will not get suffice.
If a function is not having any return statement, then implicitly it will return NULL as its return type at the time of execution.
The parameters to be passed as part of the function should be arranged in a way where the list of variables will get separated by spaces within the code.
The first variable to be passed from the function is a mandatory variable for the return type.
Another important point to look upon is the version compatibility which means that the PHP version should have version support less than 7.
Also, coming to the version compatibility for PHP then PHP version 5 in the list should assign values starting with right most parameter.
Whereas there is a difference with PHP version 7 where the assignment to the values of variable which will appear as left-most parameter.
In case normal variables are used then there is no need to worry about assigning values to the variables and then using these arrays with indices is used for arranging the values in an order.
But in case of order must be maintained like from left to right or from right to left then it is very much need to keep in mind about the PHP versioning.
Examples of PHP listGiven below are the examples of PHP list:
Example #1This program demonstrates the PHP list where the array is assigned with the variable having values as shown in the output.
Code:
<?php $an_arr = array(“Banana”,”Mango”,”Apple”); list($a_1, $b_2, $c_3) = $an_arr; echo “I have many fruits, one $a_1, one $b_2 and one $c_3.”;
Output:
Example #2This program demonstrates the PHP list where array is assigned with the first and third value within the variable as shown in the output.
Code:
<?php $arr_b = array(“Pencil”,”Copy”,”Pen”); list($k_0, , $z_1) = $arr_b; echo “Used_variable_for_modification $k_0 and $z_1 variables.”;
Output:
Example #3This program demonstrates the declaration of array in a way where initially all the variables are listed, followed by retrieving some values and then listing some of them among all from which the third one gets skipped in case of the list with all the string it will return the NULL value as shown in the output.
Code:
<?php $in_p = array(‘choco’, ‘caramel’, ‘pancake’); list($choco, $cake, $caramel) = $in_p; echo “$choco cake $color and $caramel appears relishing n”; list( , , $caramel) = $in_p; echo “caramel cake tastes wow $caramel!n”; list($choco, , $cake) = $in_p; echo “$choco has $cake.n”; list($gi_t) = “lost_in n”; list($choco, , $cake) = $in_p; echo “$choco has $cake.n”; var_dump($gi_t);
Example #4This program demonstrates the nested array by using list function as shown in the output.
Code:
<?php list($a_1, list($b_2, $c_0)) = array(1, array(4, 6)); var_dump($a_1, $b_2, $c_0); echo “listing of nested array”;
Output:
ConclusionPHP list is an inbuild function which gives user the flexibility and versatility to adapt this function as part of the implementation as per requirement. Use of PHP list make the values arranged in some order which gives user visibility to implement a user-friendly array return values with variables.
Recommended ArticlesThis is a guide to PHP list. Here we discuss the introduction, how list function works in PHP? along with examples respectively. You may also have a look at the following articles to learn more –
File Handling In C#: I/O Operations
C# has a wide array of file operations. These operations include opening a file, reading or writing to a file. There can be instances wherein you want to work with files directly, in which case you would use the file operations available in C#. Some of the basic file operations are mentioned below.
Reading – This operation is the basic read operation wherein data is read from a file.
Writing – This operation is the basic write operation wherein data is written to a file. By default, all existing contents are removed from the file, and new content is written.
Appending – This operation also involves writing information to a file. The only difference is that the existing data in a file is not overwritten. The new data to be written is added at the end of the file.
In this tutorial, you will learn-
Basics I/O CommandsThe file will be a simple text file and have 2 lines as shown below
Guru99 – .Net
Guru99 -C#
For our example, we will create a simple Console application and work with our File I/O commands. The console application is the basic one which was created in the earlier tutorial. In the console application, all code is written to the chúng tôi file.
The File exists method is used to check if a particular file exists. So now let’s see the code which can be used to check if our chúng tôi file exists or not. Enter the below code in the chúng tôi file.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Tutorial { static void Main(string[] args) { String path = @"D:Example.txt"; if (File.Exists(path)) { Console.WriteLine("File Exists"); } Console.ReadKey(); } } } Code Explanation:-
First, we are setting a string variable with the path to our chúng tôi file.
Next, we use the File.Exists method to check if the file exists or not. If the File exists, a true value will be returned.
If we get a true value and the file does exist, then we write the message “File Exists” to the console.
When the above code is set, and the project is executed using Visual Studio, you will get the below output.
Output:-
From the above output, you can see that the File.Exists command was executed successfully, and the correct message was displayed in the console window.
The method is used to read all the lines one by one in a file. The lines are then stored in a string array variable. Let’s look at an example. Enter the below code in the chúng tôi file.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Tutorial { static void Main(string[] args) { String path = @"D:Example.txt"; String[] lines; lines = File.ReadAllLines(path); Console.WriteLine(lines[0]); Console.WriteLine(lines[1]); Console.ReadKey(); } } } Code Explanation:-
First, we are declaring a string array variable. This will be used to store the result which will be returned by the File.ReadAllLines method.
Next, we use the File.ReadAllLines method to read all the lines from our text file. The result is then passed to the lines variable.
Since we know that our file contains only 2 lines, we can access the value of the array variables via the lines[0] and lines[1] command.
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-From the output, you can see that the File.ReadAllLines command returned both the lines from our file Example.txt
This method is used to read all the lines in a file at once. The lines are then stored in a string variable. Let’s look at an example. Enter the below code in the chúng tôi file.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Tutorial { static void Main(string[] args) { String path = @"D:Example.txt"; String lines; lines = File.ReadAllText(path); Console.WriteLine(lines); Console.ReadKey(); } } } Code Explanation:-
First, we are declaring a string variable called Lines. This will be used to store the result which will be returned by the File.ReadAllText method.
Next, we use the File.ReadAllText method to read all the lines from our text file. The result is then passed to the lines variable.
We can directly use the Console.Writeline method to display the value of the Lines variable.
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-From the output, you can see that the File.ReadAlltext command returned both the lines from our file Example.txt
The method is used to make a copy of an existing file. Let’s look at an example. Enter the below code in the chúng tôi file.
using System; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Tutorial { static void Main(string[] args) { String path = @"D:Example.txt"; String copypath = @"D:ExampleNew.txt"; File.Copy(path,copypath); Console.ReadKey(); } } } Code Explanation:-
First, we are declaring a string variable called path. This will be the location of our chúng tôi file. This file will be the source file used for the copy operation.
Next, we are declaring a string variable called copypath. This will be the location of a new file called chúng tôi file. This will be the destination file in which the contents will be written from the source file Example.txt.
We then call the chúng tôi method to copy the file chúng tôi file to the file ExampleNew.txt.
When the above code is set, and the project is run using Visual Studio, the file chúng tôi will be copied to ExampleNew.txt.
The method is used to delete an existing file. Let’s look at an example. Enter the below code in the chúng tôi file.
using System; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Tutorial { static void Main(string[] args) { String path = @"D:Example.txt"; File.Delete(path); Console.ReadKey(); } } } Code Explanation:-
First, we are declaring a string variable called path. This will be the location of our chúng tôi file. This is the file which will be deleted.
Next, we are calling the File.Delete method to delete the file.
When the above code is set, and the project is run using Visual Studio, the file chúng tôi will be deleted from the D drive.
Summary
C# has a number of File operations which can be performed on files. Most of these operations are part of the class File.
If you want to read data from a file, you can use the File.ReadAlltext or File.ReadAllLines methods.
File Method Description
File.Exists File exists method is used to check if a particular file exists.
File.ReadAlllines The method is used to read all the lines one by one in a file.
File.ReadAllText This method is used to read all the lines in a file at once.
File.Copy The method is used to make a copy of an existing file.
File.Delete The method is used to delete an existing file.
Update the detailed information about Circular Linked List In C++ on the Moimoishop.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!