İ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
Girinti kodunuzu ilk! – LPs
Nasıl çalışmıyor? Beklenen ve gerçek davranışları tanımlayın. –
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? –