2016-04-05 35 views
0

İkili elemanları takas için bir fonksiyon yazın.bir tek başına bağlantılı listesine bakıldığında, aşağıdaki gibi unsurlar paiwise takas etmek zorunda

void swap(struct node *head) 
{ 
    struct node *p, *a, *q; 
    p = head; 
    do{ 
     head = p; 
     p = head -> next -> next; 
     q = head->next;    
     head -> next -> next = head; 
     head -> next = p; 
     head = q; 
    } while(p!=NULL); 
} 

Ama kod çalışmıyor. şöyle My tam kodudur:

#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 

struct node 
{ 
    int dat; 
    struct node *next; 
}; 

void print(struct node *); 
void swap(struct node *); 

int main() 
{ 
    int i; 
    struct node *head, *h1, *h2, *h3, *h4, *h5, *h6, *h7, *h8; 
    h1 = (struct node *)malloc(sizeof(struct node)); 
    h2 = (struct node *)malloc(sizeof(struct node)); 
    h3 = (struct node *)malloc(sizeof(struct node)); 
    h4 = (struct node *)malloc(sizeof(struct node)); 
    h5 = (struct node *)malloc(sizeof(struct node)); 
    h6 = (struct node *)malloc(sizeof(struct node)); 
    h7 = (struct node *)malloc(sizeof(struct node)); 
    h8 = (struct node *)malloc(sizeof(struct node)); 
    head = (struct node *)malloc(sizeof(struct node)); 
    head = h1; 
    h1 -> dat = 1; 
    h1 -> next = h2; 
    h2 -> dat = 2; 
    h2 -> next = h3; 
    h3 -> dat = 3; 
    h3 -> next = h4; 
    h4 -> dat = 4; 
    h4 -> next = h5; 
    h5 -> dat = 5; 
    h5 -> next = h6; 
    h6 -> dat = 6; 
    h6 -> next = h7; 
    h7 -> dat = 7; 
    h7 -> next = h8; 
    h8 -> dat = 8; 
    h8 -> next = NULL; 
    print(head); 
    printf("\n\n"); 
    swap(head); 
    getch(); 
    return 0; 
} 

void print(struct node *head) 
{ 
    if(head != NULL) 
    { 
     printf("%d\n", head -> dat); 
     head = head -> next; 
     print(head); 
    } 
} 

void swap(struct node *head) 
{ 
    struct node *p, *a, *q; 
    p = head; 
    do{ 
     head = p; 
     p = head -> next -> next; 
     q = head -> next;    
     head -> next -> next = head; 
     head -> next = p; 
     head = q; 
    } while(p != NULL); 
} 

kodumu

+1

Girinti kodunuzu ilk! – LPs

+3

Nasıl çalışmıyor? Beklenen ve gerçek davranışları tanımlayın. –

+0

Bu, takasın gerçekten çok garip bir uygulaması gibi görünüyor. İki işaretçiyi geçmek ve listedeki yerlerini değiştirmesini istemez miydin? –

cevap

0

Kodunuzdaki yanlış bir şeyler vardır düzeltmek yardımcı olun. Birincisi, başınızı döngüde değiştirmeye devam ettiğinizde, yalnızca başlangıçta değiştirmeniz gerekir (ve bağlantılı listeyi geçtiğinizde başka bir değişken kullanın). Ayrıca listenin sonuna geldiğinizde daha fazla çeke sahip olmalısınız.

(ben aslında bunu deneyin vermedi reddi) gibi bir şey deneyin:

void swap(struct node *head) 
{ 
    struct node *odd, *even; 
    if ((head == NULL) || (head->next == NULL)) 
     return; 
    odd = head; 
    even = head->next; 
    head = head->next; // change this one once and for all 

    while ((odd != NULL) && (even != NULL)) { 
     // swap the elements 
     odd->next = even->next; 
     even->next = odd; 

     // move to the next ones 
     odd = even->next; 
     if (odd != NULL) 
      even = odd->next; 
    } 
} 
İlgili konular