I've found this simple Queue code and I'm trying to change some stuff. Since it was in spanish, I translated hoping that you can understand.
#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;
}
Note that the function below is the original and the one above is the one I'm trying to modify to Seek an element in the queue by giving it when I call the function.
//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;
//}
When I run the all the code, the console shows nothing. I don't know what I'm missing here in Seek()
. Any help would be appreciated.