0

特定のリンク リスト内の 2 つのノードを交換しようとしています。隣のノードであるか、リンクされたリスト内の任意のノードであるか。エラーはありませんが、動作させることができません。

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;
}
4

3 に答える 3

1

2つのノードのコンテンツを交換する方がはるかに簡単です!:

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;   
}
于 2013-02-24T21:08:06.097 に答える
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;
}
于 2013-02-24T22:14:01.430 に答える
-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;  
    }

}
于 2013-02-24T21:32:00.167 に答える