Thread: Linked List ( Return first element in a queue)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    17

    Linked List ( Return first element in a queue)

    "Hello C world",

    I am trying to write a function to return the first element of a link list queue. I am not real sure how to implement this. I have include a copy of the struct for my Node & queue. Thanks in advance for the help.

    -DeeMan

    Code:
    typedef struct event_Node {
        void *data;
        double arri_time;
        double serv_time;
        double depart_time;
        double start_o_serv;
        double wait_time;
        bool arri_flag;
        bool depart_flag;
        bool wait_flag;
        bool being_serv_flag;
        struct _QueueNode *link;
    } event_Node;
    
    typedef struct _Queue {
        int size;
        QueueNode *head;
        QueueNode *tail;
    } Queue;
    
    void *queue_front(Queue *q)
    {
        //return q->size ? 
        q->head->data : NULL;// this is where I am stuck
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Well, you don't seem like you've done anything. You declare a structure but don't ever seem to initialize it.

    So you seem to be going for a head and tail linked list, which is fine. So, you write a function that malloc's the structure out and you make the first element the head and the last one the tail. For one element, these are the same. By head and tail, those would just be pointers to the front and back of the list.

    Also, with the way you wrote it, it seems like the queue structure should have the event_Node structure in it and not the other way around. What are you trying to make here?

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Have you ever seen an implementation of a linked list? Here is the first one I saw and loved. Read the comments and get into the spirit. Then try to code.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Thank you std10093 and MutantJohn,

    I made those changes MutantJohn, but I also need to look through the link that std10093 sent me to. It was very helpful and I learned a lot from that. I am still looking at the code on his site so that I can get a better understanding. std10093 that was brilliant. Thanks again

    -DeeMan

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I had that help when I came across lists for my first time, so I think it would be great if more people could have that chance too! You are welcome
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minimum element value of a linked list
    By RBCC in forum C Programming
    Replies: 31
    Last Post: 01-20-2011, 03:49 PM
  2. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  3. linked-list queue
    By the_winky_files in forum C Programming
    Replies: 17
    Last Post: 11-21-2005, 03:57 PM
  4. finding an element in a linked list from the end.
    By anoopks in forum C Programming
    Replies: 9
    Last Post: 04-08-2004, 09:00 AM
  5. How to add element to the end of a linked list?
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2002, 03:04 PM