【问题标题】:change the nodes of a linked list [closed]更改链表的节点[关闭]
【发布时间】:2016-02-29 08:58:09
【问题描述】:

我的列表节点的代码是:

struct list{                                        
    int value;                                      
    struct list *next;                               
};

我想做一个这样的交换函数:

void swap(struct list *head , int v)

用户给出一个数字 v,程序在列表中搜索它并用下一个节点更改它。 例如,如果用户给出 3 并且列表包含:2 -1 7 3 -5 4,则交换函数将使列表如下所示:2 -1 7 -5 3 4 有任何想法吗 ?

我为交换制作了以下代码:

void swap(struct list *head, int v){
       struct list *before=NULL;
       struct list *found=NULL;
       struct list *after=NULL;


if(head==NULL){
   printf("Case of empty list !\n);
}

before=head;
found=head;
while(found->next !=NULL){
       if (found->value==v){
                after = before->next;
                before = found->next;
        }
        before = found;
        found = found->next;
        after = found->next;
}
return;
}

【问题讨论】:

  • 到目前为止你写了什么在你的交换函数中不起作用?
  • 我在函数中使用了 3 个用 head 初始化的指针,然后花了一段时间找到所需值的节点。第一个指针在具有值的节点之前的节点,另一个在之后,第三个在具有值的节点。
  • 请使用编辑按钮将其添加到您的原始问题中。
  • @reshad 是的,我很抱歉,我修好了...
  • @Katerina Tsellou 您确实要交换节点还是只交换节点中存储的值?

标签: c list function nodes swap


【解决方案1】:

试试这个方法:

  1. 在链表中搜索int v直到最后一个节点

  2. 如果找到并且该节点不是最后一个节点,则交换该节点的数据。

  3. 如果它是最后一个节点,则不能交换。你必须找到另一个案例

  4. 如果该节点是唯一节点,那么您还必须考虑另一个条件,例如它是否是最后一个节点

如果你想交换节点,那么试试这个代码

void swap(node *head, int v) {

node * prev,*curr,*NEXT,*temp

curr=head;

prev=curr;

NEXT=curr->next;

while(curr!=NULL){

if(curr->data==v){

  if(curr->next!=NULL){
   prev->next=NEXT;
   temp=NEXT->next;
   NEXT->next=curr;
   curr->next=temp;

   break; 
   }

  else{
     printf("\nThere is no further node to swap ");
    }
 } 

 prev = curr;
 curr = curr->next;
 NEXT = curr->next;

} 

}    

【讨论】:

    【解决方案2】:

    由于结构中只有数字,因此应该可以交换它们。

    注意以下例外情况:

    • 当没有要交换的元素时(即列表末尾)。
    • 当元素在列表中不存在时。

    在您的代码中:

    void swap(struct list *head, int v){
    
         int temporary_number;
         struct list *found=NULL;
    
         if(head==NULL){
            printf("%s", "Case of empty list !\n");
         }
    
         found = head;
    
         while(found->next != NULL){
           if (found->value == v){
                    temporary_value = found->next->value
                    found->next->value = found->value
                    found->value = temporary_value
            }
            found = found->next
         }
    
         return;
    }
    

    【讨论】:

      【解决方案3】:

      根据您的示例,您可以执行以下操作: 在列表中找到包含 3 的节点。这应该很容易,因为您正在传递指向列表头部的指针。找到 3 后,将 -5 保存在临时节点指针中。让带有“7”的节点指向与临时指针相同的位置。然后让 -5 的 next 指针指向 3。最后,重新赋值 3 指向 4。逻辑与任何交换函数相同。您将使用一个临时变量来存储您正在交换的内容。然后重新分配值。

      更一般的解释:

      1. 遍历到包含 v 的节点。
      2. 然后,将下一个节点保存在临时指针中
      3. 设置前一个节点的next指针等于临时指针
      4. 设置包含“v”的节点的next指针等于临时节点的next指针
      5. 设置临时指针的“next”指针等于包含“v”的节点
      6. 考虑边缘情况,例如:节点无法交换,因为它是列表中的最后一个节点,等等。

      【讨论】:

        【解决方案4】:

        别着急。 :) 正如你所说,你需要交换节点本身而不是只交换它们的值,然后你就在这里。 :)

        #include <stdio.h>
        #include <stdlib.h>
        
        struct list
        {                                        
            int value;                                      
            struct list *next;                               
        };
        
        void push_front( struct list **head, int value )
        {
            struct list *tmp = malloc( sizeof( struct list ) );
        
            if ( tmp )
            {
                tmp->value = value;
                tmp->next = *head;
                *head = tmp;
            }
        }
        
        void display( struct list *head )
        {
            for ( struct list *tmp = head; tmp; tmp = tmp->next )
            {
                printf( "%d ", tmp->value );
            }
            printf( "\n" );
        }    
        
        void swap( struct list **head, int value )
        {
            while ( *head && ( *head )->value != value )
            {
                head = &( *head )->next;
            }
        
            if ( *head && ( *head )->next )
            {
                struct list *next = ( *head )->next->next;
                ( *head )->next->next = *head;
                *head = ( *head )->next;
                ( *head )->next->next = next;
            }        
        }
        
        int main( void ) 
        {
            struct list *head = NULL;
        
            push_front( &head, 4 );
            push_front( &head, -5 );
            push_front( &head, 3 );
            push_front( &head, 7 );
            push_front( &head, -1 );
            push_front( &head, 2 );
        
            display( head );
        
            swap( &head, 2 );
            display( head );
        
            swap( &head, 2 );
            display( head );
        
            swap( &head, 2 );
            display( head );
        
            swap( &head, 2 );
            display( head );
        
            swap( &head, 2 );
            display( head );
        
            swap( &head, 2 );
            display( head );
        
            return 0;
        }
        

        程序输出是

        2 -1 7 3 -5 4 
        -1 2 7 3 -5 4 
        -1 7 2 3 -5 4 
        -1 7 3 2 -5 4 
        -1 7 3 -5 2 4 
        -1 7 3 -5 4 2 
        -1 7 3 -5 4 2 
        

        或者更有趣的例子

        #include <stdio.h>
        #include <stdlib.h>
        
        struct list
        {                                        
            int value;                                      
            struct list *next;                               
        };
        
        void push_front( struct list **head, int value )
        {
            struct list *tmp = malloc( sizeof( struct list ) );
        
            if ( tmp )
            {
                tmp->value = value;
                tmp->next = *head;
                *head = tmp;
            }
        }
        
        void display( struct list *head )
        {
            for ( struct list *tmp = head; tmp; tmp = tmp->next )
            {
                printf( "%d ", tmp->value );
            }
            printf( "\n" );
        }    
        
        void swap( struct list **head, int value )
        {
            while ( *head && ( *head )->value != value )
            {
                head = &( *head )->next;
            }
        
            if ( *head && ( *head )->next )
            {
                struct list *next = ( *head )->next->next;
                ( *head )->next->next = *head;
                *head = ( *head )->next;
                ( *head )->next->next = next;
            }        
        }
        
        int main( void ) 
        {
            struct list *head = NULL;
            int a[] = { 2, -1, 7, 3, -5, 4 };
        
            for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
            {
                push_front( &head, a[i] );
                display( head );
                for ( size_t j = 0; j < i; j++ ) 
                {            
                    swap( &head, a[i] );
                    display( head );
                }
                printf( "\n" );
            }
        
            display( head );
        
            return 0;
        }
        

        程序输出是

        2 
        
        -1 2 
        2 -1 
        
        7 2 -1 
        2 7 -1 
        2 -1 7 
        
        3 2 -1 7 
        2 3 -1 7 
        2 -1 3 7 
        2 -1 7 3 
        
        -5 2 -1 7 3 
        2 -5 -1 7 3 
        2 -1 -5 7 3 
        2 -1 7 -5 3 
        2 -1 7 3 -5 
        
        4 2 -1 7 3 -5 
        2 4 -1 7 3 -5 
        2 -1 4 7 3 -5 
        2 -1 7 4 3 -5 
        2 -1 7 3 4 -5 
        2 -1 7 3 -5 4 
        
        2 -1 7 3 -5 4 
        

        像往常一样,我的答案是最好的。:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-15
          • 1970-01-01
          • 1970-01-01
          • 2019-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多