【问题标题】:Swapping head of linked list with a link within it?用其中的链接交换链表的头部?
【发布时间】:2019-01-28 14:19:08
【问题描述】:

为了澄清标题:我像*head=(*head)->next一样遍历链表,因为它是一个函数。在我的函数中,我选择了我的 current head 和另一个在 current head 和要交换的列表末尾之间的链接。

我想要的是创建一个函数来交换head 和链接(不仅是数据),知道交换的所有条件都得到满足;表示链接不是当前的head 或链接不是NULL。可以这样做还是我必须尝试其他方法?

提前致谢。

由于我的问题不清楚,我给你问题。

我需要做一个这样的函数:

void intertwine(cvor **head)

我收到一个带有随机数的链表。我需要做的是交换链接,直到看起来像这样:奇数、偶数、奇数、偶数等。

我必须尊重不均匀和均匀的顺序。

如果没有相等数量的偶数和不均匀数,只需按照它们在列表中的顺序排列即可。

这里有两个例子:

输入:11、7、5、16、12、15、17、13、10、4、1

输出:11、16、7、12、5、10、15、4、17、13、1

输入:1、3、2、4

输出:1、2、3、4

我当前的代码如下所示(未完成

edit2:抱歉忘记了语言障碍

typedef struct atom{
    int el;
    struct atom *next;
} cvor;

void intertwine (cvor **head){
    cvor *pom,int br=1;

    pom=*head;
    while(*head){
        if((*head)->el%2==(br%2)){
            pom=(*head)->next;
            while(pom){
                if(pom->el%2==(br+1)%2)break;
                pom=pom->next;
            }
            if(pom==NULL) return;

最后是我想要的交换发生的时间。

【问题讨论】:

  • *head=(*head)->next 通常是head = head->next. 用于对象,-> 用于指向对象的指针。另外,在链表中,永远不要覆盖头节点,只覆盖指针。
  • 你真的不应该在遍历列表时使用*head,因为这会改变*head 指向的位置。这意味着您将失去真正的头脑。
  • 而对于链表操作,不管是什么,我总是建议你把它们都画在纸上。画几个列表,小方块是节点,箭头是节点之间的链接。然后一步一步地“执行”操作,在每个小步骤之间绘制和重绘。一旦你认为你在纸上做的很好,然后试着把图纸翻译成代码。
  • @And Ro 澄清一件事。您想将链表的某些元素作为头和头来代替那个或只是数据交换?
  • 有点不清楚你想要什么。请向我们展示图纸和/或您现有的代码。您的问题的总体答案(如果我理解正确的话)是:是的,可以做到。

标签: c function linked-list singly-linked-list


【解决方案1】:

如果您编写一个函数,可以将链表中的一个元素移动到同一链表中的另一个元素之前,则可以解决此问题。

函数应该作为输入

  • 指向head指针的指针'

  • 指向要移动的元素的指针 b

  • 指针 a 指向 b 应移动到前面的元素

喜欢

void move_b_in_front_of_a(cvor **head, cvor* b, cvor* a) { ... }

调用该函数时需要

  • ab 都指向列表中的元素 *head

  • a 在列表中位于b 之前

实现可能是这样的:

void move_b_in_front_of_a(cvor **head, cvor* a, cvor* b)
{
  // Find the element just before a
  cvor* a_prev = find_element_before(head, a);

  // Find the element just before b (start from a)
  cvor* b_prev = find_element_before(a, b);
  if (b_prev == NULL) { .... add error handling ....}

  // Take b out of the list
  b_prev->next = b->next;

  // Insert b in front of a
  if (a_prev == NULL)
  {
    (*head) = b;
  }
  else
  {
    a_prev->next = b;
  }
  b->next = a;
}

在上面的代码中我使用了函数

cvor* find_element_before(cvor* l, cvor* e)
{
  // Add code to find the element just before e in the list l
  // Return NULL if e is first element
  // Add error handling if element e isn't found

  ...
  ...

  return pointer_to_element_just_before_e;
}

您需要实施。

当您拥有这两个函数时,应该很容易实现intertwine 函数。

下面是一些可能让您入门的函数的伪代码:

current_element = *head
expected-type = odd
loop:
    if current_element is expected-type
        toggle expected-type
        current_element = next
        if current_element is NULL return
    else
        find_element with_correct_type
        if no element found return
        move found_element in front of current_element  (use above function)
        current_element = found_element

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    相关资源
    最近更新 更多