gpt4 book ai didi

c - 队列实现

转载 作者:行者123 更新时间:2023-11-30 19:10:22 24 4
gpt4 key购买 nike

#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中实现一个队列:它作为第一个队列工作。

问题是:

  1. “RemoveItemFromQueue”功能不起作用
  2. “PrintQueue”功能由于某种原因不起作用。

请注意,我知道我还没有在程序中释放 malloc,稍后会处理它。

最佳答案

试试这个(如果我的理解错误,我很抱歉):

#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 choice\n");
}
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. Exit\n"
"1. Add item to the queue\n"
"2. Remove item from the queue\n"
"3. Print queue\n"
"4. Print the maximum item in the queue\n"
"5. Print the minimum item in the queue\n"
"6. Print index of given item\n"
"7. Clear queue\n"
"8. Print the menu\n",
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 Memory\n");
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();
}

关于c - 队列实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41448833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com