简体   繁体   English

我无法使用以下代码反转给定的链表。 有人可以指出代码中的错误吗?

[英]I am unable to reverse the given linked list with the following code. Can someone point out the error in the code?

I am unable to reverse the given linked list with the following code.我无法使用以下代码反转给定的链表。 Can someone point out the error in the code?有人可以指出代码中的错误吗?

#include<iostream>
using namespace std;
class Node{
    public:
    int data;
    Node* next;
    Node* prev;
    Node(int d)
    {
        this->data=d;
        this->next=NULL;
        this->prev=NULL;
    }
};
void insertathead(Node* &head,int d)
{
    Node* temp= new Node(d);
    temp->next=head;
    head->prev=temp;
    head=temp;
}
void insertattail(Node* &tail, int d)
{
    Node* newnode=new Node(d);
    newnode->prev=tail;
    tail->next=newnode;
    tail=newnode;
}
void insertatmiddle(Node* &head, int position, int d)
{
    Node* temp=head;
    Node* newnode=new Node(d);
    int count=1;
    while(count<position-1)
    {
        temp=temp->next;
        count++;
    }
    newnode->prev=temp;
    temp->next->prev=newnode->next;
    newnode->next=temp->next;
    temp->next=newnode;
}
void reverse(Node* &head)
{
    Node* current=head;
    Node* temp=NULL;
    while(current!=NULL)
    {
       temp = current->prev; 
        current->prev = current->next; 
        current->next = temp;             
        current = current->prev;
    }
    head=temp->prev;
   

}
void display(Node* head)
{
    Node* temp=head;
    while(temp!=NULL)
    {
       cout<<temp->data<<" ";
       temp=temp->next;
    }
    cout<<endl;
}
int main()
{
  Node* node1=new Node(10);
  Node* head=node1;
  Node* tail=node1;
  insertathead(head,11);
  display(head);
  insertathead(head,12);
  display(head);
  insertathead(head,13);
  display(head);
  insertathead(head,14);
  display(head);
  insertattail(tail,15);
  display(head);
  insertatmiddle(head,3,20);
  display(head);
  cout<<head->data<<endl;
  cout<<tail->data<<endl;
  reverse(head);
  display(head);
  

  return 0;
}

In this code, when I am commenting out the insertatmiddle function then the reverse function is able to reverse the linked list completely.在这段代码中,当我注释掉 insertatmiddle function 然后反向 function 能够完全反转链表。 But after adding a node in between, the reverse function reverses the linked list till that particular node only.但是在中间添加一个节点之后,反向 function 将链表反向直到仅该特定节点。 Can someone figure out the error in this case?有人可以找出这种情况下的错误吗?

In your code for insertatmiddle在您的insertatmiddle代码中

newnode->prev=temp;
temp->next->prev=newnode->next;  // newnode->next is always NULL here
newnode->next=temp->next;
temp->next=newnode;

you've made a mistake.你犯了一个错误。 Try尝试

newnode->prev=temp;
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;

And by the way, if the code is for you, as long as you understand what you're doing, all good, but the way you implemented顺便说一句,如果代码适合你,只要你明白你在做什么,一切都很好,但你实现的方式

int count=1;
while(count<position-1) { ... }

baffles me a bit, since the link temp which has the newnode appended after itself is head for position = 0 , 1 , and 2 , which I find very unusual.让我有点困惑,因为在其后附加了newnode的链接temp指向head position = 012 ,我觉得这很不寻常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不太确定这段代码的行为。 有人可以向我解释一下吗? - I am not exactly sure of the behavior of this code. Can someone explain it to me? 我怎样才能扭转我的链接列表。 我编写了以下代码,但这仅给出第一个节点的数据为 output - how can I reverse my Linked list . I wrote following code but this is giving only first node's data as output 有人可以指出我的链表实现中的问题吗? - Can someone point out the problem in my linked list implementation? 在以下代码中执行删除操作时,我无法获得以下代码中返回的正确值。 - While performing the delete operation in the following code, I am not able to get the correct values returned in the following code. 我的代码中出现以下错误。 有人能帮我修一下吗 - i got the following error in my code. can some help me fix it 你好,其实我是想看懂下面的代码。 有人可以详细说明代码到底在做什么。 为什么它在第 31 行崩溃 - Hello, I actually want to understand the following code. Can someone please elaborate the code what exactly it is doing. why is it crashing at line 31 我正在尝试在链接列表的后面添加内容,但是我不断收到细分错误,有人可以解释原因吗? - I am trying to add at the back of the linked list but i keep getting segmentation error, can someone explain why? 有人可以解释以下链表片段吗 - Can someone explain the following snippet of linked list 我正在尝试 Leet 代码上的奇偶链表编程问题,我是 leetcode,所以我无法确定这个错误是关于什么的 - I am trying Odd Even linked list programming problem on Leet code, I am leetcode so I am unable to identify what this error is about 有人可以找到代码中的错误吗? 给出运行时错误“分段错误”。表达式树 - Can someone please find the error in code. Gives runtime error "Segmentation Fault" .Expression Tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM