A więc muszę stworzyć program, który posortuje mi np: RRBWWBWBR według kolejności flagi holenderskiej, czyli na RRRWWWBBB.
Pomysł na to mam taki aby podzielić jedną listę na 3 do każdej wpakować jedną grupę a następnie wyzerować główną listę i tam wrzucić to w dobrej kolejności.
Niestety po wrzuceniu do list danych, kiedy próbuję ją wyświetlić listę nic się nie dzieje.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//struktura danych
typedef struct node {
struct node *next;
char* data;
} node_t;
//dodawanie elementu do listy
void push_top(node_t **head, char* data){
node_t *current;
current = (node_t *)malloc(sizeof(node_t));
current->data = strcpy(malloc(strlen(data) + 1), data);
current->next=(*head);
*head=current;
}
//wyświetlanie listy
void show(node_t *head){
node_t *current=head;
while (current != NULL){
printf("%s\n", current->data);
current = current->next;
}
}
//podzielenie listy na 3 listy
void sort(node_t **head){ //w tej funkcji jest poroblem!
node_t *current = *head;
//Stworzenie 3 list
node_t *left = (node_t *)malloc(sizeof(node_t));
node_t *right = (node_t *)malloc(sizeof(node_t));
node_t *center = (node_t *)malloc(sizeof(node_t));
while(current != NULL){
if(current -> data == "R"){
push_top(&left, current -> data);
}else if(current -> data == "W"){
push_top(¢er, current -> data);
}else if(current -> data == "B"){
push_top(&right, current -> data);
}
current = current -> next;
}
//show(left);
}
int main(){
node_t *head;
head = (node_t *)malloc(sizeof(node_t));
head=NULL;
//dodawnie kolorów do listy
push_top(&head, "R");
push_top(&head, "R");
push_top(&head, "R");
push_top(&head, "W");
push_top(&head, "W");
push_top(&head, "W");
push_top(&head, "B");
push_top(&head, "B");
push_top(&head, "B");
sort(&head);
//show(head);
return 0;
}
Z góry dziękuję za jakąkolwiek pomoc.