C 中链表的队列实现

发布于 2024-10-18 06:37:31 字数 1843 浏览 1 评论 0原文

下面是我的队列的实现,它具有从队列入队和出队的功能。由于某种原因,它崩溃了,没有任何线索(崩溃的地方),因为代码是在 android 上运行的。我怀疑我的队列代码。如果你们有任何线索,我的代码有什么问题,请给我一个想法。

感谢您的任何帮助。

这是我的 C 代码:

    int qLast = 0;

typedef struct phoneSt PhoneStructure;

typedef struct{
    PhoneStructure Phone;
    struct phoneQ *next;
}phoneQ;


phoneQ *headElement = NULL;    /* front pointer in queue*/
phoneQ *tailElement = NULL;     /* rear pointer in queue */

void enqueue_Phone(PhoneStructure Frame)
{
    phoneQ *newnode;      /* New node to be inserted */
    newnode=(phoneQ*)av_malloc(sizeof(phoneQ));
    newnode->next=NULL;
    newnode->Phone=Frame;
        qLast++;
    if(headElement==NULL && tailElement==NULL)
    {
        headElement=newnode;
        tailElement=newnode;
    }
    else
    {
        tailElement->next=newnode;
        tailElement=newnode;
                                                                                                                   }
        __android_log_print(ANDROID_LOG_DEBUG, "myphone.c", "Queue is having %d element", qLast);
}

PhoneStructure dequeue_Phone()
{
    phoneQ *delnode;      /* Node to be deleted */
    PhoneStructure Frame;
        __android_log_write(ANDROID_LOG_DEBUG, "myplayer.c", "In dequeue_Phone");
    if(headElement==NULL && tailElement==NULL){
        __android_log_write(ANDROID_LOG_ERROR, "myphone.c", "Queue is empty to delete any element");
        }
    else
    {
        __android_log_write(ANDROID_LOG_DEBUG, "myphone.c", "In dequeue  queue is not empty");
        delnode=headElement;
        headElement=headElement->next;
        Frame = delnode->Phone;
        av_free(delnode);
        qLast--;
    }
        __android_log_print(ANDROID_LOG_DEBUG, "myphone.c", "In dequeue_Phone returning  remaining  %d",qLast);
        return Frame;
}

Below is the implementation of my queue, which has functionality of enqueueing and dequeing from the Queue. Because of some reason it is crashing with no clues(where it is crashing), as the code is running on android. I suspect with my queue code. If you guys have any clue, what's wrong with my code, please give me an idea.

Thanks for any help.

Here is my C Code :

    int qLast = 0;

typedef struct phoneSt PhoneStructure;

typedef struct{
    PhoneStructure Phone;
    struct phoneQ *next;
}phoneQ;


phoneQ *headElement = NULL;    /* front pointer in queue*/
phoneQ *tailElement = NULL;     /* rear pointer in queue */

void enqueue_Phone(PhoneStructure Frame)
{
    phoneQ *newnode;      /* New node to be inserted */
    newnode=(phoneQ*)av_malloc(sizeof(phoneQ));
    newnode->next=NULL;
    newnode->Phone=Frame;
        qLast++;
    if(headElement==NULL && tailElement==NULL)
    {
        headElement=newnode;
        tailElement=newnode;
    }
    else
    {
        tailElement->next=newnode;
        tailElement=newnode;
                                                                                                                   }
        __android_log_print(ANDROID_LOG_DEBUG, "myphone.c", "Queue is having %d element", qLast);
}

PhoneStructure dequeue_Phone()
{
    phoneQ *delnode;      /* Node to be deleted */
    PhoneStructure Frame;
        __android_log_write(ANDROID_LOG_DEBUG, "myplayer.c", "In dequeue_Phone");
    if(headElement==NULL && tailElement==NULL){
        __android_log_write(ANDROID_LOG_ERROR, "myphone.c", "Queue is empty to delete any element");
        }
    else
    {
        __android_log_write(ANDROID_LOG_DEBUG, "myphone.c", "In dequeue  queue is not empty");
        delnode=headElement;
        headElement=headElement->next;
        Frame = delnode->Phone;
        av_free(delnode);
        qLast--;
    }
        __android_log_print(ANDROID_LOG_DEBUG, "myphone.c", "In dequeue_Phone returning  remaining  %d",qLast);
        return Frame;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-10-25 06:37:31

当您清空队列时,您不会将 tailElement 设置为 NULL。下次入队时,headElement 将保持为 null,并且您将访问已删除的 tailElement,这可能崩溃。如果没有,当您出队时,您将访问 headElement->next,这将会崩溃。

...
headElement=headElement->next;
if (!headElement)
    tailElement=NULL;
Frame = delnode->Phone;
...

When you empty the queue you do not set tailElement to NULL. Next time you enqueue, headElement will remain null, and you will access the deleted tailElement which may crash. If it doesn't, when you dequeue, you access headElement->next, which will crash.

...
headElement=headElement->next;
if (!headElement)
    tailElement=NULL;
Frame = delnode->Phone;
...
念三年u 2024-10-25 06:37:31

要删除元素时,必须检查是否head->next==NULL。如果是这样,则必须将 tail 设置为 NULL,如下所示这必须是链表中的最后一个节点。希望这将使您的程序运行。

When you are deleting the element you must make a check whether head->next==NULL or not.Since if this is true then you must set tail to NULL as this is must be last node in the linked list.Hope this would make your program run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文