我找到了这个简单的队列代码,我正在尝试更改一些东西。由于是西类牙语,所以我翻译了希望你能看懂。
#include <stdio.h>
#include <Windows.h>
/* Returns "a - b" in seconds */
double performancecounter_diff(LARGE_INTEGER *a, LARGE_INTEGER *b)
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return (double)(a->QuadPart - b->QuadPart) / (double)freq.QuadPart;
}
typedef struct _nodo {
int value;
struct _nodo *next;
} TypeNodo;
typedef TypeNodo *pNodo;
/* Queues functions */
void Insert(pNodo *first, pNodo *last, int v);
int Seek(pNodo *first, pNodo *last, int v);
int main() {
LARGE_INTEGER t_ini, t_fin;
double secs;
QueryPerformanceCounter(&t_ini);
pNodo first = NULL, last = NULL;
int x = 1;
while (x <= 80)
{
Insert(&first, &last, x);
x++;
}
printf("%d", Seek(&first, &last,18));
printf("%d", Seek(&first, &last, 2));
QueryPerformanceCounter(&t_fin);
secs = performancecounter_diff(&t_fin, &t_ini);
printf("Algoritmo de manejo de brazo por FCFS: \n");
printf("%.16g milisegundos\n", secs * 1000.0);
system("pause");
return 0;
}
void Insert(pNodo *first, pNodo *last, int v) {
pNodo New;
/* Create a new nodo and allocate it */
New = (pNodo)malloc(sizeof(TypeNodo));
New->value = v;
/* This will be the last nodo and will point to NULL */
New->next = NULL;
/* If queue isn't empty, then add the new nodo next to the last one */
if (*last) (*last)->next = New;
/* Now, the last element of the queue is the new one */
*last = New;
/* If first is NULL, the queue is empty and the first will point to the new nodo, too */
if (!*first) *first = New;
}
int Seek(pNodo *first, pNodo *last, int v) {
pNodo nodo, nodo_aux; /* Extra variable to manipulate the nodo */
int a; /* Extra variable for return */
/* Nodo points to the first element of the queue */
nodo = *first;
nodo_aux = nodo;
if (!nodo) return 0; /* If no nodos in the queue, retunrs 0 */
while (*first != NULL)
{
if (nodo->value == v)
{
/* Storing return value */
a = nodo->value;
return a;
}
/* Assign to the first nodo the second one address */
a = *first = nodo->next;
}
/* Free the nodo */
free(nodo);
/* If queue is empty, last must be */
if (!*first) *last = NULL;
return NULL;
}
请注意,下面的函数是原始函数,上面的函数是我试图修改的函数,以便在调用该函数时通过提供它来在队列中查找元素。
//int Seek(pNodo *first, pNodo *last) {
// pNodo nodo; /* variable auxiliar para manipular nodo */
// int a; /* variable auxiliar para retorno */
//
// /* Nodo apunta al primer elemento de la fila */
// nodo = *first;
// if (!nodo) return 0; /* Si no hay nodos en la fila retornamos 0 */
// /* Asignamos al primer nodo la dirección del segundo nodo */
// *first = nodo->next;
// /* Guardamos el value de retorno */
// a = nodo->value;
// /* Borrar el nodo */
// free(nodo);
// /* Si la cola quedó vacía, last debe ser NULL también */
// if (!*first) *last = NULL;
// return a;
//}
当我运行所有代码时,控制台什么也没显示。我不知道我在 Seek()
中遗漏了什么。任何帮助将不胜感激。
最佳答案
它比你简单得多。
int Seek(pNodo first, int v)
{
while (first != NULL)
{
if (first->value == v)
{
return v;
}
first = first->next;
}
/* Didn't find the value */
return 0;
}
更好的选择是返回包含值的节点。如果值不在队列中,则返回 NULL。 (感谢 M Oehm 的建议)
pNode Seek(pNodo first, int v)
{
while (first != NULL)
{
if (first->value == v)
{
return first;
}
first = first->next;
}
/* Didn't find the value */
return NULL;
}
关于c - 在 C 代码中从队列中读取给定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23532833/