A
arnuld
WANTED: to delete element from FIFO (Queue)
WHAT I GOT: element is still there with value zero (it garbage value I
guess)
Dequeue deletes last element from this Queue, from the tail. I can't make
out whats wrong here:
/* A Queue (FIFO) implementation using doubly-linked list. All
operations are based on page 70, section 2.4 "Data Structures and
* Algorithms by Aho, Hopcraft and Ullman".
*
* VERSION 0.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { VAL_SUCC = 0, VAL_ERR = 1};
struct dlqueue
{
int num;
struct dlqueue* next;
struct dlqueue* prev;
};
struct dlqlist
{
struct dlqueue* head;
struct dlqueue* tail;
};
int enqueue(struct dlqlist*, int);
int dequeue(struct dlqlist*);
struct dlqueue* front(struct dlqlist*);
void makenull(struct dlqlist*);
int empty(struct dlqlist*);
void print_queue(struct dlqlist* );
int main(void)
{
struct dlqlist* s = malloc(1 * sizeof *s);
if(NULL == s)
{
fprintf(stderr,"IN: %s @%d: Out of Memory\n", __FILE__, __LINE__);
return EXIT_FAILURE;
}
else
{
s->head = s->tail = NULL;
}
enqueue(s, 10);
enqueue(s, 11);
print_queue(s);
dequeue(s);
printf("\n\n----------------------------\n");
print_queue(s);
return EXIT_SUCCESS;
}
int enqueue(struct dlqlist* s, int i)
{
int ret;
if(NULL == s)
{
fprintf(stderr, "IN: %s @ %d: Invalid Args\n", __FILE__, __LINE__);
ret = VAL_ERR;
}
else if(NULL == s->head && NULL == s->tail)
{
struct dlqueue* p = malloc(1 * sizeof *p);
if(NULL == p)
{
fprintf(stderr,"IN: %s @%d: Out of Memory\n", __FILE__,
__LINE__);
ret = VAL_ERR;
}
else
{
p->num = i;
p->prev = p->next = NULL;
s->head = s->tail = p;
ret = VAL_SUCC;
}
}
else if(NULL == s->head || NULL == s->tail)
{
fprintf(stderr, "IN: %s @%d: Serious error.", __FILE__, __LINE__);
fprintf(stderr,"List one of the list's head/tail is null while
other is not\n");
ret = VAL_ERR;
}
else
{
struct dlqueue* p = malloc(1 * sizeof *p);
if(NULL == p)
{
fprintf(stderr,"IN: %s @%d: Out of Memory\n", __FILE__,
__LINE__);
ret = VAL_ERR;
}
else
{
p->num = i;
p->prev = p->next = NULL;
s->tail->next = p;
p->prev = s->tail;
s->tail = p;
ret = VAL_SUCC;
}
}
return ret;
}
int dequeue(struct dlqlist* s)
{
int ret;
if(NULL == s)
{
fprintf(stderr, "IN: %s @ %d: Invalid Args\n", __FILE__, __LINE__);
ret = VAL_ERR;
}
else if(NULL == s->head && NULL == s->tail)
{
printf("Bothing to Dequeue in empty Queue\n");
ret = VAL_SUCC;
}
else if(NULL == s->head || NULL == s->tail)
{
fprintf(stderr, "IN: %s @%d: Serious error.", __FILE__, __LINE__);
fprintf(stderr,"List one of the list's head/tail is null while
other is not\n");
ret = VAL_ERR;
}
else
{
struct dlqueue* p = s->tail;
if(NULL == s->tail->next && NULL == s->head->next) /* if last
element */
{
s->head = s->tail = NULL;
}
else
{
s->tail = s->tail->prev;
}
free(p);
ret = VAL_SUCC;
}
return ret;
}
void print_queue(struct dlqlist* s)
{
if(NULL == s)
{
fprintf(stderr, "IN: %s @ %d: Invalid Args\n", __FILE__, __LINE__);
}
else if(NULL == s->head && NULL == s->tail)
{
printf("Nothing to print\n");
}
else if(NULL == s->head || NULL == s->tail)
{
fprintf(stderr, "IN: %s @%d: Serious error.", __FILE__, __LINE__);
fprintf(stderr,"List one of the list's head/tail is null while
other is not\n");
}
else
{
struct dlqueue* p = s->head;
while(p)
{
printf("num = %d\n", p->num);
p = p->next;
}
}
}
======================= OUTPUT ========================
[[email protected] C]$ gcc -ansi -pedantic -Wall -Wextra fifo-dl.c
[[email protected] C]$ ./a.out
num = 10
num = 11
WHAT I GOT: element is still there with value zero (it garbage value I
guess)
Dequeue deletes last element from this Queue, from the tail. I can't make
out whats wrong here:
/* A Queue (FIFO) implementation using doubly-linked list. All
operations are based on page 70, section 2.4 "Data Structures and
* Algorithms by Aho, Hopcraft and Ullman".
*
* VERSION 0.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { VAL_SUCC = 0, VAL_ERR = 1};
struct dlqueue
{
int num;
struct dlqueue* next;
struct dlqueue* prev;
};
struct dlqlist
{
struct dlqueue* head;
struct dlqueue* tail;
};
int enqueue(struct dlqlist*, int);
int dequeue(struct dlqlist*);
struct dlqueue* front(struct dlqlist*);
void makenull(struct dlqlist*);
int empty(struct dlqlist*);
void print_queue(struct dlqlist* );
int main(void)
{
struct dlqlist* s = malloc(1 * sizeof *s);
if(NULL == s)
{
fprintf(stderr,"IN: %s @%d: Out of Memory\n", __FILE__, __LINE__);
return EXIT_FAILURE;
}
else
{
s->head = s->tail = NULL;
}
enqueue(s, 10);
enqueue(s, 11);
print_queue(s);
dequeue(s);
printf("\n\n----------------------------\n");
print_queue(s);
return EXIT_SUCCESS;
}
int enqueue(struct dlqlist* s, int i)
{
int ret;
if(NULL == s)
{
fprintf(stderr, "IN: %s @ %d: Invalid Args\n", __FILE__, __LINE__);
ret = VAL_ERR;
}
else if(NULL == s->head && NULL == s->tail)
{
struct dlqueue* p = malloc(1 * sizeof *p);
if(NULL == p)
{
fprintf(stderr,"IN: %s @%d: Out of Memory\n", __FILE__,
__LINE__);
ret = VAL_ERR;
}
else
{
p->num = i;
p->prev = p->next = NULL;
s->head = s->tail = p;
ret = VAL_SUCC;
}
}
else if(NULL == s->head || NULL == s->tail)
{
fprintf(stderr, "IN: %s @%d: Serious error.", __FILE__, __LINE__);
fprintf(stderr,"List one of the list's head/tail is null while
other is not\n");
ret = VAL_ERR;
}
else
{
struct dlqueue* p = malloc(1 * sizeof *p);
if(NULL == p)
{
fprintf(stderr,"IN: %s @%d: Out of Memory\n", __FILE__,
__LINE__);
ret = VAL_ERR;
}
else
{
p->num = i;
p->prev = p->next = NULL;
s->tail->next = p;
p->prev = s->tail;
s->tail = p;
ret = VAL_SUCC;
}
}
return ret;
}
int dequeue(struct dlqlist* s)
{
int ret;
if(NULL == s)
{
fprintf(stderr, "IN: %s @ %d: Invalid Args\n", __FILE__, __LINE__);
ret = VAL_ERR;
}
else if(NULL == s->head && NULL == s->tail)
{
printf("Bothing to Dequeue in empty Queue\n");
ret = VAL_SUCC;
}
else if(NULL == s->head || NULL == s->tail)
{
fprintf(stderr, "IN: %s @%d: Serious error.", __FILE__, __LINE__);
fprintf(stderr,"List one of the list's head/tail is null while
other is not\n");
ret = VAL_ERR;
}
else
{
struct dlqueue* p = s->tail;
if(NULL == s->tail->next && NULL == s->head->next) /* if last
element */
{
s->head = s->tail = NULL;
}
else
{
s->tail = s->tail->prev;
}
free(p);
ret = VAL_SUCC;
}
return ret;
}
void print_queue(struct dlqlist* s)
{
if(NULL == s)
{
fprintf(stderr, "IN: %s @ %d: Invalid Args\n", __FILE__, __LINE__);
}
else if(NULL == s->head && NULL == s->tail)
{
printf("Nothing to print\n");
}
else if(NULL == s->head || NULL == s->tail)
{
fprintf(stderr, "IN: %s @%d: Serious error.", __FILE__, __LINE__);
fprintf(stderr,"List one of the list's head/tail is null while
other is not\n");
}
else
{
struct dlqueue* p = s->head;
while(p)
{
printf("num = %d\n", p->num);
p = p->next;
}
}
}
======================= OUTPUT ========================
[[email protected] C]$ gcc -ansi -pedantic -Wall -Wextra fifo-dl.c
[[email protected] C]$ ./a.out
num = 10
num = 11