【问题标题】:create a function to add a doubly linked list to itself at the end创建一个函数以在最后向自身添加一个双向链表
【发布时间】:2021-03-24 05:31:13
【问题描述】:
#include <iostream>
#include <string>
using namespace std;
class Node{
public:
double data;
Node* next;
Node* prev;
};
void insert(Node**,int);
void display(Node*);
void finaloutput(Node*,Node**);
int main()
{
    Node* head=NULL;
    Node** second=NULL;
    insert(&head,1);
    insert(&head,2);
    insert(&head,3);
    insert(&head,4);
    insert(&head,5);
    cout<<"List is"<<endl;
    display(head);
    cout<<endl;
    cout<<"Final output is"<<endl;
    second=&head;
//  Node* second=(*&head);
finaloutput(head,second);
display(head);
}
void insert(Node** headref,int new_data)
{
Node* new_node=new Node();
new_node->data=new_data;
new_node->next=(*headref);
new_node->prev=NULL;
if(*headref!=NULL)
{
(*headref)->prev=new_node;
}
(*headref)=new_node;
}
void display(Node* headref)
{
Node* temp=headref;
while (temp!=NULL)
{
    cout<<temp->data<<" ";
    temp=temp->next;
}

}
void finaloutput(Node* headref,Node** second)
{  
Node ptrbegin;
Node* temp=(headref);
while(temp->next!=NULL)
{
    temp=temp->next;
}

temp->next=(*second);
}

我想创建一个函数来在最后向自身添加一个链接列表,但在temp-&gt;next=(*second) 之后,这最终显示了一个无限循环。我试图将Node* head 的唯一值副本作为second,但它没有帮助。请用 c++ 中的示例代码指导我。

我想要的输出就像 '5 4 3 2 1 5 4 3 2 1'

【问题讨论】:

  • 您需要在display 函数中添加一些条件才能停止,因为如果您将最后一个节点与第一个节点链接,它是一个循环列表,display 中的while (temp!=NULL) 将始终返回true 因此无限循环。另外,我不明白所需的输出,中间的额外“4”来自哪里?
  • @Arsam Javed 为什么在这个预期的输出 5 4 3 2 1 4 5 4 3 2 1 中有三个数字 4?
  • 抱歉我的错误现在已更正

标签: c++ struct doubly-linked-list function-definition


【解决方案1】:

如果分配是

创建一个函数,在末尾添加一个双向链表

那么你所期望的输出

5 4 3 2 1 4 5 4 3 2 1

不正确,因为在将列表翻倍之前它具有以下值

5 4 3 2 1

所以双重列表的输出将是

5 4 3 2 1 5 4 3 2 1

您需要使用当前存储数据的副本将新节点添加到列表中。

你的类Node的数据成员data用类型说明符double声明

class Node{
public:
double data;
Node* next;
Node* prev;
};

但是函数insert 处理int 类型的数据

void insert(Node**,int);

因此您需要更改数据成员的类型或参数的类型。使用类键class 代替类键struct 没有什么意义。此外,由于该程序是 C++ 程序,因此您应该通过 C++ 含义中的引用而不是 C 含义中的指针指针将指针传递给头节点。

这是一个演示程序,展示了我在程序中重命名为double_list 的函数finaloutput 是如何编写的。

#include <iostream>

struct Node
{
    int data;
    Node *next;
    Node *prev;
};

void insert( Node * &head, int data )
{
    head = new Node { data, head, nullptr };
    if ( head->next ) head->next->prev = head;
}

std::ostream & display( const Node * head, std::ostream &os = std::cout )
{
    for ( const Node *current = head; current != nullptr; current = current->next )
    {
        os << current->data << " -> ";
    }
    
    return os << "null";
}

void double_list( Node * &head )
{
    if ( head )
    {
        Node *second_head = nullptr;
        
        Node **first = &head;

        for ( Node **second = &second_head, *prev = nullptr; 
              *first;
              first = &( *first )->next, prev = *second, second = &( *second )->next ) 
        {
            *second = new Node { ( *first )->data, nullptr, prev };
        }
        
        *first = second_head;
    }
}

int main() 
{
    Node *head = nullptr;
    
    insert( head, 1 );
    insert( head, 2 );
    insert( head, 3 );
    insert( head, 4 );
    insert( head, 5 );

    display( head ) << '\n';
    
    double_list( head );
    
    display( head ) << '\n';

    return 0;
}

程序输出是

5 -> 4 -> 3 -> 2 -> 1 -> null
5 -> 4 -> 3 -> 2 -> 1 -> 5 -> 4 -> 3 -> 2 -> 1 -> null

【讨论】:

    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多