#include<stdio.h>
typedef struct _node{
struct _node *Rnode;
struct _node *Lnode;
int var;
} node;
typedef struct _PQ{
node *one;
} PQ;
void insert(PQ *head, int input){
node *temp = (node*)malloc(sizeof(node));
temp->Lnode = NULL;
temp->Rnode = NULL;
temp->var = input;
if (head->one == NULL){
head->one = temp;
return;
}
node *dir = head->one;
while (1){
if (dir->var < input){
if (dir->Rnode == NULL){
dir->Rnode = temp;
break;
}
else{
dir = dir->Rnode;
}
}
else{
if (dir->Lnode == NULL){
dir->Lnode = temp;
break;
}
else{
dir = dir->Lnode;
}
}
}
}
void del(PQ *head, int input){
node *temp1;
node *temp2;
if (head->one == NULL){
printf("없어 없다고!빼에에엑\n");
return;
}
node *dir;
if (head->one->var == input){
temp1 = head->one->Lnode;
temp2 = head->one->Rnode;
printf("있엉: %d\n", head->one->var);
head->one = temp1;
dir = head->one;
if (temp1 == NULL){
head->one = temp2;
return;
}
else{
while (dir->Rnode != NULL){
dir = dir->Rnode;
}
dir->Rnode = temp2;
}
return;
}
dir = head->one;
while (1){
if (dir->var < input){
if (dir->Rnode == NULL){
printf("없어\n");
return;
}
else{
if (dir->Rnode->var == input){
printf("있엉: %d\n", input);
temp1 = dir->Rnode->Rnode;
temp2 = dir->Rnode->Lnode;
dir->Rnode = temp1;
if (temp1 == NULL){
dir->Rnode = temp2;
return;
}
while (dir->Rnode != NULL){
dir = dir->Rnode;
}
dir->Rnode = temp2;
break;
return;
}
dir = dir->Rnode;
}
}
else{
if (dir->Lnode == NULL){
printf("없어\n");
return;
}
else{
if (dir->Lnode->var == input){
printf("있엉: %d\n", input);
if (dir->Lnode->var == input){
temp1 = dir->Lnode->Rnode;
temp2 = dir->Lnode->Lnode;
dir->Lnode = temp1;
if (temp1 == NULL){
dir->Lnode = temp2;
return;
}
while (dir->Rnode != NULL){
dir = dir->Rnode;
}
dir->Rnode = temp2;
break;
return;
}
dir = dir->Rnode;
}
dir = dir->Lnode;
}
}
}
}
void main(){
PQ *PQhead = (PQ *)malloc(sizeof(PQ));
PQhead->one = NULL;
int i;
insert(PQhead, 10);
insert(PQhead, 5);
insert(PQhead, 20);
insert(PQhead, 100);
insert(PQhead, 30);
insert(PQhead, 1);
del(PQhead, 10);
del(PQhead, 10);
del(PQhead, 20);
del(PQhead, 5);
del(PQhead, 100);
del(PQhead, 30);
del(PQhead, 1);
del(PQhead, 10);
del(PQhead, 5);
del(PQhead, 20);
del(PQhead, 100);
del(PQhead, 30);
del(PQhead, 1);
}