簡體   English   中英

排序插入 function 用於鏈表

[英]Sorted insert function for linked list

我需要將一個節點插入到排序的鏈接列表中,但我遇到了錯誤。 有人可以幫忙解決問題嗎?

struct NodeType
{
ItemType value;
NodeType * next;
}


void sortedInsert( NodeType * & head, int data )
{
    NodeType * p = head;
    NodeType * prev = NULL;
    while (p->value < data)
    {
        prev = p;
        p= p -> next;
    }
    NodeType * newnode = new NodeType;
    newnode -> value = data;
    newnode -> next = prev;
    p -> next = newnode ;

我更改了下面的程序,但在prev -> next = newnode行上出現Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)錯誤,我該如何更改該行?

void sortedInsert( NodeType * & head, int data ){

NodeType * p = head;
NodeType * prev = NULL;
while (p->value < data)
    {
        prev = p;
        p= p -> next;
    }
NodeType * newnode = new NodeType;
    newnode -> value = data;
    newnode -> next = p;
    prev -> next = newnode;
if(head== nullptr)
{
    NodeType * newnode = new NodeType;
    newnode -> value = data;
    newnode -> next = head;
    return;
}
}

一個可能的解決方案:

發生的事情很棘手,所以我會在發生時逐行解釋。

void sortedInsert( NodeType * & head, int data )
{
    NodeType ** p = &head; // You don't really want to find a node, you want to
                           // find where the new node goes, so rather than
                           // tracking nodes, track their next pointers. Once 
                           // you find it, you know exactly where to put the 
                           // new node

                           // head exists to tell you where the first node is. 
                           // That makes it a glorified next pointer. We can 
                           // hide the different name by adding an extra level 
                           // of indirection. Now head and any next all look the 
                           // same and no special cases are needed to manage
                           // insertion at the head

    while (*p &&  // make sure there is a node. Don't want to peek into a node 
                  // that doesn't exist. This also finds the end of the list 
                  // for us
           (*p)->value < data) // the current node goes before the new node
                               // don't want to insert here, so keep looking
    {
        p= &(*p) -> next; // advance p to point at the current node's next
    }

    // now we've found where the new node has to go - the end of the list or
    // the insertion point before the first larger node
    NodeType * newnode = new NodeType;
    newnode -> value = data; // set the data
    newnode -> next = *p; // point at the current node's next
    *p = newnode ; // insert new node at insertion point found earlier 

    // note: the preceding four lines probably could be
    // *p = new NodeType{data, *p};
}

再一次沒有所有的評論和絨毛

void sortedInsert( NodeType * & head, int data )
{
    NodeType ** p = &head; 
    while (*p &&  (*p)->value < data)
    {
        p= &(*p) -> next;
    }
    *p = new NodeType{data, *p};
}

您正在這些行中重新定義指針:

prev = p;
p = p -> next;

您將 prev 定義為 p,然后您正在更改 p,因為您使用的是指針,所以您也在重新定義 prev。 你可以通過做這樣的事情來避免這樣做

while(p->next -> value < data){
p = p->next;
} //Whats this is doing is analizing the next value, so there's no need to use a prev
because your p is prev.

一旦你到達它停止的點,你將有這樣的東西:

例如:

假設我有 data = 12,當進程停止時,我的 p 將在這個示例數組中的這個 position 中。

5 7 9 10  11 14
---------[P]--

現在我必須做(偽代碼)

auto aux = p.next;

node mynode(data);

mynode.next = aux;

p.next = mynode; 

希望這有用:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM