制作队列程序 [英] Making a queue program
问题描述
谁能帮我做一个队列程序.我想将 array[0]
设置为 array[1]
只是在显示中,但实际上我在 array[0]
添加值>.我知道如何向它运行添加功能,但我无法执行将从 ex 中查看的查看和删除命令.数组[0] 到数组[4],当显示数组[1] 到数组[5] 并插入值时.
Can someone help me making a queue program. I want to set the array[0]
to be array[1]
just in display but in real I am adding value at array[0]
. I got how to run the add function to it, but I can't do the view and delete command that will view from ex. array[0] to array[4], when displayed array[1] to array[5] with the value inserted.
#include <stdio.h>
#include <stdlib.h>
#define p printf
#define s scanf
int rear = 0;
int front = 0;
int *q_array = NULL;
int size = 0;
main()
{
int num, opt;
char cont[] = { 'y' };
clrscr();
p("Queue Program\n\n");
p("Queue size: ");
s("%d", &size);
p("\n");
if(size > 0)
{
q_array = malloc(size * sizeof(int));
if(q_array == NULL)
{
p("ERROR: malloc() failed\n");
exit(2);
}
}
else
{
p("ERROR: size should be positive integer\n");
exit(1);
}
while((cont[0] == 'y') || (cont[0] == 'Y'))
{
clrscr();
p("Queue Program");
p("\n\nQueue size: %d\n\n", size);
p("MAIN MENU\n1. Add\n2. Delete\n3. View");
p("\n\nYour choice: ");
s("%d", &opt);
p("\n");
switch(opt) {
case 1:
if(rear==size)
{
p("You can't add more data");
}
else
{
p("Enter data for Queue[%d]: ", rear+1);
s("%d", &num);
add(num);
}
break;
case 2:
delt();
break;
case 3:
view();
break;
}
p("\n\nDo you want to continue? (Y\/N)");
s("%s", &cont[0]);
}
}
add(int a)
{
q_array[rear]=a;
rear++;
}
delt()
{
if(front==rear)
{
p("Queue Empty");
}
else
{
p("Queue[%d] = %d removed.", front, q_array[front]);
front++;
}
}
view()
{
int i;
for(i=front;i<=rear;i++)
p("\nQueue[%d] = %d", i, q_array[i]);
}
推荐答案
这里的一个严重问题是
One serious problem here is
char cont[] = { 'y' };
...
s("%s", &cont[0]);
您只保留了一个字节,但 scanf
将写入至少 2 个字节,这意味着您将有缓冲区溢出,然后整体行为是不可预测的.如果您想读取单个字符,则使用 "%c"
作为模式,但这里的问题是字符将在缓冲区中供下次读取时使用,因此您必须清除缓冲.
You've only reserved one byte but scanf
will write at least 2 bytes, meaning that you are going to have a buffer overflow and then the overall behaviour is unpredictable. If you want to read a single character then use"%c"
as a pattern but the problem here is that characters will be in the buffer for the next read, so you are going to have to clear the buffer.
这样做要容易得多:
char line[1024];
fgets(line, sizeof line, stdin);
if(line[strlen(line)-1] == '\n')
line[strlen(line)-1] = 0;
if(strcmp(line, "Y") == 0 || strcmp(line, "y")==0)
代码有点多,但这样更安全.
It's a little more code but it's safer this way.
有很多队列,有fifo,lifo,你根据它选择如何构建
There are many queues, there are fifo, lifo, and depending on it you choose how to build it
在处理队列时,最好使用push
、pop
和top
等函数名,因为它们被广泛用于其他程序员和队列库.用这些名字代替.
When dealing with queues, it's better to use function names like push
, pop
and top
because they are widely used among other programmers and queue libraries. Use
these names instead.
在你的情况下,如果用 front
和 rear
记住你应该使用memmove
代替并使用变量 len
来计算当前的数量节点中的元素.一旦你弹出一个元素,你就获得了新的空间更多元素.
In your case, instead if remembering with front
and rear
you should use
memmove
instead and use a variable len
to count the current number of
elements in the node. Once you've popped one element, you gained new space for
more elements.
另外,尝试使用更少的全局变量和更多的封装:(在我的例子中,我是不会关心 malloc
返回 NULL
,我想保持简短)
Also, try to use fewer global variables and more encapsulation: (in my example I am
not going to care about malloc
returning NULL
, I want to keep it short)
#include <string.h> /* for size_t */
typefed struct {
size_z len;
size_z max_size;
int *data;
} queue;
void queue_init(queue *q, size_t max_size)
{
q->len = 0;
q->max_size = max_size;
q->data = malloc(max_size * sizeof *(q->data));
/* this is a good trick!
* If you need to change the datatype of 'data',
* you only need to change the definition of it.
* This code is valid for any type */
}
int push(queue *q, int data)
{
if(q->len == q->max_size)
return 0; /* not enough space */
q->data[q->len++] = data;
return 1;
}
int top(queue *q, int *data)
{
if(q->len == 0)
return 0; /* no elements in the queue */
*data = q->data[0];
return 1;
}
int pop(queue *q, int *data)
{
if(top(q, data) == 0)
return 0;
memmove(q->data, q->data + sizeof *(q->data), q->len--);
return 1;
}
顺便说一句:
#define p printf
#define s scanf
正如 Daniel Fischer 所说,这很丑陋;不要那样做.
Just like Daniel Fischer said, this is ugly; don't do that.
这篇关于制作队列程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!