在C中实现队列 [英] implementing a queue in C

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

问题描述

好吧所以我正在尝试在C中实现一个队列,但是整个指针的使用已经让我心烦意乱,严重的是我似乎无法绕过它们,我只是想确保我在正确的轨道上(怀疑不是这样:()



所以,如果有人能够快速检查并指出我正确的方向我不胜感激。



这是我的代码到目前为止:



Ok so I'm trying to implement a queue in C but the whole use of pointers has f****d up my mind, seriously I cant seem to wrap my head around them, I just want to make sure I'm on the right track(which is suspect is not the case :( )

So if anyone could just make a quick check and point me in the right direction I'd be grateful.

here's my code so far:

#include "queue.h"
#include "string.h"

#define QUEUE_MAX_SIZE 10

static Person queue[QUEUE_MAX_SIZE];
static int head = 0, tail = 0, nbr_elem = 0;

typedef struct{
char firstName[20];
char surName[20];
char persNbr[20];
}Person;


void enqueue(Person *person)
{
    strcpy(char *queue[tail], *person.firstName);
    strcpy(char *queue[tail], *person.surName);
    strcpy(char *queue[tail], *person.persNbr);
}

void dequeue(Person *person)
{
    int i;

        for(i=0;i<queue.size;i++){
            if(queue[i]=head)
                 strcpy(person, queue[head]);
                 queue[head]=null;
        }

}

推荐答案

http://cprogramsblog.blogspot.in/2012/11/c-program-to-implement-queue-operations.html [ ^ ]


这不是指针的概念,显然困扰你,而是结构的概念。所以不要担心,指针相对容易理解。



让我们从你的入队函数开始:

It's not the concept of pointers that is apparently bothering you, but the concept of structures. So don't worry, pointers are relatively easy to understand.

Let's start with your enqueue function:
void enqueue(Person *person)
{
    strcpy(char *queue[tail], *person.firstName);
    strcpy(char *queue[tail], *person.surName);
    strcpy(char *queue[tail], *person.persNbr);
}



由于person是指向Person对象的指针,该对象的所有成员都由 person->寻址。 成员名称。您的队列数组包含Person类型的元素。因此 queue [n] 一个Person对象。因此,您通过 queue [n] .membername来引用其成员。因此,函数的第一行应该是:


As person is a pointer to a Person object, all the members of that object are being addressed by person->membername. Your queue array contains elements of type Person. And hence queue[n] is a Person object. Hence, you refer to its members by queue[n].membername. The first line of your function should therefore be:

strcpy (queue[tail].firstname, person->firstName);



而不是多次引用queue [tail],你可以通过说
$ b $来定义指向该Person对象的指针。 b


Instead of referring to queue[tail] several times, you could equally well define a pointer to that Person object by saying

void enqueue(Person *person)
{
    Person* p = &queue[tail];
    strcpy (p->firstname, person->firstName);
    ...



看起来不是更好吗?现在更进一步。为什么要逐个复制Person结构的成员。让C编译器为您做到这一点。由于您的成员都是普通数据(并且没有指向其他对象的指针),您可以在一行中完成整个复制:


Doesn't that look better? Now one step further. Why do you want to copy the members of your Person structure one-by-one. Let the C compiler do that for you. As your members are all plain data (and no pointers to other objects) you could do the whole copying in a single line:

void enqueue (Person *person)
{
    queue[tail] = *person;



最后,你忘记了一件重要的事情:你应该增加尾部索引。所以下次你进入一个人时它会进入你阵列的下一个单元格。我想你已经设想了nbr_elem变量来保存当前队列元素的数量。所以你也应该增加它。



您可能已经注意到,您的出队功能完全偏离轨道。这个想法应该是反向进行复制,这次是以索引头为源的队列单元。我不会提供相应的源代码,因为它对你来说是一个很好的练习。



另外,你应该检查一下队列溢出和下溢两个函数入队和出队。


And finally, you forgot one important thing: You should have incremented the tail index. So the next time you are entering a person it goes into the next cell of your array. And I guess you have envisioned the nbr_elem variable to hold the number of current queue elements. So you should increment that as well.

Your dequeue function is totally off track, as you might have noticed. The idea should be to do the copying in reverse, just this time take the queue cell with index head as the source. I am not going to provide the source code for that as it is a good exercise for you.

Also, you should do some checking for queue over- and underflow in both functions enqueue and dequeue.


这篇关于在C中实现队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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