用户输入队列的大小 [英] input size of a queue by the user

查看:175
本文介绍了用户输入队列的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在给定的代码中学习一个队列
的数组实现

#include <stdio.h>

main()
{
  int q[10]={0}, i, front=-1, rear=-1, max=10, n, item;
  printf("\n"
    "\tMENU\n"
    "1.ENQUEUE\n"
    "2.DEQUEUE\n"
    "3.DISPLAY\n"
    "4.EXIT\n"
    );
  do
  {
    printf("\nEnter your choice\n");
    scanf("%d",&n);
    switch(n)
    {
    case 1:
      if(rear<max-1)

    // .............so on

他们没有要求用户输入队列的大小但是已经将它定义为10.只有这种情况,或者我们应该总是定义它,而不是给予用户对队列大小的任何控制?

PS:学习队列的任何好的来源及其在初学者c中的实现

PS:any good source for learning queue and its implementation in c for a beginner

推荐答案

如果你知道队列的大小进步,这是最好的方法。否则,您可以要求用户输入队列的首选大小,并从堆中动态分配内存:

If you know the size of the queue in advance, this is the best method. Otherwise, you can ask the user to input the preferred size of the queue and dynamically allocate memory from the heap:

int queue_size;
printf("\nEnter queue size\n");
scanf("%d",&queue_size);
int *q = malloc(queue_size * sizeof(int));

这篇关于用户输入队列的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆