C - 如何在不交换数据的情况下交换两个节点


#include <stdio.h>
#include <stdlib.h>
struct node {   
    int data;
    struct node *next;
};
struct node *head;
void createnodeatbeg(int key) {
    struct node *new = (struct node*)malloc(sizeof(struct node));
    new->data = key;
    new->next = head;
    head = new;
}
void printlist() {
    struct node *temp = head;
    printf("list is:");
    while (temp != NULL) {
        printf("%d  ", temp->data);
        temp = temp->next;
    }
    printf("n");
}
void swapnodes(int x, int y) {
    struct node *prevX = NULL;
    struct node *prevY = NULL;
    struct node *currX = head;
    struct node *currY = head;
    while (currX->data != x && currX != NULL) {
        prevX = currX;
        currX = currX->next;
    }
    printf("not foundn");
    while (currY->data != y && currY != NULL) {
        prevY = currY;
        currY = currY->next;
    }
    if (currX == NULL || currY == NULL) {
        printf("elements not foundn");
        return;
    }
    struct node *swap = currY->next;
    prevX->next = currY;
    currY->next = prevY;
    prevY->next = currX;
    currX->next = swap;    
}
int main() {    
    head = NULL;
    int nodes, key;
    printf("enter number of nodesn");
    scanf("%d", &nodes);
    for (int i = 0; i < nodes; i++) {
        int data;
        printf("enter numbern");
        scanf("%d", &data);
        createnodeatbeg(data);
    }
    printlist();
    int x, y;
    printf("enter the values from the list to be swappedn");
    scanf("%d %d", &x, &y);
    swapnodes(x, y);    
    printf("swapped list is:n");
    printlist();
}

当元素(x 和 y(存在于列表中时,我的代码有效,但如果列表中不存在,则错误./a.out terminated by signal SIGSEGV (Address boundary error)。问题是控件不是从函数中的第一个 while 循环中出来的swapNodes()。该代码接受用户输入并在开始时创建一个节点。

while 语句条件中的操作数顺序是错误的。

while(currX->data!=x && currX!=NULL)
{
    prevX=currX;
    currX=currX->next;
}
//...
while(currY->data!=y && currY!=NULL)
{
    prevY=currY;
    currY=currY->next;
}

这里一定是

while(currX != NULL && currX->data!=x)
{
    prevX=currX;
    currX=currX->next;
}
//...
while(currY != NULL && currY->data!=y)
{
    prevY=currY;
    currY=currY->next;
}

因此,例如,如果currX等于NULL则表达式currX->data!=x不会被计算。

此代码片段

struct node *swap = currY->next;
prevX->next = currY;
currY->next = prevY;
prevY->next = currX;
currX->next = swap;  

也是错误的,因为例如prevXprevY可以等于NULL

并且您必须通过引用来处理函数中的头部。否则,不会更改头节点。

应将函数拆分为两个函数。第一个找到具有给定值的节点,第二个将交换找到的节点,如果它们不等于 NULL

当函数依赖于全局变量时,这也是一个坏主意。事实上,你的程序不能同时处理两个列表。

下面是一个演示程序,展示了如何实现函数交换。

#include <stdio.h>
#include <stdlib.h>
struct node 
{   
    int data;
    struct node *next;
};
struct node ** find( struct node **head, int data )
{
    while ( *head && ( *head )->data != data ) head = &( *head )->next;
    return head;
}
void swap( struct node **head, int data1, int data2 )
{
    struct node **left, **right;
    if ( *( left = find( head, data1 ) ) && *( right = find( head, data2 ) ) )
    {
        struct node *tmp = *left;
        *left = *right;
        *right = tmp;
        tmp = ( *left )->next;
        ( *left )->next = ( *right )->next;
        ( *right )->next = tmp;
    }
}
int push_front( struct node **head, int data )
{
    struct node *tmp = malloc( sizeof( struct node ) );
    int success = tmp != NULL;
    if ( success )
    {
        tmp->data = data;
        tmp->next = *head;
        *head = tmp;
    }
    return success;
}
void display( struct node **head )
{
    for ( struct node *current = *head; current; current = current->next )
    {
        printf( "%d ", current->data );
    }
}
int main(void) 
{
    const int N = 10;
    struct node *head = NULL;
    for ( int i = 0; i < N; i++ ) push_front( &head, i );
    display( &head );
    putchar( 'n' );
    for ( int i = 0; i < N; i+=2 )
    {
        swap( &head, i, i + 1 );
    }
    display( &head );
    putchar( 'n' );
    return 0;
}

它的输出是

9 8 7 6 5 4 3 2 1 0 
8 9 6 7 4 5 2 3 0 1 

问题出在以下相同的行中:

  • while(currX->data!=x && currX!=NULL)
  • while(currY->data!=y && currY!=NULL)

这是因为您不是在检查NULL然后使用它,而是在检查后者NULL。因此,当xy不存在时,您正在尝试访问NULL->data,这会产生分段错误(SIGSEGV(

分别将其更改为以下内容:

  • while(currX!=NULL && currX->data!=x)
  • while(currY!=NULL && currY->data!=y)

相关内容

  • 没有找到相关文章

最新更新