/*
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int data;
struct _node *next;
}Node;
Node *head = NULL;
Node *tail = NULL;
Node *cur = NULL;
void printNode() {
if (head == NULL) {
printf("입력된 데이터가 없음\n");
return;
}
cur = head;
printf("Linked List: ");
while (cur != NULL) {
printf("%d", cur->data);
if (cur->next != NULL) printf(" -> ");
cur = cur->next;
}
printf("\n\n");
}
void searchNode() {
if (head == NULL) {
printf("입력된 값 없음\n");
return;
}
int seD, pos = 1;
printf("검색할 값 입력: ");
scanf("%d", &seD);
cur = head;
while (cur != NULL) {
if (cur->data == seD) {
printf("\n데이터(%d)는 %d번째 노드에 있습니다.\n", seD, pos);
return;
}
cur = cur->next;
pos++;
}
printf("일치하는 데이터가 없습니다.\n");
}
void insertNode() {
int inD, inPos;
printf("입력할 값(1보다 작은 값 입력 시 종료): ");
scanf("%d", &inD);
if (inD < 1) {
printf("1보다 작은 값은 입력할 수 없음. 입력 종료\n");
return;
}
Node *inN = (Node *)malloc(sizeof(Node));
inN->data = inD;
inN->next = NULL;
if (head == NULL) {
head = tail = inN;
printNode();
return;
}
printf("\n입력할 위치 (1 = 맨 앞): ");
scanf("%d", &inPos);
if (inPos <= 1) {
inN->next = head;
head = inN;
printNode();
return;
}
cur = head;
for (int i = 1; i < inPos - 1 && cur->next != NULL; i++) {
cur = cur->next;
}
inN->next = cur->next;
cur->next = inN;
if (inN->next == NULL) {
tail = inN;
}
printNode();
}
void deleteNode() {
if (head == NULL) {
printf("입력된 데이터 없음\n");
return;
}
int deD;
printf("삭제할 값 입력: ");
scanf("%d", &deD);
Node *prev = NULL;
cur = head;
while (cur != NULL && cur->data != deD) {
prev = cur;
cur = cur->next;
}
if (cur == NULL) {
printf("일치하는 값이 없습니다.\n");
return;
}
if (cur == head) {
head = head->next;
if (cur == tail) {
tail = NULL;
}
}
else {
prev->next = cur->next;
if (cur == tail) {
tail = prev;
}
}
printf("\n%d 삭제됨\n", cur->data);
free(cur);
printNode();
}
int main() {
int newD;
printf("\n초기 노드 값 입력 (값이 1보다 작으면 작성 종료): \n");
while (1) {
scanf("%d", &newD);
if (newD < 1) break;
Node *newN = (Node *)malloc(sizeof(Node));
newN->data = newD;
newN->next = NULL;
if (head == NULL) {
head = newN;
}
else {
tail->next = newN;
}
tail = newN;
}
printNode();
while (1) {
int tyno;
printf("\n\ntype 1 : View All Data"
"\ntype 2 : Search Data Location"
"\ntype 3 : Insert Data"
"\ntype 4 : Delete Data"
"\ntype 0 : Stop\n\nWrite down type #: ");
scanf("%d", &tyno);
printf("\n");
if (tyno == 0) break;
if (tyno == 1) printNode();
else if (tyno == 2) searchNode();
else if (tyno == 3) insertNode();
else if (tyno == 4) deleteNode();
else printf("잘못된 입력입니다.\n");
}
cur = head;
while (cur != NULL) {
Node *rst = cur;
cur = cur->next;
free(rst);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int data;
struct _node *next;
struct _node *prev;
}Node;
Node *head = NULL;
Node *tail = NULL;
Node *cur = NULL;
void distMiddle() {
Node *foc = NULL;
Node *bac = NULL;
if (head == NULL) {
printf("입력된 값 없음\n");
return;
}
int miD;
printf("판별할 값 입력: ");
scanf("%d", &miD);
cur = head;
while (cur != NULL) {
if (cur->data == miD) {
break;
}
cur = cur->next;
if(cur == NULL){
printf("일치하는 데이터가 없습니다.\n");
}
}
foc = cur;
bac = cur;
int cof = 0, cob = 0;
while(foc != NULL) {
foc = foc -> prev;
cof++;
}
while(bac != NULL) {
bac = bac -> next;
cob++;
}
int m = (cof==cob);
printf("%d", m);
return;
}
void printNodeForward() {
if (head == NULL) {
printf("Forward: 입력된 데이터가 없음\n");
return;
}
cur = head;
printf("Forward: ");
while (cur != NULL) {
printf("%d", cur->data);
if (cur->next != NULL) printf(" -> ");
cur = cur->next;
}
printf("\n");
}
void printNodeBackward() {
if (tail == NULL) {
printf("Backward: 입력된 데이터가 없음\n");
return;
}
cur = tail;
printf("Backward: ");
while (cur != NULL) {
printf("%d", cur->data);
if (cur->prev != NULL) printf(" <- ");
cur = cur->prev;
}
printf("\n\n");
}
void searchNode() {
if (head == NULL) {
printf("입력된 값 없음\n");
return;
}
int seD, pos = 1;
printf("검색할 값 입력: ");
scanf("%d", &seD);
cur = head;
while (cur != NULL) {
if (cur->data == seD) {
printf("\n데이터(%d)는 %d번째 노드에 있습니다.\n", seD, pos);
return;
}
cur = cur->next;
pos++;
}
printf("일치하는 데이터가 없습니다.\n");
}
void insertNode() {
int inD;
printf("입력할 값(1보다 작은 값 입력 시 종료): ");
scanf("%d", &inD);
if (inD < 1) {
printf("1보다 작은 값은 입력할 수 없음. 입력 종료\n");
return;
}
Node *inN = (Node *)malloc(sizeof(Node));
inN->data = inD;
inN->next = NULL;
inN->prev = NULL;
if (head == NULL) {
head = tail = inN;
printNodeForward();
printNodeBackward();
return;
}
if (inD <= head->data) {
inN->next = head;
head->prev = inN;
head = inN;
printNodeForward();
printNodeBackward();
return;
}
cur = head;
while(cur->next != NULL && cur->next->data < inD) {
cur = cur->next;
}
inN->next = cur->next;
inN->prev = cur;
if (cur->next != NULL) {
cur->next->prev = inN;
} else {
tail = inN;
}
cur->next = inN;
printNodeForward();
printNodeBackward();
return;
}
void deleteNode() {
if (head == NULL) {
printf("입력된 데이터 없음\n");
return;
}
int deD;
printf("삭제할 값 입력: ");
scanf("%d", &deD);
cur = head;
while (cur != NULL && cur->data != deD) {
cur = cur->next;
}
if (cur == NULL) {
printf("일치하는 값이 없습니다.\n");
return;
}
if (cur == head) {
head = head->next;
if (head != NULL) head->prev = NULL;
if (cur == tail) tail = NULL;
}
else if (cur == tail) {
tail = tail->prev;
tail->next = NULL;
}
else {
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
}
printf("\n%d 삭제됨\n", cur->data);
free(cur);
printNodeForward();
printNodeBackward();
}
int main() {
while (1) {
int tyno;
printf("\n\ntype 1 : View All Data (Forward & Backward)"
"\ntype 2 : Search Data Location"
"\ntype 3 : Insert Data"
"\ntype 4 : Delete Data"
"\ntype 5 : Distinguish Location of Data if it is Middle"
"\ntype 0 : Stop\n\nWrite down type #: ");
scanf("%d", &tyno);
printf("\n");
if (tyno == 0) break;
if (tyno == 1) { printNodeForward(); printNodeBackward(); }
else if (tyno == 2) searchNode();
else if (tyno == 3) insertNode();
else if (tyno == 4) deleteNode();
else if (tyno == 5) distMiddle();
else printf("잘못된 입력입니다.\n");
}
cur = head;
while (cur != NULL) {
Node *tmp = cur;
cur = cur->next;
free(tmp);
}
return 0;
}
*/
int base[16] = {0};
int c, v, w;
int s = 1;
void pri(){
printf(" %d\n %d %d\n %d %d %d %d\n%d %d %d %d %d %d %d %d",
base[1], base[2], base[3], base[4], base[5],
base[6], base[7], base[8], base[9], base[10],
base[11], base[12], base[13], base[14], base[15]);
return;
}
void f(int c){
if(c > base[s]){
s = 2*s + 1;
if(s > 15){
s /= 4;
v = base[2*s + 1];
base[2*s + 1] = c;
w = base[s];
base[s] = v;
printf("base[%d] = %d, base[%d] = %d\n", s, v, 2*s + 1, c);
s = 1;
return f(w);
}
if(base[s] == 0){
base[s] = c;
printf("base[%d] = %d\n", s, c);
s = 1;
return;
}
else if(base[s] != 0){
if(base[s] > c){
v = base[s];
base[s] = c;
printf("base[%d] = %d, return\n", s, c);
s = 1;
return f(v);
}
else{
printf("return\n");
return f(c);
}
}
}
else{
s = 2*s;
if(s > 15){
s /= 4;
v = base[2*s];
base[2*s] = c;
w = base[s];
base[s] = v;
printf("base[%d] = %d, base[%d] = %d\n", s, v, 2*s, c);
s = 1;
return f(w);
}
if(base[s] == 0){
base[s] = c;
printf("base[%d] = %d\n", s, c);
s = 1;
return;
}
else if(base[s] != 0){
if(base[s] < c){
v = base[s];
base[s] = c;
printf("base[%d] = %d, return\n", s, c);
s = 1;
return f(v);
}
else{
s = s/2;
printf("return\n");
return f(c);
}
}
}
}
int main(){
for(int i = 1; i < 16; i++){
scanf("%d", &c);
if(base[1] == 0){
base[1] = c;
printf("base[1]\n");
continue;
}
else{
f(c);
}
}
pri();
}



