C言語
1#include <stdio.h> 2 3struct node 4{ 5 int value; 6 struct node *next; 7}; 8 9static struct node * 10concatante (struct node *a, struct node *b) 11{ 12 struct node *p; 13 if (a == NULL) 14 return b; 15 else if (b == NULL) 16 return a; 17 for (p = a; p->next != NULL; p = p->next) 18 ; 19 p->next = b; 20 return a; 21} 22 23struct node * 24quick_sort_list (struct node *c) 25{ 26 int pivot; 27 struct node *t; 28 struct node *l_pivot = NULL; 29 struct node *e_pivot = NULL; 30 struct node *r_pivot = NULL; 31 32 if (c == NULL || c->next == NULL) 33 return c; 34 35 pivot = c->value; 36 37 for (; c != NULL; c = t) 38 { 39 t = c->next; 40 if (c->value < pivot) 41 { 42 c->next = l_pivot; 43 l_pivot = c; 44 } 45 else if (c->value == pivot) 46 { 47 c->next = e_pivot; 48 e_pivot = c; 49 } 50 else if (c->value > pivot) 51 { 52 c->next = r_pivot; 53 r_pivot = c; 54 } 55 } 56 57 l_pivot = quick_sort_list (l_pivot); 58 r_pivot = quick_sort_list (r_pivot); 59 60 return concatante (l_pivot, concatante (e_pivot, r_pivot)); 61} 62 63 64 65 66 #include <stdio.h> 67 #include <stdlib.h> 68 69struct node *quick_sort_list (struct node *); 70 71struct node 72{ 73 int value; 74 struct node *next; 75}; 76 77static void 78print_nodes (struct node *p) 79{ 80 for (; p != NULL; p = p->next) 81 printf (" %d", p->value); 82 printf ("\n"); 83} 84 85int 86main () 87{ 88 int list[] = { 1, 3, 7, 4, 2, 6, 5 }; 89 struct node *nodes = NULL; 90 int i; 91 92 for (i = sizeof list / sizeof list[0] - 1; i >= 0; --i) 93 { 94 struct node *p = malloc (sizeof *p); 95 if (p == NULL) 96 { 97 perror ("malloc"); 98 exit (EXIT_FAILURE); 99 } 100 p->value = list[i]; 101 p->next = nodes; 102 nodes = p; 103 } 104 105 print_nodes (nodes); 106 nodes = quick_sort_list (nodes); 107 print_nodes (nodes); 108 109 exit (EXIT_SUCCESS); 110} 111```以下のプログラムでわからないところがあります 112 113 114 115クイックソートの際、3つの条件で別々のリストに代入していますが 116各条件にある以下の部分の一行目の存在意義がいまいちわかりません 117 118c->next = l_pivot; 119l_pivot = c; 120 121わかる方教えてください
回答2件
あなたの回答
tips
プレビュー