C++ 链表-appendlastnotofirst(C+;+;),我只编写了问题的逻辑。请告诉我更正的地方

C++ 链表-appendlastnotofirst(C+;+;),我只编写了问题的逻辑。请告诉我更正的地方,c++,data-structures,struct,linked-list,singly-linked-list,C++,Data Structures,Struct,Linked List,Singly Linked List,给定一个链表和一个整数n,将LL的最后n个元素追加到前面。 索引从0开始。您不需要打印元素,只需更新元素并返回更新的LL的头部即可。 假设给定的n将小于LL的长度 node* append_LinkedList(node* head,int n) { int len=0; node* temp=head; while(temp->next!=NULL){ temp=temp->next; len++; } for

给定一个链表和一个整数n,将LL的最后n个元素追加到前面。 索引从0开始。您不需要打印元素,只需更新元素并返回更新的LL的头部即可。 假设给定的n将小于LL的长度

node* append_LinkedList(node* head,int n)
{
    int len=0;
    node* temp=head;
    while(temp->next!=NULL){
        temp=temp->next;
        len++;
    }
    for(int i=1; i<=len-n; i++){
        temp=temp->next;
    }
    node* curr=temp;
    node* curr1=curr->next;
    curr->next=NULL;
    while(curr1->next!=NULL){
        curr1=curr1->next;
    }
    curr1->next=head;
    return head;
}
node*append\u链接列表(node*head,int n)
{
int len=0;
节点*温度=头部;
while(临时->下一步!=NULL){
温度=温度->下一步;
len++;
}
对于(int i=1;不精确;
}
节点*curr=temp;
节点*curr1=curr->next;
当前->下一步=空;
while(curr1->next!=NULL){
curr1=curr1->next;
}
curr1->next=头部;
回流头;
}

如果我正确理解了作业,那么您需要以下演示程序中所示的内容

我使用了一个模板结构,但如果你想你可以很容易地删除任何提到模板规范

我将函数命名为
append\n

给你

#include <iostream>
#include <iterator>

template <typename T>
struct node
{
    T data;
    node *next;
};

template <typename T, typename InputIterator>
void assign( node<T> * &head, InputIterator first, InputIterator last )
{
    while ( head != nullptr )
    {
        node<T> *current = head;
        head = head->next;
        delete current;
    }

    for ( node<T> **current = &head; first != last; ++first )
    {
        *current = new node<T>{ *first, nullptr };
        current = &( *current )->next;
    }
}

template <typename T>
void append_n( node<T> * &head, size_t n )
{
    if ( n != 0 )
    {
        node<T> **last = &head;

        while ( *last != nullptr && n-- )
        {
            last = &( *last )->next;
        }

        if ( *last != nullptr )
        {
            node<T> **first = &head;

            while ( *last != nullptr )
            {
                first = &( *first )->next;
                last  = &( *last )->next;
            }

            *last = head;
            head = *first;
            *first = nullptr;
        }
    }       
}

template <typename T>
std::ostream & operator <<( std::ostream &os, node<T> * &head )
{
    for ( const node<T> *current = head; current != nullptr; current = current->next )
    {
        os << current->data << " -> ";
    }

    return os << "null";
}

int main() 
{
    node<int> *head = nullptr;
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );

    assign( head, std::begin( a ), std::end( a ) );

    std::cout << head << '\n';

    for ( size_t i = 0, n = 1; i < N; i += n )
    {
        append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    for ( size_t i = 0, n = 2; i < N; i += n )
    {
        append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    for ( size_t i = 0, n = 5; i < N; i += n )
    {
        append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    return 0;
}
如果在函数返回指向head节点的指针时使用函数声明,则其定义可以如以下演示程序所示

#include <iostream>
#include <iterator>

template <typename T>
struct node
{
    T data;
    node *next;
};

template <typename T, typename InputIterator>
void assign( node<T> * &head, InputIterator first, InputIterator last )
{
    while ( head != nullptr )
    {
        node<T> *current = head;
        head = head->next;
        delete current;
    }

    for ( node<T> **current = &head; first != last; ++first )
    {
        *current = new node<T>{ *first, nullptr };
        current = &( *current )->next;
    }
}

template <typename T>
node<T> * append_n( node<T> * head, size_t n )
{
    if ( n != 0 )
    {
        node<T> *last = head;

        while ( last != nullptr && n-- )
        {
            last = last->next;
        }

        if ( last != nullptr )
        {
            node<T> *first = head;

            while ( last->next != nullptr )
            {
                first = first->next;
                last  = last->next;
            }

            last->next = head;
            head = first->next;
            first->next = nullptr;
        }
    }

    return head;
}

template <typename T>
std::ostream & operator <<( std::ostream &os, node<T> * &head )
{
    for ( const node<T> *current = head; current != nullptr; current = current->next )
    {
        os << current->data << " -> ";
    }

    return os << "null";
}

int main() 
{
    node<int> *head = nullptr;
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );

    assign( head, std::begin( a ), std::end( a ) );

    std::cout << head << '\n';

    for ( size_t i = 0, n = 1; i < N; i += n )
    {
        head = append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    for ( size_t i = 0, n = 2; i < N; i += n )
    {
        head = append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    for ( size_t i = 0, n = 5; i < N; i += n )
    {
        head = append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    return 0;
}

我不知道是谁否决了你的问题,但这项作业对初学者来说并不容易。
#include <iostream>
#include <iterator>

template <typename T>
struct node
{
    T data;
    node *next;
};

template <typename T, typename InputIterator>
void assign( node<T> * &head, InputIterator first, InputIterator last )
{
    while ( head != nullptr )
    {
        node<T> *current = head;
        head = head->next;
        delete current;
    }

    for ( node<T> **current = &head; first != last; ++first )
    {
        *current = new node<T>{ *first, nullptr };
        current = &( *current )->next;
    }
}

template <typename T>
node<T> * append_n( node<T> * head, size_t n )
{
    if ( n != 0 )
    {
        node<T> *last = head;

        while ( last != nullptr && n-- )
        {
            last = last->next;
        }

        if ( last != nullptr )
        {
            node<T> *first = head;

            while ( last->next != nullptr )
            {
                first = first->next;
                last  = last->next;
            }

            last->next = head;
            head = first->next;
            first->next = nullptr;
        }
    }

    return head;
}

template <typename T>
std::ostream & operator <<( std::ostream &os, node<T> * &head )
{
    for ( const node<T> *current = head; current != nullptr; current = current->next )
    {
        os << current->data << " -> ";
    }

    return os << "null";
}

int main() 
{
    node<int> *head = nullptr;
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );

    assign( head, std::begin( a ), std::end( a ) );

    std::cout << head << '\n';

    for ( size_t i = 0, n = 1; i < N; i += n )
    {
        head = append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    for ( size_t i = 0, n = 2; i < N; i += n )
    {
        head = append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    for ( size_t i = 0, n = 5; i < N; i += n )
    {
        head = append_n( head, n );

        std::cout << head << '\n';
    }

    std::cout << '\n';

    return 0;
}
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> null
4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> null
3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> null
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null

8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null

5 -> 6 -> 7 -> 8 -> 9 -> 0 -> 1 -> 2 -> 3 -> 4 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null