交换两个节点链表C

时间:2013-02-24 20:58:46

标签: c nodes

我正在尝试交换给定链表中的两个节点。它是否是它旁边的节点,或链表中的任何节点。没有错误,但我无法让它发挥作用。

struct lnode 
{
    int some_line;
    int count;
    char* some_word;
    struct lnode* next;
};
void swap_alternate_nodes(struct lnode** head, struct lnode* odd_node, struct lnode* even_node)
{
    struct lnode* temp;
    if((*head)==NULL)
    {
        return;
    }
    odd_node = (*head);
    even_node= (*head)->next;
    if((*head)->next!=NULL)
    {
        (*head)=(*head)->next;
    }
    while(odd_node && even_node)
    {
        temp=even_node->next;
        even_node->next=odd_node;
        odd_node->next=temp;
        odd_node=temp;
        if(odd_node!=NULL)
        {
            even_node=odd_node->next;
        }
    }
    return;
}

3 个答案:

答案 0 :(得分:1)

交换两个节点的内容会更容易!:

void swap_alternate_nodes(struct lnode** head, 
    struct lnode* odd_node, struct lnode* even_node) {
    int some_line = odd_node->some_line;
    int count = odd_node->count;
    char* some_word = odd_node->some_word;
    odd_node->some_line = even_node->some_line;
    odd_node->count = even_node->count;
    odd_node->some_word = even_node->some_word;
    even_node->some_line = some_line;
    even_node->count = count;
    even_node->some_word = some_word;   
}

答案 1 :(得分:0)

#include <stdio.h>
#include <string.h>
#include <ctype.h>

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

void swaptwo (  struct list **pp, struct list *one, struct list *two )
{
struct list *tmp=NULL, **aaa, **bbb;

if (!pp || !*pp || !one || !two || one==two) return ;

    for (aaa=bbb=NULL; *pp; pp = &(*pp)->next) {
        if ( !aaa && *pp == one) aaa = pp;
        else if ( !bbb && *pp == two) bbb = pp;
        if (aaa && bbb) break;
        }       
    if (!aaa || !bbb) return;

    tmp = *aaa;
    *aaa = *bbb;
    *bbb = tmp;

    tmp = one->next;
    one->next = two->next;
    two->next = tmp;
}       

struct list arr[]
= { {arr+1, 'A'} , {arr+2, 'B'} , {arr+3, 'C'} , {arr+4, 'D'} , {arr+5, 'E'} , {arr+6, 'F'} , {arr+7, 'G'} , {arr+8, 'H'}
, {arr+9, 'I'} , {arr+10, 'J'} , {arr+11, 'K'} , {arr+12, 'L'} , {arr+13, 'M'} , {arr+14, 'N'}, {arr+15, 'O'} , {arr+16, 'P'}
, {arr+17, 'Q'} , {arr+18, 'R'} , {arr+19, 'S'} , {arr+20, 'T'} , {arr+21, 'U'} , {arr+22, 'V'}, {arr+23, 'W'} , {arr+24, 'X'}
, {arr+25, 'Y'} , {NULL, 'Z'} };

int main (void) {
    struct list *root = arr, *ptr;

    printf( "Swap Q<-->X\n" );
    swaptwo ( &root, arr+16, arr+23);

    for (ptr=root ; ptr; ptr = ptr->next ) {
        printf( "-> %c" , ptr->ch );
        }       
    printf( "\n" ); 

    printf( "Swap B<-->C\n" );
    swaptwo ( &root, arr+1, arr+2); 

    for (ptr=root ; ptr; ptr = ptr->next ) {
        printf( "-> %c" , ptr->ch );
        }       
    printf( "\n" );

    printf( "Swap A<-->B\n" );
    swaptwo ( &root, arr, arr+1);

    for (ptr=root ; ptr; ptr = ptr->next ) {
        printf( "-> %c" , ptr->ch );
        }
    printf( "\n" );

   return 0;
}

答案 2 :(得分:-2)

首先,我会声明一个交换两个音符的函数

typedef struct lnode 
{
    int some_line;
    int count;
    char* some_word;
    struct lnode* next;
} Node;

void swapNodes(Node* a, Node* b)
{
    if (!(a || b))
        return;

    Node* buffer = a;
    a = b;
    b = buffer;

    //now its b-a-c-d-e ...
    buffer = b->next;
    b->next = a->next;
    a->next = buffer;

   // the same with content of the node, that defines list stuff like 'where am i' and so on ... you should get what i mean
}

然后你会用

之类的内容浏览你的列表
void swapList(Node* begin, Node* end)
{
    if (begin == end)
        return;
    Node* a,b;
    b = begin;
    a = b->next;
    while (a != end && b != end)  // if you dont have 'end' just use while(a != 0 || b != 0)
    {
        swapNodes(a,b);
        a = b->next;
        if (a == end)
            break;
        b = a->next;  
    }

}