gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:51 24 4
gpt4 key购买 nike

#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 由信号 SIGSEGV(地址边界错误)终止。问题是控制不是从 swapNodes() 函数中的第一个 while 循环中出来的。该代码接受用户输入并在开头创建一个节点。

最佳答案

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

关于c - 如何在不交换数据的情况下交换两个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49858382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com