Queue in C

Joined
Aug 9, 2011
Messages
1
Reaction score
0
AIM: Just wanted to write a queue in C, to know C better.
PROBLEM: no problems in code
WHAT I WANT: want comments from CLC for better C coding, program design etc.



Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct node
{
  char c;
  struct node* next;
};


struct queue
{
  struct node* head;
  struct node* tail;
};


/* for every function 0 is returned for success and negative integer for
 * failure. -11 is reserved for args check and -12 for malloc() failure */
int enqueue(struct queue* q, char cr);
void dequeue(struct queue* q, struct node* np);
void create_queue(struct queue** q);
void print_queue(struct queue* q);
void print_node(struct node p);


/* These operatiors was created just for the sake of understanding
 * pointers.
 */
int search_queue(struct queue* q, const char cr);
int delete_from_queue(struct queue* q, const char cr);
int insert_after(struct queue* q, const char cr, const char elem);
int insert_before(struct queue* q, const char cr, const char elem);



int main(void)
{
  int i, r;
  struct queue* q;
  struct node p;

  create_queue(&q);
  for(i = 97; i <= 101; ++i)
  {
    if((r = enqueue(q, i))) /* implicit conversion to charcter intended */
    {
      printf("Could not enqueue, r = %d\n", r);
    }
  }

  print_queue(q);
  dequeue(q, &p);
  print_queue(q);
  print_node(p);
  printf("Element 'n' exist == %d\n\n", search_queue(q, 'n'));
  delete_from_queue(q, 'e');
  print_queue(q);
  delete_from_queue(q, 'b');
  print_queue(q);

  printf("Element 'd' exist == %d\n\n", search_queue(q, 'd'));

  insert_after(q, 'e', 'd');
  insert_after(q, 's', 'r');
  print_queue(q);
  insert_before(q, 'b', 'c');
  insert_before(q, 'a', 'b');
  insert_before(q, 'a', 'B');
  print_queue(q);

  return EXIT_SUCCESS;
}



/* insert "cr" before "elem" */
int insert_before(struct queue* q, const char cr, const char elem)
{
 struct node *prev, *temp;
 if((NULL == q) || (NULL == q->head)) return -11;

 for(prev = NULL, temp = q->head; temp; temp = temp->next)
 {
   if(elem == temp->c)
   {
     struct node* t = malloc(1 * (sizeof *t));
     if(NULL == t) return -12;
     t->c = cr;
     if(NULL == prev) /* we are dealing with head */
     {
      t->next = q->head;
      q->head = t;
     }
     else
     {
      prev->next = t;
      t->next = temp;
     }
     return 0;
   }
   else
   {
    prev = temp;
   }
 }

 return -13;
}


/* insert "cr" next to "elem" */
int insert_after(struct queue* q, const char cr, const char elem)
{
 struct node* temp;
 if((NULL == q) || (NULL == q->head)) return -11;

 for(temp = q->head; temp; temp = temp->next)
 {
   if(elem == temp->c)
   {
     struct node* t = malloc(1 * (sizeof *t));
     if(NULL == t) return -12;
     t->c = cr;
     t->next = temp->next;
     temp->next = t;
     return 0;
   }
 }

 return -13;
}


int search_queue(struct queue* q, const char cr)
{
  struct node* temp;
  if((NULL == q) || (NULL == q->head)) return -11;

  for(temp = q->head; temp; temp = temp->next)
  {
    if((cr == temp->c)) return 0;
  }

  return -13;
}


int delete_from_queue(struct queue* q, const char cr)
{
  struct node *prev, *temp;;
  if((NULL == q) || (NULL == q->head)) return -11;

  for(prev = NULL, temp = q->head; temp; temp = temp->next)
  {
    if(cr == temp->c)
    {
      if(NULL == prev) /* we are dealing with head */
      {
        q->head = temp->next;
        free(temp);
        if(NULL == q->head) q->tail = q->head;
        return 0;
      }
      else
      {
        prev->next = temp->next ;
        free(temp);
        if(NULL == prev->next) q->tail = prev->next;
        return 0;
      }
    }
    else
    {
     prev = temp;
    }
  }

  return -13;
}


int enqueue(struct queue* q, char cr)
{
  struct node* p;
  if((NULL == q)) return -11;

  p = malloc(1 * (sizeof *p));
  if(NULL == p) return -12;
  else          p->c = cr;

  if((NULL == q->head) && (NULL == q->tail))
  {
    q->head = q->tail = p;
  }
  else if((NULL == q->head) || (NULL == q->tail))
  {
    free(p);
    return -13;
  }
  else
  {
    struct node* temp = q->tail;
    temp->next = p;
    q->tail = p;
  }

  return 0;
}


void dequeue(struct queue* q, struct node* np)
{
  struct node* temp;

  if((NULL == q) || (NULL == np)) return;
  if((NULL == q->tail)) return;

  temp = q->head;
  q->head = q->head->next;
  np->c = temp->c;
  free(temp);
  if(NULL == q->head) q->tail = q->head;
}


void print_queue(struct queue* q)
{
  if(q)
  {
    struct node* temp = q->head;
    while(temp)
    {
      printf("Element = %c\n", temp->c);
      temp = temp->next;
    }
    printf("--------------- EoQ -----------\n\n");
  }
}

void print_node(struct node p)
{
  printf("Node = %c\n", p.c);
}

void create_queue(struct queue** q)
{
 if(q && *q)
 {
  *q = malloc(1 * (sizeof **q));
  if(NULL == *q) exit(EXIT_FAILURE);

  (*q)->head = (*q)->tail = NULL;
 }
}
================ OUTPUT ====================
$ gcc -ansi -pedantic -Wall -Wextra LL.c
$ ./a.out
Element = a
Element = b
Element = c
Element = d
Element = e
--------------- EoQ -----------

Element = b
Element = c
Element = d
Element = e
--------------- EoQ -----------

Node = a
Element 'n' exist == -13

Element = b
Element = c
Element = d
--------------- EoQ -----------

Element = c
Element = d
--------------- EoQ -----------

Element 'd' exist == 0

Element = c
Element = d
Element = e
--------------- EoQ -----------

Element = a
Element = b
Element = c
Element = d
Element = e
--------------- EoQ -----------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top