Write C/C++ program which will repeatedly prompt for and read command from the terminal and perform desired action.
The commands to be supported are:
Add an item into the queue at a given priority, e.g.
Priority: 1
Item:item 1
Print the current contents of the queue.
1: item 1, next item, …
…
9: my item, …
Reverse – toggle the priority order of the queue, e.g.
9: my item, …
…
1: item 1, next item, …
Remove the highest priority item form the queue and print it, e.g.
Removed: my item
Specifications of the priority queue:
·Items added into queue may have priority in range 0 to 9
·Initially, 0 is the highest priority.
·Within one priority level, items should be maintained as FIFO.
Hints:
· It is not imperative that you complete application.
· Parts of application you did not finished comment:
1.Why did you not implement?
2.How would you implement?
===========================================
The commands to be supported are:
Add an item into the queue at a given priority, e.g.
Priority: 1
Item:item 1
Print the current contents of the queue.
1: item 1, next item, …
…
9: my item, …
Reverse – toggle the priority order of the queue, e.g.
9: my item, …
…
1: item 1, next item, …
Remove the highest priority item form the queue and print it, e.g.
Removed: my item
Specifications of the priority queue:
·Items added into queue may have priority in range 0 to 9
·Initially, 0 is the highest priority.
·Within one priority level, items should be maintained as FIFO.
Hints:
· It is not imperative that you complete application.
· Parts of application you did not finished comment:
1.Why did you not implement?
2.How would you implement?
===========================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
typedef int BOOL;
struct item {
char *info;
struct item *next;
} ;
struct item *queue[10 ];
BOOL swcmd(int );
void add_item(int priority, char * buf);
void print_item(void );
void rm_item(void );
struct item *get_last_item(int priority); // get the last item with the priority.
void free_item(struct item * tmp);
void flush_in(void); // to flush input buffer.
int main(void) {
int ch, loop = TRUE;
while (loop) {
printf("type a command to run the program(h for help): ");
ch = getchar();
flush_in();
loop = swcmd(ch);
}
exit(0);
}
/**/ /*
* to process the different command.
*
*/
BOOL swcmd(int ch) {
BOOL ret = TRUE;
int priority;
char buf[256];
switch (ch) {
case 'a' :
printf("add an item./n");
printf("priority: ");
scanf("%d", &priority);
flush_in();
if ((priority < 0) || (priority > 9)) {
printf("priority must be in 0 to 9./n");
break;
}
printf("item: ");
scanf("%s", buf);
flush_in();
add_item(priority, buf); // maybe unsucessful.
break;
case 'p' :
printf("print the items./n");
print_item();
break;
case 'o' :
rm_item();
break;
case 'h' :
printf("a -- add an item to the queue./n");
printf("p -- print the queue./n");
printf("o -- take out the highest priority item./n");
printf("h -- show this help information./n");
printf("q -- quit this program./n");
break;
case 'q' :
ret = FALSE;
break;
default :
printf("an unrecgnized command!/n");
}
return ret;
}
/**/ /*
* add an item to the date structure.
*
*/
void add_item(int priority, char *buf) {
struct item *tmp, *last;
tmp = (struct item *)malloc(sizeof(struct item));
if (tmp == NULL) {
perror("add_item::malloc");
exit(1);
}
tmp->info = strdup(buf);
if (tmp->info == NULL) {
perror("add_item::strdup");
exit(1);
}
tmp->next = NULL;
last = get_last_item(priority);
if (last == NULL) {
queue[priority] = tmp;
return;
}
last->next = tmp;
return;
}
/**/ /*
* to show the items on the screen.
*
*/
void print_item(void) {
int i;
struct item *tmp;
for (i = 0; i < 10; i++) {
printf("%d: ", i);
tmp = queue[i];
while (tmp != NULL) {
printf("%s, ", tmp->info);
tmp = tmp->next;
}
printf("-/n");
}
return ;
}
/**/ /*
* get the last item position with the same priority.
*
*/
struct item *get_last_item(int priority) {
struct item *tmp;
tmp = queue[priority];
if (tmp == NULL) {
return tmp;
}
while (tmp->next != NULL) {
tmp = tmp->next;
}
return tmp;
}
/**/ /*
* output the highest priority item,and remove it from the queue.
*
*/
void rm_item(void) {
int i;
struct item *tmp;
for (i = 9; i >= 0; i--) {
if (queue[i] == NULL) {
continue;
}
tmp = queue[i];
queue[i] = tmp->next;
break;
}
if (i < 0) { // a trap to remove nothing.
return;
}
printf("the highest priority item: [%d] %s/n", i, tmp->info);
free_item(tmp);
print_item();
return;
}
/**/ /*
* to free the struct item.
*
*/
void free_item(struct item *tmp) {
free(tmp->info);
free(tmp);
}
/**/ /*
* to flush the input buffer.
*
*/
void flush_in(void) {
int ch;
ch = getchar();
while (ch != '/n') {
ch = getchar();
}
return ;
}
#include <stdlib.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
typedef int BOOL;
struct item {
char *info;
struct item *next;
} ;
struct item *queue[10 ];
BOOL swcmd(int );
void add_item(int priority, char * buf);
void print_item(void );
void rm_item(void );
struct item *get_last_item(int priority); // get the last item with the priority.
void free_item(struct item * tmp);
void flush_in(void); // to flush input buffer.
int main(void) {
int ch, loop = TRUE;
while (loop) {
printf("type a command to run the program(h for help): ");
ch = getchar();
flush_in();
loop = swcmd(ch);
}
exit(0);
}
/**/ /*
* to process the different command.
*
*/
BOOL swcmd(int ch) {
BOOL ret = TRUE;
int priority;
char buf[256];
switch (ch) {
case 'a' :
printf("add an item./n");
printf("priority: ");
scanf("%d", &priority);
flush_in();
if ((priority < 0) || (priority > 9)) {
printf("priority must be in 0 to 9./n");
break;
}
printf("item: ");
scanf("%s", buf);
flush_in();
add_item(priority, buf); // maybe unsucessful.
break;
case 'p' :
printf("print the items./n");
print_item();
break;
case 'o' :
rm_item();
break;
case 'h' :
printf("a -- add an item to the queue./n");
printf("p -- print the queue./n");
printf("o -- take out the highest priority item./n");
printf("h -- show this help information./n");
printf("q -- quit this program./n");
break;
case 'q' :
ret = FALSE;
break;
default :
printf("an unrecgnized command!/n");
}
return ret;
}
/**/ /*
* add an item to the date structure.
*
*/
void add_item(int priority, char *buf) {
struct item *tmp, *last;
tmp = (struct item *)malloc(sizeof(struct item));
if (tmp == NULL) {
perror("add_item::malloc");
exit(1);
}
tmp->info = strdup(buf);
if (tmp->info == NULL) {
perror("add_item::strdup");
exit(1);
}
tmp->next = NULL;
last = get_last_item(priority);
if (last == NULL) {
queue[priority] = tmp;
return;
}
last->next = tmp;
return;
}
/**/ /*
* to show the items on the screen.
*
*/
void print_item(void) {
int i;
struct item *tmp;
for (i = 0; i < 10; i++) {
printf("%d: ", i);
tmp = queue[i];
while (tmp != NULL) {
printf("%s, ", tmp->info);
tmp = tmp->next;
}
printf("-/n");
}
return ;
}
/**/ /*
* get the last item position with the same priority.
*
*/
struct item *get_last_item(int priority) {
struct item *tmp;
tmp = queue[priority];
if (tmp == NULL) {
return tmp;
}
while (tmp->next != NULL) {
tmp = tmp->next;
}
return tmp;
}
/**/ /*
* output the highest priority item,and remove it from the queue.
*
*/
void rm_item(void) {
int i;
struct item *tmp;
for (i = 9; i >= 0; i--) {
if (queue[i] == NULL) {
continue;
}
tmp = queue[i];
queue[i] = tmp->next;
break;
}
if (i < 0) { // a trap to remove nothing.
return;
}
printf("the highest priority item: [%d] %s/n", i, tmp->info);
free_item(tmp);
print_item();
return;
}
/**/ /*
* to free the struct item.
*
*/
void free_item(struct item *tmp) {
free(tmp->info);
free(tmp);
}
/**/ /*
* to flush the input buffer.
*
*/
void flush_in(void) {
int ch;
ch = getchar();
while (ch != '/n') {
ch = getchar();
}
return ;
}