Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
*队列,int*head,int*tail,int*amountomes){ int itemToAdd; printf(“输入要添加的项目值”); scanf(“%d”、&itemToAdd); *queue=realloc(*队列,++*amountO_C_Pointers_Queue_Malloc - Fatal编程技术网

*队列,int*head,int*tail,int*amountomes){ int itemToAdd; printf(“输入要添加的项目值”); scanf(“%d”、&itemToAdd); *queue=realloc(*队列,++*amountO

*队列,int*head,int*tail,int*amountomes){ int itemToAdd; printf(“输入要添加的项目值”); scanf(“%d”、&itemToAdd); *queue=realloc(*队列,++*amountO,c,pointers,queue,malloc,C,Pointers,Queue,Malloc,*队列,int*head,int*tail,int*amountomes){ int itemToAdd; printf(“输入要添加的项目值”); scanf(“%d”、&itemToAdd); *queue=realloc(*队列,++*amountOfTimes*sizeof(int)); 如果(*队列==NULL){ printf(“错误:内存不足\n”); 返回; } (*队列)[(*尾部)+]=itemToAdd; } void removitemfromqueue(int**que

*队列,int*head,int*tail,int*amountomes){ int itemToAdd; printf(“输入要添加的项目值”); scanf(“%d”、&itemToAdd); *queue=realloc(*队列,++*amountOfTimes*sizeof(int)); 如果(*队列==NULL){ printf(“错误:内存不足\n”); 返回; } (*队列)[(*尾部)+]=itemToAdd; } void removitemfromqueue(int**queue,int*head,int*tail){ //如果重组队列,需要int*amountOfTimes 如果(*头部==*尾部){ printf(“错误:队列为空!\n”); 返回; } ++*头部; } 无效打印队列(int*队列,int头,int尾){ int i; printf(“队列项目为:”); 对于(i=头部;i<尾部;++i){ printf(“%d”,队列[i]); } 认沽权(“”); } 内部主(空){ 任务九(); }
您是否使用调试器逐行检查代码?这可能会让你很容易地确定问题的性质和位置。如果您以前没有使用过调试器,那么您应该知道,对于任何程序员来说,使用调试器都是必需的知识。有关更多信息,请参阅。
int**queue=(int**)malloc(1)-->
int*队列=NULL首先,我更改了粘贴箱并为每个功能添加了技术规范。第二,我改变了它仍然不起作用。请发布代码-而不是代码的链接:-)你说“不起作用”是什么意思?非常感谢,你的解决方案简单多了,我现在意识到使用一个指针比使用两个指针简单多了,谢谢。
#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));
    }
}
#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();
}