【发布时间】:2017-01-03 17:13:44
【问题描述】:
#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 choice\n");
}
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. Exit\n");
printf("1. Add item to the queue\n");
printf("2. Remove item from the queue\n");
printf("3. Print queue\n");
printf("4. Print the maximum item in the queue\n");
printf("5. Print the minimum item in the queue\n");
printf("6. Print index of given item\n");
printf("7. Clear queue\n");
printf("8. Print the menu\n");
}
/************************************************************************
* 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 Memory\n");
return;
}
queue[*head]=(int *)malloc(sizeof(int)); //builds size for the data
if (queue[*head]==NULL){
printf("Error: Insufficient Memory\n");
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 中实现一个队列:它作为先入先出的方式工作。
问题是:
- “RemoveItemFromQueue”功能不起作用
- “PrintQueue”函数由于某种原因无法工作。
请注意,我知道我的程序中没有释放 malloc,稍后再处理。
【问题讨论】:
-
您是否使用调试器逐行执行代码?这可能会让您轻松查明您遇到的问题的性质和位置。如果您以前没有使用过调试器,那么您应该知道,无论出于何种意图和目的,使用调试器都是任何程序员所必需的知识。有关详细信息,请参阅How to debug small programs。
-
int **queue=(int **)malloc(1);-->int *queue=NULL; -
首先,我更改了 pastebin 并为每个功能添加了技术规范。其次,我改了还是不行。
-
请张贴代码 - 不是代码链接 :-)
-
“不起作用”是什么意思?