【问题标题】:How do I code a function to test if a linked list is sorted如何编写函数来测试链表是否已排序
【发布时间】:2015-02-20 21:42:33
【问题描述】:

我查看了其他帖子,并没有为我的询问找到一个很好的解决方案。我不想实际对链接列表进行排序,我想查看它是否已排序。我在 C++ 中有一个链表问题。我被要求编写一个给定链表定义的函数,以查看它是否已排序。

实现函数 isSorted - 如果链表中的值按升序排序,则返回 true。 (链表由整数组成)。

给定以下结构:

struct ListNode
   {
      double value;           // The value in this node
      struct ListNode *next;  // To point to the next node
   }; 

示例数据:从 isSorted 返回
1 -> 3 -> 7 真
4 -> 2 -> 7 错误
() True // 空列表。
3 真
1-> 5 -> 7 -> 2 假

我有这样的东西。

bool NumberList::isSorted() const
{
   ListNode *nodePtr;  // To move through the list
   nodePtr = head;

   while (nodePtr)
   {
      if(nodePtr->value <= nodePtr->value+1)
         nodePtr = nodePtr->next;
      else 
         return false;

       return true;
   }
}

我不确定我这样做是否正确,我需要帮助。 谢谢。

【问题讨论】:

  • 您的return true; 应该在循环之外,而不是在循环内。
  • 好点,谢谢。
  • @DieterLücking 没有必要粗鲁。我很难理解我发布问题的逻辑。

标签: c++ sorting object struct linked-list


【解决方案1】:

也许这会起作用......

bool NumberList::isSorted() const
{
    ListNode *nodePtr;
    nodePtr = head;
    double d;

    if (!nodePtr) return true; // Empty list

    // Save value of current node
    d = nodePtr->value;

    // Point to next node
    nodePtr = nodePtr->next;

    while (nodePtr)
    {
        if(d > nodePtr->value) return false; // Not sorted

        // Save value of current node
        d = nodePtr->value;

        // Point to next node
        nodePtr = nodePtr->next;
    }

    return true;
}

【讨论】:

    【解决方案2】:

    如果列表按升序排序,那么对于您使用的比较函数,每个节点的值都大于或等于前一个节点的值。 IE。价值永远不会减少。只需遍历节点并检查即可。

    注意:您所显示的条件,

    nodePtr->value <= nodePtr->value+1
    

    不检查任何合理的东西:它检查一个值是否小于或等于该值加 1。

    更合理的条件的一个想法是将nodePtr-&gt;value 与前一个节点的值进行比较。要轻松做到这一点,您需要在前一次迭代中存储前一个节点的值(或指向前一个节点的指针)。在一些变量中。

    【讨论】:

    • 是的,我被要求测试我的列表是否按升序排序。我列出了给定的示例数据 - 在指定的实例中为真,仅当值未按升序排序时为假。我理解你的说法,我该如何修复它以检查给定的选项。
    • 所以根据您的建议,我将声明一个值,将其设置为等于 nodePtr->value。然后遍历我的循环,如果我的以下值大于前面的设置温度值。我可以。我必须在每次迭代时更新我的​​临时值?
    • 请注意,除了测试访问的节点按升序比较外,测试节点 count 是否符合您的期望也很重要,以防您的链表代码有不知何故离开了一个节点或无意中破坏了previousnext链接。
    【解决方案3】:
    #include<bits/stdc++.h>
    using namespace std;
    struct node
    {
        int data;
        struct node* next;
    };
    void insert(struct node* p,int x)
    {
        struct node* q=new node;
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=q;
        q->data=x;
        q->next=0;
    
    }
    void printlist(struct node* p)
    {
        while(p)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
    }
    bool ifsorted(struct node* p)
    {
        while(p->next!=NULL)
        {
            if(p->data>p->next->data)
                return false;
            p=p->next;
    
    
        }
        return true;
    
    }
    int main()
    {
       struct node* head=new node;
       struct node* second=new node;
       struct node* third=new node;
       head->data=1;
       head->next=second;
       second->data=2;
       second->next=third;
       third->data=3;
       third->next=NULL;
       insert(head,4);
       insert(head,56);
       insert(head,100);
       printlist(head);
       ifsorted(head)?cout<<"Sorted":cout<<"Not sorted";
    
    }
    

    【讨论】:

    • 尝试使用 cmets 来引导和提供更多信息。
    猜你喜欢
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    相关资源
    最近更新 更多