Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查链接列表是否为回文_C++_List_Palindrome - Fatal编程技术网

C++ 检查链接列表是否为回文

C++ 检查链接列表是否为回文,c++,list,palindrome,C++,List,Palindrome,我正在写一个程序来检查单链表是否是回文。我使用的是通过迭代进行反转的概念 我在链表中插入了2,3,5,3,2,并基于这样的思想对其进行了反转:如果反转后获得的链表与反转前的链表相同,那么它就是回文。但是我不能用match语句来结束这个程序。我如何匹配这两个列表 这是我的密码: struct Node{ int data; Node* next; }; Node*head; void Insert(int x) { Node*temp=new Node(); te

我正在写一个程序来检查单链表是否是回文。我使用的是通过迭代进行反转的概念

我在链表中插入了2,3,5,3,2,并基于这样的思想对其进行了反转:如果反转后获得的链表与反转前的链表相同,那么它就是回文。但是我不能用match语句来结束这个程序。我如何匹配这两个列表

这是我的密码:

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

void Insert(int x)
{
    Node*temp=new Node();
    temp->data=x;
    temp->next=head;
    head=temp;  
}

void print()
{
    Node*temp=head;
    cout<<"list is";
    while(temp!=NULL) 
    {
        cout<<temp->data;
        temp=temp->next;
    }
}

void rec()
{
    Node*current, *prev, *next;
    current=head;
    prev=NULL;
    while(current!=NULL)
    {
        next= current->next;
        current->next= prev;
        prev= current;
        current=next;
    } 
    head=prev;
}

int main()
{
    clrscr();
    Node*head=NULL;
    Insert(2);
    Insert(3);
    Insert(5);
    Insert(3);
    Insert(2);
    cout<<"list before reversing is\n";
    print();

    cout<<"\n";
    cout<<"list after reversing is \n";
    rec();
    print();

    getch();
}
struct节点{
int数据;
节点*下一步;
};
节点*头;
空白插入(整数x)
{
Node*temp=新节点();
温度->数据=x;
温度->下一步=头部;
压头=温度;
}
作废打印()
{
节点*温度=头部;
coutnext=prev;
prev=当前值;
当前=下一个;
} 
头=上一个;
}
int main()
{
clrsc();
Node*head=NULL;
插入(2);
插入(3);
插入(5);
插入(3);
插入(2);

cout使用函数to来检查回文是否正确,而不是单独的函数to reverse。在该函数中,反转列表并存储在临时列表中。 然后迭代并比较每个节点->数据。如果所有节点都匹配,则其回文部分将跳出循环并设置为false。

在列表的节点上创建一个双链接列表(添加一个节点*prev)。 然后,迭代并比较两个迭代器,如下所示:

bool isPalindrome(Node * head, Node * tail)
{
    Node * inc = head, * dec = tail;
    while (inc != nullptr && dec != nullptr)
    {
        if (inc->data != dec->data) return false;
        inc = inc->next;
        dec = dec->prev;
    }
    return inc == nullptr && dec == nullptr;
}

始终尝试为任务使用最佳容器。

使其递归,并在返回时检查

#include <functional>

using namespace std;

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

bool isPalin( Node* h )
{
    function<bool(Node*)> ip_rec = [&](Node* p) -> bool
    {
        if(p)
        {
            if( ! ip_rec(p->next) ) return false;
            if( p->data != h->data ) return false;
            h=h->next;
        }
        return true;
    };

    return ip_rec(h);
}
#包括
使用名称空间std;
结构体类型
{
int数据;
节点*下一步;
};
bool isPalin(节点*h)
{
函数ip_rec=[&](节点*p)->bool
{
如果(p)
{
如果(!ip_rec(p->next))返回false;
如果(p->data!=h->data)返回false;
h=h->next;
}
返回true;
};
返回ip_rec(h);
}

<代码> >我的C++解决方案…很容易但不确定代码是否优化了……/P>
bool isPalindrome(Node *head)
{

    int count=0;
    Node *ptr, *nptr;
    ptr=head;
    while(ptr!=NULL)
    {

        count++;
        ptr=ptr->next;
    }
    ptr=head;
    int arr[count],count1=0;
    while(ptr!=NULL)
    {
       // cout<<ptr->data;
        arr[count1]=ptr->data;
        count1++;
        ptr=ptr->next;

    }

    ptr=head;

 while(ptr!=NULL)
    {
        if(ptr->data!=arr[count-1])
        return false;
        ptr=ptr->next;
        count--;
        if(count==-1)
        return true;
    }

}
bool isPalindrome(节点*头)
{
整数计数=0;
节点*ptr,*nptr;
ptr=头部;
while(ptr!=NULL)
{
计数++;
ptr=ptr->next;
}
ptr=头部;
int arr[count],count1=0;
while(ptr!=NULL)
{
//库特达;
count1++;
ptr=ptr->next;
}
ptr=头部;
while(ptr!=NULL)
{
如果(ptr->data!=arr[count-1])
返回false;
ptr=ptr->next;
计数--;
如果(计数==-1)
返回true;
}
}