Like Tree1Likes
  • 1 Post By vart

Circular queue base on struct.

This is a discussion on Circular queue base on struct. within the C Programming forums, part of the General Programming Boards category; Hi. Iam stuck with how to make a circular queue that are based on a struct. Have been reading about ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    Post Circular queue base on struct.

    Hi. Iam stuck with how to make a circular queue that are based on a struct. Have been reading about the implementation but cant really understand it fully.

    Here is what i got so far.

    Code:
    #define SIZE 10
    typedef struct
    {
        char reg;
        char brand;
        int modelyear;
        int mileage;
    }car;
    
    typedef struct
    {
        struct *car_queue;
        int start;
        int stop;
    }circular;
    
    car keyinput();
    
    
    int main(int argc, char **argv)
    {
        int i;
        car item[SIZE];
        
        printf("Add %d cars to the que.\n\n",SIZE);
        for(i=0;i<SIZE;i++){
            item[i]=keyinput();
        }
        return 0;
    }
    
    
    car keyinput()
    {
        car item1;
    
    
        puts("Number:");
        scanf("%s",item1.reg);
        fflush(stdin);
        puts("");
        puts("Brand:");
        scanf("%s",item1.brand);
        fflush(stdin);
        puts("");
        puts("Modelyear:");
        scanf("%d",&(item1.modelyear));
        fflush(stdin);
        puts("");
        puts("Milage:");
        scanf("%d",&(item1.milage));
        fflush(stdin);
    
    
        return item1;
    }
    Whould be nice with some help here.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    First step is turn up your compiler warnings.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Salem likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,273
    Is the circular struct supposed to have a pointer to the array? Currently, the pointer in the circular struct has bad syntax. The circular structure should have a member called "size" so the code knows when to "wrap around" from the end of the array to the beginning of the array.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    Have made some changes.

    Quote Originally Posted by camel-man View Post
    First step is turn up your compiler warnings.
    I have been fixing some code that didnt seem so right.
    First i have changed "struct *car_queue" to "car *car_queue[QUE_SIZE]" and after that i added this to the main, "circular que".

    So now i tried to add this for my forloop:
    "que.car_queue[i]=input();" then i got an error so then i thougth that i can have a "->" instead of "."

    Now when i did this i get this error:

    "invalid type argument of '->' (have 'circular')"

    So what does it mean. I get that i does not want me to use "->"
    do it want me to have 'circular' but what does it really mean.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    I posted an answer to that just now. Changed the struct to this:
    Code:
    typedef struct
    {
        char reg[25];
        char brand[25];
        int modelyear;
        int mileage;
    }car;
    
    
    typedef struct
    {
        car *car_queue[QUE_SIZE];
        int start;
        int stop;
        
    }circular;
    added "circular que" to main.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,273
    car *car_queue[QUE_SIZE] is an array of pointers to struct car. I think what you want is a single pointer to struct car, which would be a pointer to the first element of the array (which would be a pointer to the array).

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    Quote Originally Posted by rcgldr View Post
    car *car_queue[QUE_SIZE] is an array of pointers to struct car. I think what you want is a single pointer to struct car, which would be a pointer to the first element of the array (which would be a pointer to the array).
    So what you are saying that is I should have a pointer like this "car *car_queue".

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,273
    Quote Originally Posted by Mogren3000 View Post
    So what you are saying that is I should have a pointer like this "car *car_queue".
    Yes, that would be a pointer to the first member of an array of struct cars.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help in Circular Queue implementation
    By MARCELO in forum C Programming
    Replies: 7
    Last Post: 04-19-2013, 06:47 AM
  2. IPC producer consumer with Circular Queue
    By mpunzirudu in forum C Programming
    Replies: 9
    Last Post: 12-01-2012, 07:37 PM
  3. Queue with a circular array... nitpick !
    By manasij7479 in forum C++ Programming
    Replies: 18
    Last Post: 10-02-2011, 08:16 PM
  4. Circular Queue
    By Giridhar Sanjay in forum C++ Programming
    Replies: 6
    Last Post: 07-02-2011, 02:22 PM
  5. circular queue
    By strotee76 in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2004, 09:55 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21