在 C 中的单向链表中交换位置



我被分配了一个任务,为C中的链表创建各种方法。我被困在交换方法上,这似乎弄乱了整个链表。有人对我哪里出错有任何建议吗?干杯!

这是我的代码。

int main(int argc, char* argv[])
{
    // A list of  pointers to Reminders 
    const int MAX_ENTRIES = 10;
    int numOfEntries = 0 ;
    reminder_t* pFirst = (reminder_t*) malloc ( sizeof(reminder_t));
    reminder_t* pSecond = (reminder_t*) malloc ( sizeof(reminder_t));
    reminder_t* pThird = (reminder_t*) malloc ( sizeof(reminder_t));
    reminder_t* pStart = NULL;
    if (pFirst != NULL)
    {
        strcpy( pFirst->message, "Mikes Birthday");
        pFirst->dateOfEvent.day= 1;
        pFirst->dateOfEvent.month= 1;
        pFirst->dateOfEvent.year= 2013;
        pFirst->pNext = NULL;
    }
    if (pSecond != NULL)
    {   
        strcpy( pSecond->message, "Als Soccer Match");
        pSecond->dateOfEvent.day= 2;
        pSecond->dateOfEvent.month= 2;
        pSecond->dateOfEvent.year= 2013;
        pSecond->pNext = NULL;
    }
    if ( pThird != NULL)
    {
        strcpy( pThird->message, "School Concert");
        pThird->dateOfEvent.day= 3;
    pThird->dateOfEvent.month= 3;
    pThird->dateOfEvent.year= 2013;
    pThird->pNext = NULL;
}
pFirst->pNext = pSecond;
pSecond->pNext = pThird;
pThird->pNext = NULL;
pStart = pFirst;
printf("n------Before------n");
listEntries(pStart);
swapPositonOf(pFirst,pThird);
printf("n------After-aa-----n");
listEntries(pStart);
getchar();
return 0;
}
void listEntries(reminder_t * pList) 
{
    printf("n");
    while (pList != NULL)
    {
            printf("%sn", pList->message);
        pList = pList->pNext;
    }
}
void swapPositonOf(reminder_t* first , reminder_t* second)
{
    reminder_t* pFirst = (reminder_t*) first;
reminder_t* pSecond = (reminder_t*) second;
reminder_t* temp = second->pNext;
pSecond->pNext = pFirst->pNext;
pFirst->pNext = temp;
temp = pSecond;
pSecond = pFirst;
pFirst = temp;
}

预期产出:

------Before------
Mikes Birthday
Als Soccer Match
School Concert
------After-aa-----
School Concert
Als Soccer Match    
Mikes Birthday

输出:

------Before------
Mikes Birthday
Als Soccer Match
School Concert
------After-aa-----
Mikes Birthday

你没有交换它,那么第一个节点之前的节点和第二个节点之前的节点呢?

对于单向链表,您无法直接找到要交换的元素之前的列表元素。 你有第一,第二,你可以直接操作它们,但你没有first.prev和second.prev。

您需要遍历列表,并找到要交换的两个节点(first_previous、second_previous)之前的节点。 然后,您的节点交换还需要交换这些先前每个节点的下一个。

reminder_t* first_prev, *second_prev;
first_prev = second_prev = pStart;
reminder_t* iter;
for( iter = pStart; iter; iter=iter->next )
{
    if( iter->next == first ) first_prev = iter;
    if( iter->next == second ) second_prev = iter;
}

您将需要修复上述内容以处理空列表,一个元素列表,并在列表的开头传递第一个或第二个......

您不会在firstsecond节点之前修改节点的pNext指针。

您需要将"first节点"前面的节点的pNext指向"second节点",反之亦然。

假设链表:

Node_A -> Node_B -> Node_C -> Node_D -> Node_E

您必须交换Node_B并Node_D:
断开和形成的总链接:

  1. 旧链接: Node_A -> Node_B .... 新链接:Node_A -> Node_D
  2. 旧链接: Node_B -> Node_C .... 新链接: Node_D -> Node_C
  3. 旧链接: Node_C -> Node_D .... 新链接:Node_C -> Node_B
  4. 旧链接: Node_D -> Node_E .... 新链接:Node_B -> Node_E

还要记住极端情况,如 NULL 指针和连续节点。

如果要交换列表节点的内容,则并不难:只需交换两个节点中的messagedateOfEvent字段即可。

但是如果你想交换这些节点的位置(正如函数的名称所暗示的那样),那么你必须注意pNext数据成员。
事实上,仅仅交换节点指针是不够的。
你需要在firstlast之前找到节点的位置,然后执行以下操作:

/* reminder_t* beforeFirst, reminder_t* beforeSecond */
beforeFirst->pNext = second;
beforeSecond->pNext = first;

并交换first->pNextsecond->pNext.

此外,在这些列表实现中,通常重要的是要注意头节点和尾节点等特殊情况

最新更新