链表cstring插入排序

时间:2012-02-09 07:10:50

标签: c++

这是我的插入排序代码,通过节点的cstring成员进行排序。当前代码仅在头部之前插入。封装在注释中的代码是我尝试工作的排序插入。我想我必须使用前任和后继指针,但也许这是令我困惑的比较。任何帮助,将不胜感激。谢谢!

#include <iostream>
#include "groclist.h"

void insert_item(Grocery_Item_Ptr &head, int quantity, const char name[])
{
bool exists = false;
bool done = false;
Grocery_Item_Ptr temp = NULL;
Grocery_Item_Ptr current = NULL;
Grocery_Item_Ptr pred = NULL;
Grocery_Item_Ptr succ = NULL;

if (head == NULL) {
    head = new Grocery_Item;
    head->quantity = quantity;
    strncpy(head->name, name, MAX_ITEM_NAME_LEN);
    head->next = NULL;
    return;
}
else {
    current = head;
    while (current != NULL) {
        if (strncmp(current->name, name, MAX_ITEM_NAME_LEN) == 0) {
            current->quantity += quantity;
            exists = true;
        }
        current = current->next;
    }

    if (exists) {
        current = NULL;
        return;
    }
    else {
        current = head;
    }

    if (!exists) {
        temp = new Grocery_Item;
        temp->quantity = quantity;
        strncpy(temp->name, name, MAX_ITEM_NAME_LEN);
/*                                                                                                                                 
        while (!done || current != NULL) {                                                                                     
            if (strncmp(current->name, name, MAX_ITEM_NAME_LEN) < 0) {                                                                                                              
                pred = current;
                succ = current->next;
                current->next = temp;
                temp->next = succ;

                done = true;                                                                                                   
            }                                                                                                                  
            if (!done) {                                                                                                       
                current = current->next;                                                                                       
            }                                                                                                                  
        }                                                                                                                      
*/

        temp->next = head;
        head = temp;
        temp = NULL;
    }
}

return;
}

3 个答案:

答案 0 :(得分:0)

您缺少的一件事是在搜索时保留对前任的引用。这是保持链完好无损的必要条件。

这是一个应该有效的草案(目前未经测试!):

while (!done || current != NULL)
{ 
    //If we found the place to insert
    if (strncmp(current->name, name, MAX_ITEM_NAME_LEN) < 0)
    {
        //If the place to insert was at head
        if(pred == NULL)
        {
            //The new node becomes the head
            head = temp;
        }
        else
        {
            //Set the previous nodes next to point at this node.
            pred->next = temp;
        }

        //Always set the node to be inserted's next
        //pointing to the node we should be inserted before
        temp->next = current;
    done = true;        
  }                     
    if (!done)
    {
        //No match, keep looking but keep an updated pred pointer
        pred = current;
        current = current->next;
    }
} 

答案 1 :(得分:0)

It's just pseudocode, but maybe it helps:

if(head == NULl)
    {
          //make newnode as head
}

if(head.name == new name)
    {
    //update quantity
    }

if(head.name <new name)
    {
    //insert newnode before head
    //make newnode as head
    }

if (new name > head.name)

{
    current = head;
    succ = current.next;

    while (succ && new name <succ.name)
        {
                 curent = succ;
         succ = succ.next
        }


    if(succ = NULL)
        current->next = newnode
    else
        if new name = succ->name
            update quantity
        else
            curent->next = newnode
            newnode->next = succ;
}

答案 2 :(得分:0)

我感谢你的输入家伙,只是让我以不同的方式思考它。但是我的结果完全不同,我必须真的感谢我的白板。

if (strncmp(name, head->name, MAX_ITEM_NAME_LEN) < 0) {
    temp->next = head;
    head = temp;
    temp = NULL;
}
else {
    pred = head;
    current = head->next;    
    do {
        if (strncmp(name, current->name, MAX_ITEM_NAME_LEN) < 0) {
            pred->next = temp;
            temp->next = current;
            done = true;
        }
        else if (current->next == NULL) {
            current->next = temp;
            done = true;
        }
        else {
            pred = current;
            current = current->next;
        }
        if (done) {
            pred = NULL;
            current = NULL;
        }
    } while (!done && current != NULL);
}