【问题标题】:Elements are not being inserted in different positions in the queue and value is not showing correctly元素未插入队列中的不同位置,并且值未正确显示
【发布时间】:2013-09-13 06:05:59
【问题描述】:

这是我的en_queue 函数,用于在不同条件下插入新元素。

void en_queue(int *queue,int max,int front,int rear)
{
    int ch1;
    printf("\n Enter element to add->");
    scanf("%d",&ch1);
    if(front==0 && rear==(max-1))
    {
        printf("\n Caution!! Queue Overflow!!");
    }
    else if(rear==(max-1) && front>0)
    {
        rear=0;
        queue[rear]=ch1;
        printf("\n %d added to the queue where front fo the queue has room but rear does not" ,ch1);
    }
    else if(front==-1 && rear==-1)
    {
        front=rear=0;
        queue[rear]=ch1;
        printf("\n %d added to the queue where this is the first element to the queue" ,ch1);
    }
    else 
    {
        rear++;
        queue[rear]=ch1;
        printf("\n %d added to the queue where the element added in the rear" ,ch1);
    }
}

这是我的show_queue 函数。

void show_queue(int *newqueue,int front,int rear)
{
    int i=0;
    for(i=front;i<=rear;i++)
    {
        printf("%d",newqueue[i]);
    }
}

通过打印语句,我检查了元素是否总是被插入到第一个位置。所以,我最好的猜测是 rearfront 元素没有成功更新,但我无法找到原因。另外,我至少应该通过show_queue 函数看到正确插入的第一个值;相反,我看到的是垃圾值,但这些值是 constant 即。 4235968 每次。

UPDATE-这里是要求的主要功能。

#include<stdio.h>
#include<stdlib.h>

#define MAX 10

void en_queue(int *,int, int, int);
void show_queue(int *,int,int);

int main()
{
    int queue[MAX],front=-1,rear=-1,ch;
    do{
        printf("\n <<Queue MENU>>");
        printf("\n 1. Add Element");
        printf("\n 2. Delete Element");
        printf("\n 3. Show Queue");
        printf("\n 4. Exit menu");
        printf("\n Enter your choice->");
        scanf("%d", &ch);

        switch(ch)
        {
            case 1: en_queue(queue,MAX,front,rear);
                break;
            /*  
            case 2: del_queue(queue,MAX,front,rear);
                 break;
             */
            case 3: printf("\n The queue is->");
                show_queue(queue,front,rear);
                break;

            case 4:exit(0);

            default: printf("\n Invalid Choice!!!");
                return 0;
        }
    } while(ch!=4);

    return 0;
}

【问题讨论】:

  • 您知道,当您更改 en_queue 函数内的 frontrear 变量时,它只会更改该函数中的本地副本,而不是您在调用功能?
  • @JoachimPileborg,啊!愚蠢的错误。我的错。我应该使用pointers
  • 请在将来格式化您的问题时更加小心。不仅代码很难阅读,而且由于您从原始源中复制的所有制表符字符,也很难编辑。
  • @ChrisHayes,感谢您的编辑。我一定会的。 :)

标签: c queue


【解决方案1】:

您的frontrear按值传递,因此它们的更改不会在此函数之外维护。

基本上发生的情况是您的frontrear 变量是调用函数中存在的变量的副本,例如main。因此,此处对其所做的更改不会反映在调用函数中。

dequeue 也会有这个问题。

您可能希望按照与推荐相反的顺序执行以下操作之一

  1. 使它们成为全局变量。 - 不推荐,风格不好,容易出错,只允许 1 个队列。
  2. 使它们成为指向父函数变量的指针
  3. OOP 样式,将队列变成包含数组maxfrontrearstruct,并将其作为指针传递。

【讨论】:

  • 好吧,现在插入做得很好。但问题仍然存在于显示器。现在只显示最后一个元素。
  • @Mistu4u 你的意思是最后一个元素还是等于最后一个元素的所有元素?
  • 仅显示最后一个元素。例如,假设我插入了 4,5,6;然后只显示 6 个! 4,5 未显示。
  • 还有一个问题,你说“让它们全球化”。我会从中得到什么好处?我想,它不会让我独立于使用指针!或者即使我不使用指针,值也会改变?
  • @Mistu4u 是的,使它们全局化意味着它们可以在程序的各个角落访问,您不需要使用指针,甚至不需要传递它们。然而,这被广泛认为是不好的风格和容易出现错误,并且还会阻止您拥有超过 1 个队列
【解决方案2】:
    else 
    {
        rear++;
        queue[rear]=ch1;
        printf("\n %d added to the queue where the element added in the rear" ,ch1);
    }

将 后方++ 更改为 *后方++ 。它应该可以解决您的问题。

编辑:

我试过你修改的代码,问题好像是*rear++,改成

    *rear++ to (*rear)++

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多