0

我在实现双链表的选择排序时遇到问题。

请有人帮我对c中的双链表进行选择排序。

节点结构是:

struct node {
         char name [15];
         node * nextnode;
         node * prevnode;
}node;

我按名称排序

代码是这样的:

我需要按升序对列表进行排序names

char names[5][15] = {"tom","jack","mike","bernard","leo"};

我已经构建了一个双向链表,上面的节点结构和功能如下:

int list_add(char *lname)
{
    int i, length;
    struct node *current = NULL;
    struct node *temp = NULL;
    current = head;
    while (current->nextnode != NULL)
    {
        current = current->nextnode;
    }
    temp = malloc(sizeof(struct node));
    current->nextnode = temp;
    strcpy(temp->name,lname);
    temp->prevnode = current;
    temp->nextnode = NULL;

    return 0;
}

int init_list(char names[][15])
{
    int length;
    struct node * current = NULL;
    head = malloc(sizeof(struct node));
    strcpy(head->name,"Head");
    head->nextnode = head->prevnode = NULL;
    for(length = 0; length < 5; length++)
    {
        //printf("names are %s \n", (char *)names[length]);
        list_add(names[length]);
    }

    return length;
}

以上是简单的双向链表添加逻辑。

选择排序代码流程如下:

start node= 跟踪需要从中搜索最短节点的元素节点。

temp1被分配起始节点 - 原因是保持起始节点遍历不受交换干扰

tempnode 拥有该遍历的最短节点。

prev and next node's用于交换逻辑。

使用下面的代码,我没有得到排序列表。

int selsort(int length)
{
    int i = 0,j = 0;
    struct node *start = NULL;
    struct node *prev, *next, *temp, *temp1;
    struct node *current = NULL;
    struct node *traversal = NULL;
    prev = next = NULL;
    if (head == NULL)
        return 0;
    start = head;
    printf("selsort called \n");
    for(/*start = head->nextnode*/; start != NULL && i < 6; /*start = start->nextnode*/)
    {
        printf("Iteration number %d \n",i);
        start = head->nextnode;
        j = i;
        while(j)
        {
            start = start->nextnode;
            j--;
        }
        printf("start node %s \n",start->name);
        temp1 = start;
        current = start;
        temp = start;
        while(current != NULL)
        {
            if(strcmp(temp->name, current->name) > 0)
            {
                //ascending logic
                temp = current;
                current = current->nextnode;
            } 
            else 
                current = current->nextnode;
        }
        //printf("Before swap logic \n");
        //swap logic
        if(temp1->prevnode == NULL)
        {
            //swap current with temp
            next = temp1->nextnode;
            temp1->nextnode = temp->nextnode;
            temp1->prevnode = temp->prevnode;
            temp->nextnode->prevnode = temp1;
            temp->prevnode->nextnode = temp1;
            temp->nextnode = next;
            temp->prevnode = NULL;
            next->prevnode = temp;

        }else
        {
            //printf("second swap : %d \n",i);
            if(temp1->nextnode == NULL)
                continue;
            else 
            {
                next = temp1->nextnode;
                prev = temp1->prevnode;
                if (temp->nextnode == NULL)
                {
                    temp1->nextnode = NULL;
                    temp1->prevnode = temp->prevnode;
                    temp->prevnode->nextnode = temp1;
                }else{
                    temp1->nextnode = temp->nextnode;
                    temp1->prevnode = temp->prevnode;
                    temp->nextnode->prevnode = temp1;
                    temp->prevnode->nextnode = temp1;
                }
                temp->prevnode = prev;
                temp->nextnode = next;
                next->prevnode = temp;
                prev->nextnode = temp;
            }
        }
        i++;

        traversal = head;
        while(traversal != NULL)
        {
            printf("after iter %d Names are %s \n",i, traversal->name);
            traversal = traversal->nextnode;
        }
    }
}

上述实现无法对双向链表进行排序。

我得到了无限循环:

在 iter 4 之后的名字是 mike

在 iter 4 之后的名字是 mike

在 iter 4 之后的名字是 mike

在 iter 4 之后的名字是 mike

请有人为我的实施提供修复或对新实施的建议(参考链接)。提前致谢。

4

0 回答 0