Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

We discussed the introduction and application of circular lists in our last article on circular lists. In this article, we discuss traversal operations

In a traditional linked list, we traverse the list from the beginning node and stop when we reach NULL. In a circular list, when we reach the first node again, we stop traversing. Here is the C++ code for list traversal.

void printList(Node* head)
{
	Node* temp = head;

	if(head ! =NULL) {

		do {
			cout << temp->data << "";
			temp = temp->next;
		} while (temp != head);
	}
}
Copy the code

Complete procedures to demonstrate traversal. The following is a demonstration loop linked list traversal of the complete program.

#include <bits/stdc++.h>
using namespace std;
/* Node structure */
class Node
{
	public:
	int data;
	Node *next;
};
/* The function that inserts a node at the beginning of the circular list */
void push(Node **head_ref, int data)
{
	Node *ptr1 = new Node(a); Node *temp = *head_ref; ptr1->data = data; ptr1->next = *head_ref;/* Sets the next */ of the last node if the list is not NULL
	if(*head_ref ! =NULL)
	{
		while(temp->next ! = *head_ref) temp = temp->next; temp->next = ptr1; }else
		ptr1->next = ptr1; /* For the first node */

	*head_ref = ptr1;
}
/* The function that prints the nodes in the given circular list */
void printList(Node *head)
{
	Node *temp = head;
	if(head ! =NULL)
	{
		do
		{
			cout << temp->data << "";
			temp = temp->next;
		}
		while (temp != head);
	}
}
/* Test the driver for the above function */
int main(a)
{
        /* Initializer list is empty */
	Node *head = NULL;
        /* The list created will be 11->2->56->12 */
	push(&head, 12);
	push(&head, 56);
	push(&head, 2);
	push(&head, 11);

	cout << "Loop list contents \n";
	printList(head);

	return 0;
}
Copy the code

Output:

Loop list contents11 2 56 12
Copy the code

🥇 past quality articles

Singly linked list data structure of the chain table from the introduction of | first set of singly linked list data structure linked list and array | second singly linked list data structure of chain table insert | | delete nodes of the third set of singly linked list data structure of the fourth set of singly linked list data structure to delete a given location linked list node | the fifth set Singly linked list data structure of the check method of array and list | singly linked list data structure of the set of 6-1 check method of array and list | set 6-2 singly linked list data structure of the length of the chain table lookup (iteration and recursion) | 7 sets of singly linked list data structure of switching nodes in the list without exchanging data | 8 sets Singly linked list data structure of the inversion list | 9 sets of singly linked list data structure of merging two sorted lists | 10 sets of circular linked list data structure is introduced and the application of | first set of singly linked list data structure and the second set of insert | circular linked list

📣 Endnote: For more information on data structures, you can follow me: Haiyong, I hope you found this article helpful.

If you see this, thank you for reading 🙂

💌 welcomes your comments and suggestions in the comments section! 💌

If you really learn something new from this article, like it, bookmark it and share it with your friends. 🤗 and finally, don’t forget ❤ or 📑.