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



给定一个链表和一个整数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;
}

如果我正确理解了赋值,那么您需要下面演示程序中显示的内容。

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

我将函数命名为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;
}

程序输出为

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

如果在函数返回指向头节点的指针时使用函数声明,则其定义可以如以下演示程序中所示。

#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

相关内容

  • 没有找到相关文章

最新更新