CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: circular queue

  1. #1
    Join Date
    May 1999
    Posts
    7

    circular queue

    A data representation that sequentially maps 'n' queues into an array First[Memory_Size]. Represent each queue as a circular queue within memory. Write functions to add and delete elements to queue and other functions to handle queue full situation.
    How do I do this using pure 'C' coding .

    I'am in great trouble please help me.

    thanks.




  2. #2
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: circular queue

    use a next available index for Insertion (ixIn) and a next available index for Retreival (ixOut)

    when poking something in to the array check that ixOut is not equal to ixIn => full
    if poking in is OK, insert the element into the array, and increment ixIn, making sure you use mod to wrap aroud to the beginning of the array

    when taking something out of the array chack that the array is not empty
    if it is not empty, take the element out, and increment ixOut, making sure you use mod to wrap aroud to the beginning of the array

    it might need some improvement, but it is the basic design principle

    sally


  3. #3
    Guest

    Re: circular queue

    can u give me the code please in C i'will be greatfull to u


  4. #4
    Guest

    Re: circular queue

    //Following code will help U
    //Look this is for a single queue i.e not array of Queue . From your question found that u want
    //array of queues , for this U will have to have 2-dimentional array i.e datatype queue[m][n]
    //where 'm' is total number of queues & 'n' is the size of queue .
    //now to the functions written by me where U are passing ptr to the queue pass ptr to 2-dimentional array
    //& U will have to pass the queue number on which operations are to be done .
    // so following is the code for single dimentional queue , hope it will help U .

    //Following program is used to create circular queue

    //Queue is of integers
    //Comments :
    //Queue follows First In Last Out model
    //front always point to the one position less than latest element added &
    //rear points to the last element of the queue


    #include <stdio.h>
    #include <conio.h>
    #include <conio.h>
    #include <stdlib.h>


    #define QUEUESIZE 4



    int IsQueueFull(int front,int rear)//should be called while addding
    {//returns 1 if queue is full
    rear++ ;
    rear = rear % QUEUESIZE ;
    if (front == rear)
    return 1 ;
    return 0 ;
    }

    int IsQueueEmpty(int front,int rear)//should be called while deleting
    {//return 1 when queue is empty
    if (front == rear)
    return 1 ;
    return 0 ;
    }

    void add(int *queue,int* rear,int value)
    {
    (*rear)++ ;
    *rear = *rear % QUEUESIZE ;
    queue[*rear] = value ;
    }

    void del(int *queue,int* front,int *value)
    {
    (*front)++ ;
    *front = *front % QUEUESIZE ;
    *value = queue[*front] ;
    }

    void main()
    {
    int queue[QUEUESIZE],choice ;
    int front , rear ,i ,value ;
    front = rear = QUEUESIZE - 1 ;

    while (1)
    {
    clrscr() ;
    printf("Following are the options available for CIRCULAR Queue") ;
    printf("\n1. Add") ;
    printf("\n2. Delete") ;
    printf("\n3. Exit") ;
    printf("\n4. Display Queue") ;
    printf("\n\n\t\tEnter your choice : ") ;
    fflush(stdin) ;
    scanf("%d",&choice) ;
    switch (choice)
    {
    case 1:
    if (!IsQueueFull(front,rear))
    {
    printf("\nEnter the value to be inserted : ") ;
    fflush(stdin) ;
    scanf("%d",&value) ;
    add(queue,&rear,value) ;
    printf("\n %d has been added to the queue",value );
    }
    else
    printf("\nQueue is Full hence value cannot be added") ;
    fflush(stdin) ;
    getch() ;
    break ;

    case 2:
    if (IsQueueEmpty(front,rear))
    {
    printf("\nQueue is empty hence no item is deleted") ;
    }
    else
    {
    del(queue,&front,&value) ;
    printf("\nFollowing value is deleted from the queue : %d",value) ;
    }
    fflush(stdin) ;
    getch() ;
    break ;
    case 3:
    exit(0) ;
    }
    }
    }


    //my email id is [email protected]
    //if need further help U can mail me there .U are welcome
    //Even if U find any mistake in code do inform me as I have not compiled above code , just written

    Yours
    gautam


  5. #5
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: circular queue

    Sorry, I have no C code, I just did the outline from memory
    I used to write real time apps and we used them quite extensively then
    but now I am elsewhere, and have no access to my old programs

    sally


  6. #6
    Join Date
    Apr 1999
    Posts
    383

    Re: circular queue

    This would make a good homework question...


  7. #7
    Join Date
    May 1999
    Posts
    7

    Re: circular queue

    thank u for ur valuable suggestion




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured