Thread: Concatenating two static queues?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Concatenating two static queues?

    How would I go about concatenating two static queues together? I'm guessing I take the second queue and append it onto the first, but since they are static queues I can't change the size.... below are the structure and functions I'm using

    Code:
    typedef struct {
       int q[10];
       int i, top, bottom;
    } Queue;
    
    Queue *ConcQueues(Queue *q1,Queue *q2);

  2. #2
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    What would you use this for give an example. structs keep your members in a contiguous sequence of memory so I suppose theoretically it would be possible, but...Why not just nest them?
    Code:
    typedef struct one{
       int bar,harry, tod;
       char foobar[BUFSIZ];
    }FOO;
    
    struct two{
        FOO harry;
        int stuff, somemore;
        char hold[BUFSIZ];
    };
    this way you can access both structs ex
    Code:
    struct two BOTH;
    BOTH.harry.foobar = "this string";
    forgive me if i'm misunderstanding your intention.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Queues are usually implemented using linked lists since it's so much more efficient. Your queue can only ever process 10 items without having to keep shifting all the items in the array. Here's a quick queue I just whipped up for you:
    Code:
    itsme@itsme:~/C$ cat queue.c
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct queue_node_s
    {
      int num;
      struct queue_node_s *next;
    } QUEUE_NODE;
    
    typedef struct
    {
      struct queue_node_s *head;
      struct queue_node_s *tail;
    } QUEUE;
    
    void add_to_queue(QUEUE *q, int num)
    {
      QUEUE_NODE *new;
    
      if(!(new = malloc(sizeof(QUEUE_NODE))))
      {
        puts("Memory allocation error!");
        exit(EXIT_FAILURE);
      }
    
      new->num = num;
      new->next = NULL;
    
      if(q->tail)
      {
        q->tail->next = new;
        q->tail = new;
      }
      else
        q->head = q->tail = new;
    }
    
    void new_queue(QUEUE *q)
    {
      q->head = q->tail = NULL;
    }
    
    void destroy_queue(QUEUE *q)
    {
      QUEUE_NODE *n, *nnext;
    
      for(n = q->head;n;n = nnext)
      {
        nnext = n->next;
        free(n);
      }
    
      q->head = q->tail = NULL;
    }
    
    void print_queue(QUEUE *q, char *msg)
    {
      QUEUE_NODE *n;
    
      printf("%s ", msg);
      for(n = q->head;n;n = n->next)
        printf("%d ", n->num);
      putchar('\n');
    }
    
    void conc_queues(QUEUE *q1, QUEUE *q2)
    {
      if(q1->tail)
        q1->tail->next = q2->head;
      else
        q1->head = q2->head;
    
      q1->tail = q2->tail;
    }
    
    int main(void)
    {
      int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      int i;
      QUEUE q1, q2;
    
      new_queue(&q1);
      new_queue(&q2);
    
      for(i = 0;i < 5;++i)
        add_to_queue(&q1, nums[i]);
      for(;i < 10;++i)
        add_to_queue(&q2, nums[i]);
    
      print_queue(&q1, "Queue 1:");
      print_queue(&q2, "Queue 2:");
    
      conc_queues(&q1, &q2);
      print_queue(&q1, "Concatenated:");
    
      destroy_queue(&q1);
    
      return EXIT_SUCCESS;
    }
    itsme@itsme:~/C$ ./queue
    Queue 1: 1 2 3 4 5
    Queue 2: 6 7 8 9 10
    Concatenated: 1 2 3 4 5 6 7 8 9 10
    itsme@itsme:~/C$
    It can definitely be improved on, but it works.
    Last edited by itsme86; 10-18-2004 at 11:30 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  4. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM