C 中链表的队列实现
下面是我的队列的实现,它具有从队列入队和出队的功能。由于某种原因,它崩溃了,没有任何线索(崩溃的地方),因为代码是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您清空队列时,您不会将 tailElement 设置为 NULL。下次入队时,headElement 将保持为 null,并且您将访问已删除的 tailElement,这可能崩溃。如果没有,当您出队时,您将访问 headElement->next,这将会崩溃。
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.
当要删除元素时,必须检查是否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.