Linked Queue in Data Structure example in C programming :
#include <stdio.h>
#include <stdlib.h>
struct qnode {
int data;
int prio;
struct qnode *next;
};
void quein(struct qnode **, struct qnode **, int, int);
int quedel(struct qnode **, struct qnode **, int *, int *);
int main(void) {
int tab[10] = { 2, 8, 3, 5, 4, 9, 6, 7, 1, 0 };
struct qnode *first = NULL;
struct qnode *last = NULL;
int val, prio, i;
for (i = 0; i < 10; i++) {
val = tab[i], prio = i;
printf("Inserting: value: %d with priority: %d\n", prio, val);
quein(&first, &last, val, prio);
}
printf("=-=\n");
for (i = 0; i < 11; i++) {
val = tab[i], prio = i;
if (quedel(&first, &last, &val, &prio) != -1)
printf("Deleting: value: %d with priority: %d\n", prio, val);
}
return 0;
}
int quedel(struct qnode **first, struct qnode **last, int *prio, int *val) {
struct qnode *tmp = NULL;
if ((NULL == *last) && (*last == *first)) {
fprintf(stderr, "Empty queue.....\n");
return -1;
}
*val = (*first)->data, *prio = (*first)->prio;
tmp = *first, *first = (*first)->next;
if (*last == tmp)
*last = (*last)->next;
free(tmp);
return 0;
}
void quein(struct qnode **first, struct qnode **last, int prio, int val) {
struct qnode *tmp = NULL;
struct qnode *tmp1 = NULL;
tmp = malloc(sizeof(struct qnode));
tmp->data = val;
tmp->prio = prio;
tmp->next = NULL;
if (*last == NULL) {
*last = tmp;
*first = *last;
} else {
if ((*first)->prio < prio) {
tmp->next = *first;
*first = tmp;
} else {
if ((*last)->prio > prio) {
(*last)->next = tmp;
*last = tmp;
} else {
tmp1 = *first;
while ((tmp1->next)->prio >= prio) {
tmp1 = tmp1->next;
}
tmp->next = tmp1->next;
tmp1->next = tmp;
}
}
}
return;
}
OUTPUT
Empty queue..... Inserting: value: 0 with priority: 2 Inserting: value: 1 with priority: 8 Inserting: value: 2 with priority: 3 Inserting: value: 3 with priority: 5 Inserting: value: 4 with priority: 4 Inserting: value: 5 with priority: 9 Inserting: value: 6 with priority: 6 Inserting: value: 7 with priority: 7 Inserting: value: 8 with priority: 1 Inserting: value: 9 with priority: 0 =-= Deleting: value: 5 with priority: 9 Deleting: value: 1 with priority: 8 Deleting: value: 7 with priority: 7 Deleting: value: 6 with priority: 6 Deleting: value: 3 with priority: 5 Deleting: value: 4 with priority: 4 Deleting: value: 2 with priority: 3 Deleting: value: 0 with priority: 2 Deleting: value: 8 with priority: 1 Deleting: value: 9 with priority: 0
1 Comments
Betway Casino, Las Vegas NV - Mapyro
ReplyDeleteFind your way around 대구광역 출장마사지 the casino, find where everything 광명 출장안마 is located with this hotel 태백 출장안마 and casino map. No matter where you are 김제 출장샵 located, you can rest easy knowing Rating: 2.3 · 6 votes · Price range: $$ 광명 출장샵