微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

比较链接列表,C ++,相同顺序但可以不同

如何解决比较链接列表,C ++,相同顺序但可以不同

|| 假设我们得到了函数定义 bool sameValsOrder(节点* p,节点* q) 我们必须编写一个函数来比较两个链表,如果它们具有相同的顺序,则返回true,否则返回false [77777]和[77]->是 [1234567]和[1234555567]-> true
bool sameValsOrder (node *p,node *q)
{
     if ( q==NULL && p==NULL)
        return true;
     else if ((q=NULL && p != NULL)|| (p=NULL && q!= NULL))
         return false;
     else if ( q != NULL && p != NULL)
           while ( q != 0 && p != 0)
            {
                if (p->data != q->data)
                    {
                        return false;
                        break;
                    }
                else 
                    {
                  p= p-> next ;
                  q= q-> next;
                    } 
            }
             return true;
    }
上面的代码是我的答案,但我意识到了一些。我需要在while循环内添加更多if语句,以使[77777]和[7]的链接列表返回true,因为它的相同顺序要少得多。     

解决方法

根据您所写的内容,您实际上并不关心这些值,但是如果要订购列表,您想返回true吗?似乎您只需要遍历列表中的每个值即可。只要NEXT值不小于PREVIOUS值,就继续遍历该列表。如果到达末尾,则返回true,因为列表是按顺序排列的。如果在任何时候遇到的值都小于以前的任何值,则只需在此处返回false即可。
#include <iostream>

using namespace std;

class node{
public:
    node(){next = NULL;}
    int data;
    node * next;
};
class myList{
public:
    myList(){pRoot = NULL;}
    void add(int data);
    node * pRoot;
};
bool sameValsOrder (node *p,node *q)
{
     if ( q==NULL && p==NULL) // If both lists are empty
        return true;
     else if ((q==NULL && p != NULL)|| (p==NULL && q!= NULL)) // One list is empty and the other is not
         return false;
     else if ( q != NULL && p != NULL) //Both lists contain values we must check
     {      
         int temp; //I am going to assume a singly linked list (no access to previous value),need this to hold previous value
         temp = p->data; 
         while (p->next != NULL) //The list still contains elements
         {
            if (p->next->data < temp) //The value in the current node is LESS than our temp,then list is out of order so return false
                return false;
            else { //Otherwise move to the next node
                temp = p->data;
                p = p->next;
            }
         }
         temp = q->data; //Reset temp for q
         //Do the same thing for q
         while (q->next != NULL) //The list still contains elements
         {
            if (q->next->data < temp) //The value in the current node is LESS than our temp,then list is out of order so return false
                return false;
            else { //Otherwise move to the next node
                temp = q->data;
                q = q->next;
            }
         }
     }
     return true; //If we are this are then both lists should be ordered
}
int main()
{
    myList * p = new myList();
    myList * q = new myList();
    p->add(7);
    p->add(6);
    p->add(5);
    p->add(4);
    p->add(3);
    p->add(2);
    p->add(1);

    q->add(7);
    q->add(6);
    q->add(5);
    q->add(5);
    q->add(5);
    q->add(5);
    q->add(4);
    q->add(3);
    q->add(2);
    q->add(1);
    cout << sameValsOrder (p->pRoot,q->pRoot) << endl;
    return 0;
}
void myList::add(int data)
{
    node * nodeToAdd = new node();
    nodeToAdd->data = data;
    if(pRoot == NULL) //List is empty
    {
        pRoot = nodeToAdd;
        pRoot->next = NULL;
    }
    else //List not empty insert new node at beginning
    {
        nodeToAdd->next = pRoot;
        pRoot = nodeToAdd;
    }
}
    ,
 while ( q != 0 && p != 0)
        {
            if (p->data != q->data)
                    return false;
                    break;
            else 
              p= p-> next ;
              q= q-> next;
        }
这是错误的,如果不相同,则返回false,因为p和q的大小可以不同 [1112] [12]将返回不应该返回的false     ,
while(q!=NULL || p!=NULL)
{    
    if(q->data==p->data)    
    { 
        p=p->next;
        break;
    }
    else(q->data <  p->data)
    q=q->next;
}
基本上,只有当值不匹配时,才应在第一个链接列表中向前移动,直到第二个列表匹配为止。     ,OP在评论中指出,他想考虑具有相同数据的节点“游程”,即使这些游程在2个列表中的长度不同也是如此。简化为“ѭ4”何时跳过构成行程的节点。 这是一些伪代码; C实现实际上并没有什么复杂(尽管可能还有几行):
bool sameValsOrder (node *p,node *q)
{
    while not at the end of either list,and the current nodes are the same {

        skip run in q,taking care to deal with the NULL at the end of the list

        skip run in p,taking care to deal with the NULL at the end of the list
    }

    if we\'ve reached the end of both lists,they are equivalent
}
C实现:
bool sameValsOrder (node *p,node *q)
{
    while (q && p && (q->data == p->data)) {
        /* find next different node in each list (ie.,skip runs) */

        int tmp = q->data;  /* or whatever type `data` is */

        while (q && (q->data == tmp)) {
            q = q->next;
        }

        while (p && (p->data == tmp)) {
            p = p->next;
        }
    }

    /*
     * we\'ve either reached the end of one or both lists,*  or have found a `data` difference
     */

    if (p == q) {
        /* should happen only when `q` and `p` are NULL */
        assert(q == NULL);
        assert(p == NULL);

        /* we\'ve reached the end of both lists,so they\'re \'equivalent\' */

        return true;
    }

    return false;
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。