c - 跟踪插入队列的每个元素

标签 c timer queue stack

我有一个值列表,它根据条件插入到队列中,然后根据条件再次插入堆栈。我想跟踪插入到队列中的每个变量,并存储每个元素被插入堆栈的时间。
只是意味着我需要保留一个计时器。我怎样才能做到这一点?目前我不知道如何跟踪每个元素。

以下是需要计数的程序函数:

while(!isqueueFull(&belt))
{
    insert(&belt,theorder->meringue);   //i want to keep a timer for each of these values getting inserted
    insert(&belt,theorder->chocalate);
    insert(&belt,theorder->red_velvet); 
    insert(&belt,(theorder->spongecake));       
}

value1=removes(&belt);
push(&baking,value1);

if(!isFull(&baking))
{
    for(;v<=MAX;v++)
    {
        if(counter%4==0)
        {
            value1=theorder->meringue;
            counter=counter+2;
        }
        else
        {   
                if(!isEmpty(&baking))
                  {
                     printf("\n%d",pop(&baking));
                  } 
                else
                {   
                    value2=removes(&belt);   

                    if(value1>=value2)    
                    {
                         while(!isFull(&baking))     
                         {      
                            push(&baking,value2); //if this gets executed i need to store the time where the value got in to stack.
                            value1=value2;
                            counter++;
                            break;           
                         }     
                     }

                    if(value1<value2 && value1!=value2) 
                    {   
                      while(!isqueueEmpty(&belt))
                      { 
                       insert(&belt,value2);    
                           break;        
                      } 
                    }
                }
        }
    }
}   

最佳答案

一些IDE(Visual Studio等)和调试工具(如gdb)支持在调试时跟踪变量,但您可以在程序本身中跟踪它们。

一种方法是编写两个函数来打印队列和堆栈中的所有元素。

每次调用insert()/remove()/push()/pop()时,依次调用相应的。

your previous question中的队列,显示队列中所有元素的函数可能是:

int queue_display(struct Queue *q) {
    if(isEmpty(q)) 
        return 0;
    else
    {
        int p=q->front,count=q->count;
        printf("queue from front to rear:\n");
        do {
            printf("%d ",q->cake[p]);
            count--;
            p = (p+1)%10;
        } while(count>0);
        printf("\n\n");
    }
}

堆栈的相似。

关于c - 跟踪插入队列的每个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16847007/

相关文章:

c - C中的for循环条件

c# - 将分钟转换为小时、分钟和秒

java - 一项全程运行的 Activity

c# - 队列有时会损坏

c# - ConcurrentQueue 持有对象的引用或值? "out of memory"异常

c - g_source_set_callback : invalid callback data pointer

c - 是否允许在 POSIX 中复制未命名的信号量?

c - 评估递归关系

java - Android 来电通知 - 单击时停止计时器

laravel - 如何测试 laravel 连续作业?