简体   繁体   English

链表中的add_sorted函数

[英]add_sorted function in linked lists

I am currently trying to make a linked list with a function that can add integers sorted to the list while using some of the functions I made, but I am running into a problem with my code: 我目前正在尝试使用一个函数创建链表,该函数可以在使用我创建的某些函数时添加对列表进行排序的整数,但是我的代码遇到了问题:

void list::add_sorted(int el)
    {
      if (empty()) add_first(el);
      else if (el<=head->data) add_first(el);
      else if (el >= tail->data) add_last(el);
    }

And I am wondering why the output in my code: 3 我想知道为什么我的代码中的输出:3

While in the main function i wrote : 在主要功能中,我写道:

list t1;
t1.add_sorted(5);
t1.add_sorted(3);
t1.add_sorted(9);
t1.print();

Here is the implementation for add_first(int el) , add_last(int el) and empty(): 这是add_first(int el),add_last(int el)和empty()的实现:

list::list()
{
  head = tail = 0;

}
bool list::empty()
{
  return (head == 0);

}
void list::add_first(int el)
{
    if (empty()) head = tail = new node(el);
    else
     head = new node(el);

}
void list::add_last(int el)
{
    if (empty()) head = tail = new node(el);
    else
     tail=tail->next = new node(el);

}

Thanks in advance. 提前致谢。

My understanding of an insertion into a sorted linked list is to keep the list sorted. 我对插入已排序链表的理解是使列表保持排序。

This means that you will need to search the list to find an appropriate location to add the new element: 这意味着您将需要搜索列表以找到合适的位置来添加新元素:

void addSorted(int value)
{
  node * new_element = new node(el);
  new_element->next = nullptr;
  if (head == nullptr)
  {
    head = new_element;
    tail = head;
  }
  else
  {
     node * p = head;
     node * previous = head;
     while (p != nullptr)
     {
       if (el > p.value)
       {
          new_element->next = p;
          previous->next = new_element;
          break;
       }
     }
     if (p == nullptr)
     {
        previous.next = new_element;
     }
  }
}

The idea here is that you need to search the list for the insertion point, then add your new element. 这里的想法是,您需要在列表中搜索插入点,然后添加新元素。 There are corner cases, such as when the head is null. 在某些情况下,例如head为null时。

You have done a small mistake in your void list::add_first(int el) method. 您在void list::add_first(int el)方法中犯了一个小错误。 As you did not add you all codes I have modified your code and which is available below. 由于您未添加所有代码,因此我已经修改了您的代码,下面提供了这些代码。 Please compare the below codes to understand you mistake, especially compare method void list::add_first(int el) . 请比较以下代码以了解您的错误,尤其是比较方法void list::add_first(int el) Hope this will help. 希望这会有所帮助。

struct node { public: int data; struct node {public:int数据; node *next; 节点* next; node(int val):data(val),next(NULL){} }; node(int val):data(val),next(NULL){}};

    class list
    {
      private:
         node *head,*tail, *temp;
      public:
         list();
         bool empty();
         void add_sorted(int el);
         void add_first(int el);
         void add_last(int el);
         void print();
    };

    list::list()
    {
      head = tail = 0;

    }
    void list::add_sorted(int el)
    {
       if (empty()) add_first(el);
       else if (el<=head->data) add_first(el);
       else if (el >= tail->data) add_last(el);
    }
    bool list::empty()
    {
      return (head == 0);

    }
    void list::add_first(int el)
    {
        if (empty()) head = tail = new node(el);
        else
        {
         //here temp is pointing to the first node of the list
         temp = head;
         head = new node(el); //now head is pointing to a new node
         head->next = temp; // and that new node is added to the list and finally head is pointing to the first node of the final list
        }

    }
    void list::add_last(int el)
    {
        if (empty()) head = tail = new node(el);//here I dont think this condition will ever satisfied
        else
         tail->next = new node(el);
         tail = tail->next;//more clearity

    }
    void list::print()
    {
      temp = head;
      while(temp)
      {
        std::cout<<temp->data<<", ";
        temp = temp->next;
      }
      std::cout<<std::endl;
    }
    int main()
    {
      list t1;
      t1.add_sorted(5);
      t1.add_sorted(3);
      t1.add_sorted(9);
      t1.print();
      return 0;
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM