Queue in C

Discussion in 'C Programming' started by arnuld, May 15, 2014.

  1. arnuld

    arnuld

    Joined:
    Aug 9, 2011
    Messages:
    1
    Likes Received:
    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 (Text):

    #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 -----------
     
    arnuld, May 15, 2014
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Paul L. Du Bois

    Queue.Queue-like class without the busy-wait

    Paul L. Du Bois, Mar 24, 2005, in forum: Python
    Replies:
    29
    Views:
    1,581
    Antoon Pardon
    Apr 4, 2005
  2. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    1,037
    Russell Warren
    Jun 27, 2006
  3. Kceiw
    Replies:
    3
    Views:
    1,490
    Jim Langston
    Mar 14, 2006
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    889
    Jerry Hill
    Apr 25, 2008
  5. redbaron
    Replies:
    3
    Views:
    852
    Paul Rubin
    Oct 21, 2008
  6. Laszlo Nagy

    Slow Queue.queue? (was: slow network)

    Laszlo Nagy, Jan 15, 2009, in forum: Python
    Replies:
    0
    Views:
    443
    Laszlo Nagy
    Jan 15, 2009
  7. Laszlo Nagy
    Replies:
    2
    Views:
    618
    Laszlo Nagy
    Jan 15, 2009
  8. Kris
    Replies:
    0
    Views:
    815
Loading...