#include <stdlib.h>
#include <stdio.h>
struct TNode {
int data;
struct TNode *next;
};
typedef struct TNode Node;
struct TQueue {
Node* head;
Node* tail;
};
typedef struct TQueue Queue;
Queue* newQueue()
{
Queue* queue = malloc(sizeof(Queue));
queue->head = NULL;
queue->tail = NULL;
return queue;
}
void deleteQueue(Queue* queue)
{
Node* current = queue->head;
while (current != NULL) {
Node* next = current->next;
free(current);
current = next;
}
free(queue);
}
void pushBackQueue(Queue* queue, int data)
{
Node* newNode = malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (queue->tail == NULL) {
queue->head = newNode;
queue->tail = newNode;
}
else {
queue->tail->next = newNode;
queue->tail = newNode;
}
}
void splitQueue(Queue* source, Queue* even, Queue* odd)
{
for (Node* current = source->head; current != NULL; current = current->next) {
int data = current->data;
pushBackQueue((data % 2 == 0) ? even : odd, data);
}
}
void printQueue(Queue* queue)
{
for (Node* current = queue->head; current != NULL; current = current->next) {
printf("%d ", current->data);
}
printf("\n");
}
int main()
{
Queue* source = newQueue();
for (int i = 0; i < 20; i++) {
pushBackQueue(source, i + 1);
}
printQueue(source);
Queue* even = newQueue();
Queue* odd = newQueue();
splitQueue(source, even, odd);
printQueue(even);
printQueue(odd);
deleteQueue(source);
deleteQueue(even);
deleteQueue(odd);
system("pause"); // not needed in Linux
return 1;
}