Implementing fa unction that adds and sorts data in a singly linked list (addsort)?

不打扰是莪最后的温柔 提交于 2019-12-12 01:59:59

问题


I'm very new to C++ so it's a bit hard to understand the syntax sometimes. Anyways, I'm supposed to implement a function that adds and sorts the data given into a linked list. For example if I pass 2 into the list [1,4,5] then I should get [1,2,4,5]

Here is what I have written so far, and no it does not work, I keep getting "blah blah not declared in this scope"

void addSorted(Data * ){
  temp = 0;
  if (head == NULL) {
      head = new LinkNode(newData);
  }
  else {
      LinkNode * current = head;
      while (current->next != NULL) {
              current = current->next;
          }
      if(current->data > current->next->data){
          temp = current->data;
          current->data = current->next->data;
          current->next->data = temp;
      }
      current->next = new LinkNode(newData);
  }
}

Someone please help me, I'm using the struct LinkNode I think which is already given, in addition to a bunch of other functions like add, insert, remove, get, and size

I'm not looking to just get the answer, I need to know why what I have isn't working.


回答1:


Hope that you have really put your effort in it..I am posting the whole function for inserting data into list in sorted order which I had written long time back

void addData(node * head, int data){

        if(head == NULL) {  // First node

                node * temp = new node;
                temp -> data = data;
                temp -> next = NULL;
                head = temp;
                root = head;
        }else{

                node * prev = NULL;

                while(head -> next != NULL){  // Sorted Addition Logic is HERE

                        if(data >= head -> data){
                                prev = head;
                                head = head -> next;
                                continue;
                        }else{

                                node * temp = new node;
                                temp -> data = data; 
                                temp -> next = head;
                                if(prev != NULL)
                                   prev -> next = temp;
                                else
                                   head = temp;
                                break;
                        }
                }

                if(head -> next == NULL){

                        node * temp = new node;
                        temp -> data = data;
                        head -> next = temp;
                        temp -> next = NULL;
                }

        }
}

Seeing your code, it seems that your logic itself is wrong...you are always ending on the last node (while loop) and in the next if statment you are trying to compare the data of the next node which is not present



来源:https://stackoverflow.com/questions/7540554/implementing-fa-unction-that-adds-and-sorts-data-in-a-singly-linked-list-addsor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!