Last active
July 14, 2018 08:53
-
-
Save gollilla/6bd5405c5fe57ccb46fa0082fa92d71b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
struct Car{ | |
int id; | |
int count; | |
struct Car *next; | |
}; | |
/** | |
* @return | |
*/ | |
int car_in(struct Car *p, int type); | |
/** | |
* @return | |
*/ | |
int car_out(struct Car *p, int type); | |
/** | |
* @return | |
*/ | |
int print_cars(struct Car *p); | |
int main(void){ | |
char command; | |
int car_id,r=1; | |
struct Car *root=NULL, *tmp; | |
while(1){ | |
printf("COMMAND:"); | |
scanf(" %c",&command); | |
switch(command){ | |
case 'i': | |
printf("CAR_TYPE:"); | |
scanf("%d",&car_id); | |
if(root==NULL){ | |
root = (struct Car *)calloc(1,sizeof(struct Car)); | |
if(root==NULL){ | |
r=-1; | |
}else{ | |
root->id=car_id; | |
root->count=1; | |
root->next=NULL; | |
r=1; | |
} | |
}else{ | |
r=car_in(root,car_id); | |
} | |
break; | |
case 'o': | |
printf("CAR_TYPE:"); | |
scanf("%d",&car_id); | |
if(root->id == car_id){ | |
root->count--; | |
if(root->count == 0){ | |
tmp=root; | |
root=root->next; | |
free(tmp); | |
}else if(root->count < 0){ | |
r=-2; | |
}else{ | |
r=1; | |
} | |
}else{ | |
r=car_out(root,car_id); | |
} | |
break; | |
case 'p': | |
if(root==NULL){ | |
printf("NO Cars\n"); | |
r=1; | |
break; | |
} | |
r=print_cars(root); | |
break; | |
case 'e': | |
r=0; | |
break; | |
case 'h': | |
default: | |
printf("----Command List-----\n"); | |
printf(" i | insert car\n"); | |
printf(" o | out car \n"); | |
printf(" p | print car list\n"); | |
printf(" e | exit \n"); | |
printf(" h | show help\n"); | |
r=1; | |
} | |
if(r==-1){ | |
printf("error:car_in\n"); | |
}else if(r==-2){ | |
printf("error:car_out\n"); | |
}else if(r==-3){ | |
printf("error:print_cars\n"); | |
}else if(r==0){ | |
printf("exit\n"); | |
break; | |
} | |
} | |
return 0; | |
} | |
int car_in(struct Car *p, int type){ | |
while(1){ | |
if(p->id == type){ | |
p->count += 1; | |
return 1; | |
}else{ | |
if(p->next == NULL){ | |
p->next = (struct Car *)malloc(sizeof(struct Car)); | |
if(p->next == NULL) return -1; | |
p->next->id = type; | |
p->next->count = 1; | |
p->next->next = NULL; | |
return 1; | |
} | |
p = p->next; | |
} | |
} | |
} | |
int car_out(struct Car *p, int type){ | |
struct Car *tmp; | |
while(1){ | |
if(p->next->id == type){ | |
p->next->count -= 1; | |
if(p->next->count == 0){ | |
tmp = p->next; | |
p->next = p->next->next; | |
free(tmp); | |
} | |
return 1; | |
} | |
if(p->next->next == NULL) return -2; | |
p = p->next; | |
} | |
} | |
int print_cars(struct Car *p){ | |
while(1){ | |
printf("ID : %d\n", p->id); | |
printf("COUNT : %d\n", p->count); | |
if(p->next == NULL) return 1; | |
p = p->next; | |
} | |
return -3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment