Generate a link list by recursive methods

The outputs:

  2  8  5  1 10

the corresponding codes:\

#include <iostream>
#include <iomanip>
using namespace std;
struct LNode{
    int value;
    LNode *next;
};
LNode *generate_Link(int n);
void disp_Link(LNode *head);
int main()
{
    int n = 5;
    LNode *head;
    head = generate_Link(n);
    disp_Link(head);
    return 0;
}
LNode *generate_Link(int n)
{
    if (n == 0) return NULL;
    LNode *head;
    head = new LNode;
    head->value = rand() % 10 + 1; //
    head->next = NULL;
    head -> next = generate_Link(n-1);
    return head;
}
void disp_Link(LNode *head)
{
    if (head == NULL) return;
    cout << setw(3) << head->value ;
    disp_Link(head->next);
}

おすすめ

転載: blog.csdn.net/weixin_38396940/article/details/121666715