fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. struct node{
  6.  
  7. char name[101];
  8. char event[21];
  9. char kondisi[21];
  10. int prioritas;
  11.  
  12. struct node *next;
  13. struct node *prev;
  14.  
  15. }*head=NULL,*tail=NULL;
  16.  
  17. void pushHead(char name[],char event[],char kondisi[],int prioritas){
  18.  
  19. struct node *curr = (struct node*)malloc(sizeof(struct node));
  20. strcpy(curr->name,name);
  21. strcpy(curr->event,event);
  22. strcpy(curr->kondisi,kondisi);
  23. curr->prioritas = prioritas;
  24.  
  25. if(head==NULL){
  26. head=tail=curr;
  27. }else{
  28.  
  29. head->prev = curr;
  30. curr->next = head;
  31. curr->prev = NULL;
  32. head = curr;
  33.  
  34. }
  35. }
  36.  
  37. void pushTail(char name[],char event[],char kondisi[],int prioritas){
  38.  
  39. struct node *curr = (struct node*)malloc(sizeof(struct node));
  40. strcpy(curr->name,name);
  41. strcpy(curr->event,event);
  42. strcpy(curr->kondisi,kondisi);
  43. curr->prioritas = prioritas;
  44.  
  45. if(head==NULL){
  46. head=tail=curr;
  47. }else{
  48.  
  49. tail->next = curr;
  50. curr->prev = tail;
  51. curr->next = NULL;
  52. tail = curr;
  53.  
  54. }
  55. }
  56.  
  57. void pushMid(char name[],char event[],char kondisi[],int prioritas){
  58.  
  59. struct node *curr = (struct node*)malloc(sizeof(struct node));
  60. strcpy(curr->name,name);
  61. strcpy(curr->event,event);
  62. strcpy(curr->kondisi,kondisi);
  63. curr->prioritas = prioritas;
  64.  
  65. if(head==NULL){
  66. head=tail=curr;
  67. }else if(curr->prioritas < head->prioritas){
  68. pushHead(name,event,kondisi,prioritas);
  69. }else if(curr->prioritas > tail->prioritas){
  70. pushTail(name,event,kondisi,prioritas);
  71. }else{
  72.  
  73. struct node *temp = head;
  74.  
  75. while(curr->prioritas >= temp->prioritas){
  76. temp = temp->next;
  77. }
  78.  
  79. temp->prev = curr;
  80. curr->next = temp;
  81. temp->prev->next = curr;
  82. curr->prev = temp->prev;
  83.  
  84. }
  85. }
  86.  
  87. void view(){
  88.  
  89. struct node *temp = head;
  90.  
  91. if(temp!=NULL){
  92. if(temp->prioritas == 1){
  93. printf("%s is in the emergency room",temp->name);
  94. }else if(temp->prioritas==2){
  95. printf("%s is in the examination room",temp->name);
  96. }else{
  97. printf("%s is in the consultation room",temp->name);
  98. }
  99. }
  100.  
  101. puts("");
  102. puts("WAITING ROOM");
  103.  
  104. while(temp!=0){
  105. printf("%s, ",temp->next->name);
  106. temp = temp->next;
  107. }
  108.  
  109.  
  110.  
  111. }
  112.  
  113. void popHead(){
  114.  
  115. if(head==NULL)return;
  116. else{
  117. struct node *curr = head;
  118. head = head->next;
  119. head->prev = NULL;
  120. // curr= NULL;
  121. free(curr);
  122. curr= NULL;
  123. }
  124. }
  125.  
  126. int main(){
  127.  
  128. int num;
  129. scanf("%d",&num);getchar();
  130.  
  131. char c1[101];
  132. char c2[101];
  133. char c3[101];
  134. int prioritas;
  135.  
  136. for(int i =0 ; i < num ; i++){
  137.  
  138. scanf("%s",c1);getchar();
  139.  
  140. // scanf(" %s %s",c2,c3);
  141.  
  142. if(strcmp(c1,"add")==0){
  143.  
  144. printf("Input patient data : ");
  145. scanf("%s %s",c2,c3);getchar();
  146.  
  147. if(strcmp(c3,"good")==0 || strcmp(c3,"fair")==0){
  148. prioritas = 3;
  149. pushMid(c2,c1,c3,prioritas);
  150. }else if(strcmp(c3,"serious")==0 ){
  151. prioritas = 2;
  152. pushMid(c2,c1,c3,prioritas);
  153. }else if(strcmp(c3,"critical")==0){
  154. prioritas = 1;
  155. pushMid(c2,c1,c3,prioritas);
  156. }
  157.  
  158. }else if(strcmp(c1,"call")==0){
  159.  
  160. view();
  161. popHead();
  162.  
  163. }
  164.  
  165. }
  166.  
  167. return 0;
  168. }
Success #stdin #stdout 0.02s 25808KB
stdin
Standard input is empty
stdout
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct node{
	
	char name[101];
	char event[21];
	char kondisi[21];
	int prioritas;
	
	struct node *next;
	struct node *prev;
	
}*head=NULL,*tail=NULL;

void pushHead(char name[],char event[],char kondisi[],int prioritas){
	
	struct node *curr = (struct node*)malloc(sizeof(struct node));
	strcpy(curr->name,name);
	strcpy(curr->event,event);
	strcpy(curr->kondisi,kondisi);
	curr->prioritas = prioritas;
	
	if(head==NULL){
		head=tail=curr;
	}else{
		
		head->prev = curr;
		curr->next = head;
		curr->prev = NULL;
		head = curr;

	}
}

void pushTail(char name[],char event[],char kondisi[],int prioritas){
	
	struct node *curr = (struct node*)malloc(sizeof(struct node));
	strcpy(curr->name,name);
	strcpy(curr->event,event);
	strcpy(curr->kondisi,kondisi);
	curr->prioritas = prioritas;
	
	if(head==NULL){
		head=tail=curr;
	}else{
		
		tail->next = curr;
		curr->prev = tail;
		curr->next = NULL;
		tail = curr;
		
	}
}

void pushMid(char name[],char event[],char kondisi[],int prioritas){
	
	struct node *curr = (struct node*)malloc(sizeof(struct node));
	strcpy(curr->name,name);
	strcpy(curr->event,event);
	strcpy(curr->kondisi,kondisi);
	curr->prioritas = prioritas;
	
	if(head==NULL){
		head=tail=curr;
	}else if(curr->prioritas < head->prioritas){
		pushHead(name,event,kondisi,prioritas);
	}else if(curr->prioritas > tail->prioritas){
		pushTail(name,event,kondisi,prioritas);
	}else{
		
		struct node *temp = head;
		
		while(curr->prioritas >= temp->prioritas){
			temp = temp->next;
		}
		
			temp->prev = curr;
			curr->next = temp;
			temp->prev->next = curr;
			curr->prev = temp->prev;	
			
	}
}

void view(){
	
	struct node *temp = head;
	
	if(temp!=NULL){
		if(temp->prioritas == 1){
			printf("%s is in the emergency room",temp->name);
		}else if(temp->prioritas==2){
			printf("%s is in the examination room",temp->name);	
		}else{
			printf("%s is in the consultation room",temp->name);
		}
	}
	
	puts("");
	puts("WAITING ROOM");
	
	while(temp!=0){
		printf("%s, ",temp->next->name);
		temp = temp->next;
	}
	
	
	
}

void popHead(){
	
	if(head==NULL)return;
	else{
		struct node *curr = head;
		head = head->next;
		head->prev = NULL;
//		curr= NULL;
		free(curr);
		curr= NULL;
	}
}

int main(){

	int num;
	scanf("%d",&num);getchar();
	
	char c1[101];
	char c2[101];
	char c3[101];
	int prioritas;
	
	for(int i =0 ; i < num ; i++){
		
		scanf("%s",c1);getchar();
		
//		scanf(" %s %s",c2,c3);
		
			if(strcmp(c1,"add")==0){
				
				printf("Input patient data : ");
				scanf("%s %s",c2,c3);getchar();
				
				if(strcmp(c3,"good")==0 || strcmp(c3,"fair")==0){
					prioritas = 3;
					pushMid(c2,c1,c3,prioritas);
				}else if(strcmp(c3,"serious")==0 ){
					prioritas = 2;
					pushMid(c2,c1,c3,prioritas);
				}else if(strcmp(c3,"critical")==0){
					prioritas = 1;
					pushMid(c2,c1,c3,prioritas);
				}
				
			}else if(strcmp(c1,"call")==0){
				
				 view();
				 popHead();	
				 	
			}
			
		}
		
	return 0;
}