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

时间:2018-04-16 13:22:16

标签: c pointers linked-list swap singly-linked-list

#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 found\n");
    while (currY->data != y && currY != NULL) {
        prevY = currY;
        currY = currY->next;
    }

    if (currX == NULL || currY == NULL) {
        printf("elements not found\n");
        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 nodes\n");
    scanf("%d", &nodes);
    for (int i = 0; i < nodes; i++) {
        int data;
        printf("enter number\n");
        scanf("%d", &data);
        createnodeatbeg(data);
    }
    printlist();
    int x, y;
    printf("enter the values from the list to be swapped\n");
    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)。 问题是控件不是来自swapNodes()函数中的第一个while循环。 代码接受用户输入并在开头创建节点。

2 个答案:

答案 0 :(得分:2)

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将不会是eva; luated。

此代码段

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 

答案 1 :(得分:0)

问题在于以下相同的行:

  • 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)