#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include "Mission6.h"
/************************************************************************
* function name:MissionSix
* input:none
* output:none
* operation:gets a choice from the user makes a menu to choose from.
*************************************************************************/
void MissionSix(){
static int queueTail=0,queueHead=0,amountOfTimes=0; //so their values stay throughout the program.
//amountOfItems represents the amount of times i used the functionAddItemToQueue
int choice;
int **queue=NULL;
PrintMenu();
do{
scanf("%d",&choice);
switch(choice){
case 0:
return;
case 1:
AddItemToQueue(queue,&queueHead,&queueTail,&amountOfTimes);
break;
case 2:
RemoveItemFromQueue(queue,&queueHead,&queueTail);
break;
case 3:
PrintQueue(queue,&queueHead,&queueTail);
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
PrintMenu();
break;
default:
printf("Error: Unrecognized choicen");
}
printf("Please select your next choice (select 8 for complete menu)n");
} //end of "do"
while(choice!=0);
}
/************************************************************************
* function name:printMenu
* input:none
* output:none
* operation:prints the menu.
*************************************************************************/
void PrintMenu(){
printf("Please select your choice:n");
printf("0. Exitn");
printf("1. Add item to the queuen");
printf("2. Remove item from the queuen");
printf("3. Print queuen");
printf("4. Print the maximum item in the queuen");
printf("5. Print the minimum item in the queuen");
printf("6. Print index of given itemn");
printf("7. Clear queuen");
printf("8. Print the menun");
}
/************************************************************************
* function name:AddItemToQueue
* input: the queue (an array of pointers), its head(integer), its tail(int) and the amount
of times this function was called(int)
* output:none
* operation:gets an item from the user and adds it to the queue.
*************************************************************************/
void AddItemToQueue(int **queue,int *head,int* tail,int* amountOfTimes){
int itemToAdd;
printf("Enter item value to add n");
scanf("%d",&itemToAdd);
queue=(int **)realloc(queue,sizeof(int*)*((*amountOfTimes)+1)); //amount of items in queue =*head - *tail
if(queue==NULL){
printf("Error: Insufficient Memoryn");
return;
}
queue[*head]=(int *)malloc(sizeof(int)); //builds size for the data
if (queue[*head]==NULL){
printf("Error: Insufficient Memoryn");
return;
}
*queue[*head]=itemToAdd;//adding the item to the correct spot
(*head)++;
(*amountOfTimes)++;
}
/************************************************************************
* function name:RemoveItemFromQueue
* input:the queue (an array of pointers), its head(integer) and its tail(int).
* output:none
* operation:removes first element in the queue.
*************************************************************************/
void RemoveItemFromQueue(int **queue,int *head, int* tail){
if((*head)==(*tail)){ //if head and tail are equal the queue is empty.
printf("Error: Queue is empty!n");
return;
}
free(queue[*tail]);
(*tail)++;
}
/************************************************************************
* function name:PrintQueue
* input:the queue (an array of pointers), its head(integer) and its tail(int).
* output:none
* operation:prints the elements of the queue
*************************************************************************/
void PrintQueue(int **queue,int *head, int*tail){
int i;
printf("Queue items are: ");
for(i=(*tail);i<(*head);i++){
printf("%d ",**(queue+i));
}
}
我正在尝试做的是在 C 中实现一个队列:它作为第一个 IT 中的第一个工作。
问题是:
- 函数"RemoveItemFromQueue"不起作用
- 由于某种原因,函数"打印队列"不起作用。
请注意,我知道我没有在我的程序中释放 mallocs,稍后再处理。
试试这个(如果我的理解是错误的,我很抱歉):
#include <stdio.h>
#include <stdlib.h>
//#include "Mission6.h"
void PrintMenu(void);
void AddItemToQueue(int **queue,int *head,int* tail,int* amountOfTimes);
void RemoveItemFromQueue(int **queue,int *head, int* tail);
void PrintQueue(int *queue, int head, int tail);
void MissionSix(void){
int queueTail = 0, queueHead = 0, amountOfTimes = 0;
//amountOfItems represents the amount of times i used the functionAddItemToQueue
int choice;
int *queue = NULL;
PrintMenu();
do{
scanf("%d", &choice);
switch(choice){
case 0:
free(queue);
return;
case 1:
AddItemToQueue(&queue, &queueHead, &queueTail, &amountOfTimes);
break;
case 2:
RemoveItemFromQueue(&queue, &queueHead, &queueTail);
break;
case 3:
PrintQueue(queue, queueHead, queueTail);
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
PrintMenu();
break;
default:
printf("Error: Unrecognized choicen");
}
printf("Please select your next choice (select 8 for complete menu)n");
} while(choice != 0);
}
void PrintMenu(void){
fputs(
"Please select your choice:n"
"0. Exitn"
"1. Add item to the queuen"
"2. Remove item from the queuen"
"3. Print queuen"
"4. Print the maximum item in the queuen"
"5. Print the minimum item in the queuen"
"6. Print index of given itemn"
"7. Clear queuen"
"8. Print the menun",
stdout
);
}
void AddItemToQueue(int **queue, int *head, int *tail, int *amountOfTimes){
int itemToAdd;
printf("Enter item value to add n");
scanf("%d", &itemToAdd);
*queue = realloc(*queue, ++*amountOfTimes * sizeof(int));
if(*queue == NULL){
printf("Error: Insufficient Memoryn");
return;
}
(*queue)[(*tail)++] = itemToAdd;
}
void RemoveItemFromQueue(int **queue, int *head, int *tail){
//if restructure queue, need int *amountOfTimes
if( *head == *tail){
printf("Error: Queue is empty!n");
return;
}
++*head;
}
void PrintQueue(int *queue, int head, int tail){
int i;
printf("Queue items are: ");
for(i = head; i < tail; ++i){
printf("%d ", queue[i]);
}
puts("");
}
int main(void){
MissionSix();
}