#include "..\CookHeader.h"

typedef struct _Node {
	int data;  // int형 데이터
	struct _Node* link = NULL;
} Node;

Array <Node*> memory;
Node* head, * current, * pre;

void printNodes(Node* start) {
	if (start == NULL)
		return;
	Node* current = start;
	print(current->data);
	while (current->link != start) {
		current = current->link;
		print(current->data);
	}
	println("");
}

void freeMemory() { // 할당된 모든 노드의 메모리를 해제한다.
	for (int i = 0; i < len(memory); i++)
		delete memory[i];
}

Array <int> countPlusMinus() {	// 양수와 음수 개수를 배열로 반환
	Array <int> retData;		// {양수, 음수} 형태 배열
	int plus = 0, minus = 0;

	current = head;
	while (true) {
		if (current->data % 2 != 0)
			plus++;		// 양수
		else
			minus++;	// 음수
		if (current->link == head)
			break;
		current = current->link;
	}
	retData = { plus, minus };
	return retData;
}

void makeReverseNumber(int plus, int minus) {
	current = head;
	while (true) {
		if (plus > minus) {
			if (current->data < 0)
				current->data *= -1;
		}
		else {
			if (current->data > 0)
				current->data *= -1;
		}
		if (current->link == head)
			break;
		current = current->link;
	}
}

int main() {
	srand(time(NULL));

	Array <int> dataArray;
	for (int i = 0; i < 7; i++)
		dataArray.push_back(rand() % 201 - 100); // -100 ~ 100 랜덤 숫자

	Node* node = new Node;
	node->data = dataArray[0]; // 첫 번째 노드
	head = node;
	node->link = head;
	memory.push_back(node);

	for (int i = 1; i < len(dataArray); i++) {  // 두 번째 이후 노드
		int data = dataArray[i];
		pre = node;
		node = new Node;
		node->data = data;
		pre->link = node;
		node->link = head;
		memory.push_back(node);
	}

	Array <int> retCount = countPlusMinus(); // {홀수, 짝수}로 반환
	int oddCount = retCount[0];
	int evenCount = retCount[1];

	print("원래 숫자-->");
	printNodes(head);
	println("양수-->" + str(oddCount));
	println("음수-->" + str(evenCount));
	print("변경 숫자-->");
	makeReverseNumber(oddCount, evenCount);
	printNodes(head);

	freeMemory();
}