【问题标题】:Swap two nodes in singly linked list交换单链表中的两个节点
【发布时间】:2017-05-05 09:27:18
【问题描述】:

我在交换单链表中的节点时遇到问题。当两个节点都不是列表的开头时,我的代码当前有效。

编辑:我正在学习 ADT,所以我无法更改函数的输入和输出。

typedef struct textbuffer *TB;

struct textbuffer {
    char *data;
    TB next;
};

void swapTB(TB tb, int pos1, int pos2) {
    if (tb == NULL || pos1 == pos2) return;
    int totalLines = linesTB(tb) - 1;
    if (pos1 < FIRST_LINE || pos1 > totalLines || pos2 < FIRST_LINE || pos2 > totalLines) {
        printf("Error: line number out of range, %d-%d.\n", FIRST_LINE, totalLines);
        abort();
    } else {
        TB all = tb;
        int i = 0;
        TB prevX = NULL;
        TB currX = tb;
        while (i != pos1) {
            prevX = currX;
            currX = currX->next;
            i++;
        }

        int j = 0;
        TB prevY = NULL;
        TB currY = tb;
        while (j != pos2) {
            prevY = currY;
            currY = currY->next;
            j++;
        }

        if (prevX != NULL) {
            prevX->next = currY;
        } else {
            all = currY; //update head of list
        }

        if (prevY != NULL) {
            prevY->next = currX;
        } else {
            all = currX; //update head of list
        }

        TB temp = currY->next;
        currY->next = currX->next;
        currX->next = temp;
    }
    //return all;
}

我知道我交换节点的方式是正确的,因为如果我更改为返回 TB(在本例中为全部)的函数,那么它就可以工作。

我的问题是如何使用 void 函数而不改变函数接收的内容?我想我需要一个头指针?但是我该如何使用呢?

【问题讨论】:

  • 这段代码看起来非常复杂。顺便说一句,请提供minimal reproducible example
  • TB 是隐藏指针吗?否则,它的可见性/范围仅限于 swapTB 函数。
  • 将这些信息添加到您的帖子中。
  • 我选择使用单链表所以我这样写了struct,不知道要不要在里面加个TB头?
  • 头节点必须传递给该函数。我没有得到这个问题。

标签: c function pointers linked-list


【解决方案1】:

做两件事:- 在函数中传递了 struct textbuffer 的地址。

void swapTB(TB *tb, int pos1, int pos2)

在 main() 中:-

swapTB(tb,pos1,pos2);

还要检查你的 currx 和 curry NULL 与否。

typedef struct node *TB;
struct node
{
    int data;
    TB next;
};

TB head=NULL;

void swapNodes(TB head_ref, int x, int y)
{
   if (x == y) return;
  head = head_ref;

   struct node *prevX = NULL, *currX = head_ref;
   while (currX && currX->data != x)
   {
       prevX = currX;
       currX = currX->next;
   }

   struct node *prevY = NULL, *currY = head_ref;
   while (currY && currY->data != y)
   {
       prevY = currY;
       currY = currY->next;
   }

   if (currX == NULL || currY == NULL)
       return;

   if (prevX != NULL)
       prevX->next = currY;
   else
       head = currY;

   if (prevY != NULL)
       prevY->next = currX;
   else
       head = currX;

   struct node *temp = currY->next;
   currY->next = currX->next;
   currX->next  = temp;

}

int main()
{
    TB start=NULL;
    // Create linked list here

    swapNodes(start, pos1, pos2);

    print_linkedlist(head);  // print the linked list after swap

    return 0;
}

【讨论】:

  • TB **tb --> TB *tb ...一个例子说明为什么最好避免使用 typedef 隐藏指针
  • @LPs 我正在传递指针的地址,据我所知,我们需要双指针来保存单指针的地址,因为我建议 OP 传递单指针的地址。
  • 有没有办法在不改变 void swapTB(TB tb, int pos1, int pos2) 的情况下做到这一点?因为我正在学习 ADT,这就是提供的内容
  • @LPs 哎呀,我没有观察到 OP 编辑​​了他的问题。
  • @kuroholic 你可以使用全局对象,但这不是好的编程。在您的情况下,通过参考是最佳选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 2015-05-25
  • 2010-12-04
  • 1970-01-01
相关资源
最近更新 更多