/*
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <math.h>
#define MAX 1000
int numstack[1000];
int stack[1000];
char charstack[1000];
char equation[1000];
int numtop=0;
int chartop=0;
int top=0;
void convert();
void calculator();
void convert()
{
int i;
for(i=0;i<strlen(equation);i++)
{
//printf("------------------------------------------\n");
if(equation[i]>=48&&equation[i]<=57)
{
int temp=0;
while(equation[i]>=48&&equation[i]<=57)
{
stack[top++]=equation[i]-'0';
i++;
}
int cnt=0;
while(top>0)
{
temp+=(stack[top-1])*pow(10.0, cnt);
top--;
}
numstack[numtop++]=temp;
}
if(equation[i]<48||equation[i]>57)
{
if(equation[i]==')'||equation[i]=='}'||equation[i]==']')
calculator();
else
charstack[chartop++]=equation[i];
}
}
calculator();
}
void calculator()//괄호 계산은 1번 , 그냥 계산은 0번
{
while(chartop>0)
{
int x, y;
int temp;
x=numstack[numtop-2];
y=numstack[numtop-1];
if(charstack[chartop-1]=='('||charstack[chartop-1]=='{'||charstack[chartop-1]=='[')
{
chartop--;
return;
}
if(charstack[chartop-1]=='+')
{
temp=x+y;
numtop=numtop-2;
numstack[numtop++]=temp;
}
else if(charstack[chartop-1]=='-')
{
temp=x-y;
numtop=numtop-2;
numstack[numtop++]=temp;
}
else if(charstack[chartop-1]=='*')
{
temp=x*y;
numtop=numtop-2;
numstack[numtop++]=temp;
}
else if(charstack[chartop-1]=='/')
{
temp=x/y;
numtop=numtop-2;
numstack[numtop++]=temp;
}
chartop--;
}
}
int main()
{
scanf("%s", equation);
// printf("%s\n", equation);
convert();
printf("%d\n", numstack[numtop-1]);
return 0;
}
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
typedef struct node
{
int data;
node *rlink;
node *llink;
}node;
node* queue[1000];
int front=0;
int rear=0;
node *start;
node *current;
node *before;
//int nodes=1;
void MAXheap();
void deletemax();
void menu();
void print();
void MAXheap()
{
printf("---------------------------------------------------\n");
int num;
printf("please enter a integer you want to insert.\n");
scanf("%d", &num);
while(num<=0)
{
printf("please enter a integer.\n");
scanf("%d", &num);
}
if(start->data==0)
{
start->data=num;
printf("insert complete.\n");
printf("---------------------------------------------------\n");
return;
}
node *newnode;
newnode=(node*)malloc(sizeof(node));
//newnode->index=nodes++;
newnode->llink=NULL;
newnode->rlink=NULL;
newnode->data=num;
if(current->llink==NULL)
current->llink=newnode;
else
current->rlink=newnode;
//비교
if(current->data<newnode->data)
{
int temp;
temp=current->data;
current->data=newnode->data;
newnode->data=temp;
}
before=current;
current=newnode;
printf("insert complete.\n");
printf("---------------------------------------------------\n");
}
void deletemax()
{
printf("---------------------------------------------------\n");
if(start->data==0)
{
printf("the heap is empty\n");
return;
}
else if(start->llink==NULL)
{
printf("delete complete\n");
start->data=0;
return;
}
node *temp;
temp=start;
start->data=current->data;
free(current);
current=before;
while(1)
{
if(temp->llink==NULL||(temp->llink->data>=temp->data&&(temp->rlink->data>=temp->data||temp->rlink==NULL)))
break;
else
{
int t;
if(temp->llink->data<temp->data)
{
t=temp->llink->data;
temp->llink->data=temp->data;
temp->data=t;
}
else
{
t=temp->rlink->data;
temp->rlink->data=temp->data;
temp->data=t;
}
}
}
printf("delete complete.\n");
printf("---------------------------------------------------\n");
}
void menu()
{
printf("---------------------------------------------------\n");
printf("Welcome to heap management\n");
int m=0;
start = (node*)malloc(sizeof(node));
//start->index=nodes++;
start->rlink=NULL;
start->llink=NULL;
start->data=0;
current=start;
while(m!=4)
{
printf("press 1 to insert, 2 to delete the max node,3 to print, 4 to quit.\n");
scanf("%d", &m);
if(m==1)
MAXheap();
else if(m==2)
deletemax();
else if(m==3)
print();
else if(m==4)
{
printf("BYE.");
return;
}
else
printf("Please enter a number between 1 and 3.\n");
printf("---------------------------------------------------\n");
}
}
void print()
{
if(start->data==0)
{
printf("the heap is empty.\n");
printf("---------------------------------------------------\n");
return;
}
queue[rear++]=start;
node *temp;
while(1)
{
int i;
for(i=front;i<rear;i++)
{
printf("%d ", queue[i]->data);
if(queue[i]==current)
{
printf("\n");
front=0;
rear=0;
printf("---------------------------------------------------\n");
return;
}
}
printf("\n");
int t=rear;
for(i=front;i<t;i++)
{
if(queue[i]->llink!=NULL)
queue[rear++]=queue[i]->llink;
if(queue[i]->rlink!=NULL)
queue[rear++]=queue[i]->rlink;
}
front=t;
}
}
int main()
{
menu();
return 0;
}