Tag: 队列

我如何在c中使用enqueue函数对列表进行排序?

我的列表 : list= C,3 –>C,5,—> A,7 –> A,5 –> G,2–> C,11 –>A,4 我的输出: output= C,5 C,11 G,2 A,7 A,4 A,5 C,3 但是有一些错误。 我在答案中编写代码,然后编写charcmp函数并用strcmp替换为charcmp。 它有时会发挥作用。 但通常第一个元素是在错误的地方。我的排序​​代码喜欢答案的代码

制作队列程序

有人可以帮我制作一个队列程序。 我想在显示中将array[0]设置为array[1] ,但实际上我在array[0]处添加值。 我得到了如何运行add函数,但我无法执行将从ex查看的视图和删除命令。 array [0]到array [4],当数组[1]显示到数组[5]并插入值时。 #include #include #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) { […]

使用数组的队列

下面是我使用数组实现的简单队列。 #include #include #define QSIZE 5 //Limit size of queue to just 5 enteries /*Beginning of prototype for queue functions: Insert, retrieve and display*/ void qdisp(); //Display to queue array contents void qinsert(); //Insert a element into rear of queue int qdelete(); //Remove an element from front of queue /*End of prototyping*/ // Variables int […]

用户输入队列的大小

我试图在给定的代码中学习队列的数组实现 #include main() { int q[10]={0}, i, front=-1, rear=-1, max=10, n, item; printf(“\n” “\tMENU\n” “1.ENQUEUE\n” “2.DEQUEUE\n” “3.DISPLAY\n” “4.EXIT\n” ); do { printf(“\nEnter your choice\n”); scanf(“%d”,&n); switch(n) { case 1: if(rear<max-1) // ………….so on 他们没有要求用户输入队列的大小,但已经将其定义为10.仅适用于这种情况,还是我们应该始终定义它而不是让用户对队列的大小有任何控制权? PS:学习队列的任何好来源及其在初学者中的实现

无锁队列实现最终会在压力下产生循环

我有一个用C语言编写的无锁队列,它以链表的forms存在,包含发布到单个线程并在单个线程中处理的多个线程的请求。 经过几个小时的压力后,我最终得到了最后一个请求的下一个指针指向自身,这创建了一个无限循环并锁定了处理线程。 应用程序在Linux和Windows上运行(并失败)。 我在Windows上调试,我的COMPARE_EXCHANGE_PTR映射到InterlockedCompareExchangePointer 。 这是将请求推送到列表头部的代码,并从多个线程调用: void push_request(struct request * volatile * root, struct request * request) { assert(request); do { request->next = *root; } while(COMPARE_EXCHANGE_PTR(root, request, request->next) != request->next); } 这是从列表末尾获取请求的代码,仅由处理它们的单个线程调用: struct request * pop_request(struct request * volatile * root) { struct request * volatile * p; struct request * request; do { p […]

Radix使用队列排序

我想用队列创建一个基数排序实现。 我无法弄清楚我的代码的哪个部分有问题或我应该阅读哪些资源。 我的代码可能完全错误,但这是我的实现没有任何帮助(我尚未采用数据结构和算法课程)。 我创建了一个函数,但它没有用。 在做研究时,我看到了一些代码示例,但对我来说它们似乎更复杂。 首先,我想找到所有整数的最低有效位然后在其下标匹配的队列元素中对它们进行排序, 然后在排序后将所有队列复制到第11个队列元素的末尾。 再次在第11个队列元素中进行排序,直到达到最高位数。 我能找到最不重要的数字。 并根据这个数字排序。 但是,我无法分析其他数字。 例如; 我可以排序1,2,4,5,3,但是当排序2位或更多位数时,它会失败…… 我希望,我很清楚并简要地解释了我的问题。 // My function declaration // Pre: arrInts holds the linked list of integers which are going to be sort. // Post: queue will return result and its 11th element should hold sorted integers in // that queue queue_node_t * radixSort(const queue_node_t […]

如何在c中的链表中实现队列?

我得到了这些结构声明,以实现使用循环链表的队列集合。 typedef struct intnode { int value; struct intnode *next; } intnode_t; typedef struct { intnode_t *rear; // Points to the node at the tail of the // queue’s linked list int size; // The # of nodes in the queue’s linked list } intqueue_t; intnode_t *intnode_construct(int value, intnode_t *next) { intnode_t *p = malloc(sizeof(intnode_t)); […]

C中的非繁忙阻塞队列实现

我试图在C中实现一个队列,导致进程非忙等待,直到队列中有一个元素要消耗。 我尝试过两种不同的尝试来实现这一目标。 我遇到的第一个问题是,如果enqueue / dequeue操作有条件检查边界( if (q-> count == QUEUESIZE)),对sem_wait的调用将立即返回,因为没有其他进程获得锁定。 如果我将条件更改为while (q-> count == QUEUESIZE),我相信消费者进程将“忙等待”,直到生产者进程发布信号量,这不是我实现的目标,并通过测试,我发现消费者进程不会获得锁定并继续。 我认为我很接近,但我似乎无法弄清楚如何解决这些问题。 我考虑过添加条件变量或pthread_mutex,但是在添加额外的复杂性之前想要耗尽信号量选项。 #define QUEUESIZE 48 typedef struct { char q[QUEUESIZE+1][150]; int first; int last; int count; sem_t *lock; } Queue; init_queue(Queue *q, sem_t *l) { q->first = 0; q->last = QUEUESIZE-1; q->count = 0; q->lock = l; } enqueue(Queue *q, […]

pthread同步阻塞队列

我正在寻找使用pthread同步语义在C中建议的线程安全阻塞队列(多生产者/消费者)的实现。