简体   繁体   中英

Linked list - AppendLastNToFirst (C++), I have written only the logic for the problem. Please tell me the correction

Given a linked list and an integer n, append the last n elements of the LL to front. Indexing starts from 0. You don't need to print the elements, just update the elements and return the head of updated LL. Assume given n will be smaller than length of 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;
}

If I have understood correctly the assignment then you need something like the following shown in the demonstrative program below.

I used a template structure but if you want you can easily remove any mentions of template specifications.

I named the function append_n .

Here you are.

#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;
}

The program output is

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

If to use your function declaration when the function returns pointer to the head node then its definition can look as it is shown in the following demonstrative program.

#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;
}

The program output is the same as shown above.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM