Recursively insert and traverse linked list in C++
We get the integer value used to form the linked list. The task is to first insert and then traverse the singly linked list using recursive method.
Add nodes recursively at the end
If head is NULL → add node to head
Otherwise add to head( head → next )
Traverse nodes recursively
If head is NULL → exit
-
otherwise Print ( head → next )
Example
Input− 1 - 2 - 7 - 9 - 10
Output
Output strong>− Linked list: 1 → 2 → 7 → 9 → 10 → NULL
Input− 12 - 21 - 17 - 94 - 18
Output− Linked list: 12 → 21 → 17 → 94 → 18 → NULL
The method used in the following program is as follows
In this method we will use functions to add nodes and traverse the singly linked list and call them recursively for the next input.
Take a structure SLLNode with an integer and a next pointer SLLNode*.
-
Function addtoEnd(SLLNode* head, int data) Gets the pointer to the head of the linked list and the integer of the data part, and adds the node to the end of the linked list.
li> If the head pointer is NULL, the list is empty, now add a new node and set it as the head. Add head → next as NULL. Returns a pointer to the node
If head is not null, use head->next = addtoEnd(head->next, data) to add the node to head → next.
Function traverseList(SLLNode* head) Traverses from head and prints each value.
If head is NULL, print NULL and return.
Otherwise, print the data value and use traverseList(head->next) to traverse one.
Use addtoEnd() in the main creation list and use traverseList() to print the list.
Example
#include <bits/stdc++.h> using namespace std; struct SLLNode { int data; SLLNode* next; }; SLLNode* addtoEnd(SLLNode* head, int data){ if (head == NULL){ SLLNode *nodex = new SLLNode; nodex->data = data; nodex->next = NULL; return nodex; } else{ head->next = addtoEnd(head->next, data); } return head; } void traverseList(SLLNode* head){ if (head == NULL){ cout <<"NULL"; return; } cout << head->data << " -> "; traverseList(head->next); } int main(){ SLLNode* head1 = NULL; head1 = addtoEnd(head1, 1); head1 = addtoEnd(head1, 8); head1 = addtoEnd(head1, 56); head1 = addtoEnd(head1, 12); head1 = addtoEnd(head1, 34); cout<<"Linked List is :"<<endl; traverseList(head1); return 0; }
Output
If we run the above code, the following output will be generated
Linked List is : 1 -> 8 -> 56 -> 12 -> 34 -> NULL
The above is the detailed content of Recursively insert and traverse linked list in C++. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

Usage of MINUS in SQL and specific code examples In SQL, MINUS is an operator used to perform a difference operation between two result sets. It is used to delete the same rows from the first result set as in the second result set. The result set returned by the MINUS operator will contain rows that exist only in the first result set. The following uses specific code examples to demonstrate the usage of MINUS: Assume there are two tables - "table1" and "table2", their structures are as follows: Table name: table1 field

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

Java is a popular programming language with powerful file handling capabilities. In Java, traversing a folder and getting all file names is a common operation, which can help us quickly locate and process files in a specific directory. This article will introduce how to implement a method of traversing a folder and getting all file names in Java, and provide specific code examples. 1. Use the recursive method to traverse the folder. We can use the recursive method to traverse the folder. The recursive method is a way of calling itself, which can effectively traverse the folder.

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

1. Open the wps software and enter the wps text operation interface. 2. Find the insert option in this interface. 3. Click the Insert option and find the Shape option in its editing area. 4. Click the shape option and find the recommended option in its sub-menu. 5. Find the China map option in the recommended options. 6. Click on the China map option and drag it with the left mouse button in the editing input area to get the China map we need.

Recursion is a powerful technique that allows a function to call itself to solve a problem. In C++, a recursive function consists of two key elements: the base case (which determines when the recursion stops) and the recursive call (which breaks the problem into smaller sub-problems ). By understanding the basics and practicing practical examples such as factorial calculations, Fibonacci sequences, and binary tree traversals, you can build your recursive intuition and use it in your code with confidence.
