Thread: help in Circular Queue implementation

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    12

    Unhappy help in Circular Queue implementation

    hello there this program i wrote it to do circular queue
    but i have many error can you help me ?
    Code:
    #include<stdio.h>
    #include<conio.h>
    #define max 9
    struct cQueue
    {
    int front,rear;
    int items[max];
    } ;
    int isempty(struct cQueue *q)
    {
    if(q->front==q->rear)
    return 1;
    else
    return 0;
    }
    void create(struct cQueue *q)
    {
    q->front=q->rear=0;
    }
    void display(struct cQueue *q)
    {
    int i;
    if(isempty(*q))
    printf("\n circular Queue is underflow ");
    else
    {
    if(q->front==0)
    q->front++;
    for(int i=q->front;i!=q->rear;i=(i+1)%max)
    {
    printf("%d ",q->items[i]);
    }
    }
    int isfull(struct cQueue *q)
    {
    if(((q->rear++)%max)==q->front)
    return 1;
    else
    return 0;
    }
    void insert(struct cQueue *q,int d)
    {
    if(isfull(*q)==1)
    printf("Circular Queue Overflow\n");
    else
    {
    q->items[q->rear]=d;
    q->rear=(q->rear++)%max;
    printf("data inserted\n");
    }
    int remove(struct cQueue *q)
    {
    int del;
    if(isempty(*q)==1)
    printf("Circular Queue Underflow\n");
    else
    {
    del=q->items[q->front];
    q->front=(q->front++)%max;
    }
    return del;
    }
    void main()
    {
    struct cQueue q;
    int x;
    clrscr();
    do{
    printf("1-Create New Circular Queue\n2-add New element to Circular Queue\n");
    printf("3-Remove element from Circular Queue\n4-Display all element of Circular Queue\n5-Exit");
    printf("Enter Your Chocie:");
    scanf("%d",&x);
    switch(x)
    {
    case 1:
    create(&q,x);
    break;
    case 2:
    insert(&q,x);
    braek;
    case 3:
    m=remove(&q);
    printf("%d",m);
    break;
    case 4:
    diplay(q);
    break;
    case 5:printf("bye ^_^");
    getch();
    }while(x!=5)
    }
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indent your code properly and tell us how does it not work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    can you copy the into your compiler and try it ??

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What warnings and errors are you getting?

    Have you reviewed this code? Doesn't line 80, for instance, look funny to you?

    This bears an awfully close resemblance to some code I just found on the internet.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, I do not intend to mess up my compiler's source code with your source code

    What's stopping you from describing what is wrong?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    help in Circular Queue implementation-untitled-png

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    OK for the first one, line 24 should be

    Code:
        if(isempty(q))
    


    Since q is already a pointer. You might consider "hungarian" style notation where you prefix pointer names with a p, in this case instead of "struct cQueue *q", you would use "struct cQueue *pq".

    Also you can typedef to avoid having to keep using "struct" everywhere in your code. The microsoft convention for C is to prefix struct names with _ and to use uppercase for defines, typedefs, ...


    Code:
    typedef struct _CQUEUE
    { 
    int front,rear; 
    int items[max]; 
    }CQUEUE, *PCQUEUE;
    
    /* ... */
    
    int isempty(PCQUEUE pq) /* pq is ptr to CQUEUE type */
    { 
    /* ... */
    }
    
    

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    505
    Code:
    int isfull(struct cQueue *q)
    Code:
    {
    if(((q->rear++)%max)==q->front)
    return 1;
    else
    return 0;
    }
    


    Here#s one error. The ++ operator has side effects. It has no place in the isfull() function.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wut is wrong in this circular list implementation ?
    By rajarshi in forum C Programming
    Replies: 10
    Last Post: 11-24-2011, 10:17 AM
  2. Queue with a circular array... nitpick !
    By manasij7479 in forum C++ Programming
    Replies: 18
    Last Post: 10-02-2011, 08:16 PM
  3. Circular Queue
    By Giridhar Sanjay in forum C++ Programming
    Replies: 6
    Last Post: 07-02-2011, 02:22 PM
  4. Circular Array Implementation
    By jturner38 in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2010, 11:44 PM
  5. circular queue
    By strotee76 in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2004, 09:55 AM