Queue function

This is a discussion on Queue function within the C Programming forums, part of the General Programming Boards category; Suppose s structure function is defined as: Code: typedef struct node { char *name; char phone[9]; struct node *next; } ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    6

    Lightbulb Queue function

    Suppose s structure function is defined as:
    Code:
    typedef struct node {
                     char *name;
                     char phone[9];
                     struct node *next;
                     } queue;
    I need to write a function void insert_queue( queue **q_front, queue **q_rear, char *str, char str_num[]), such that *q_front point to the 1st node, *q_rear point to the last node. It adds new node with name str and phone str_num at the end queue while updating q_rear and q_front.

    I came out with the below definition but the program don't work. Hope anyone can spot my error. Thanks
    Code:
    void insert_queue( queue **q_front, queue **q_rear, char *str, char str_num[]){
    * q_front= (queue*) malloc(sizeof (struct node));
    * q_front-> name= *str;
    * q_front-> phone= str_num;
    * q_front-> next= NULL;
    
    if (*q_rear !=NULL)
    *q_rear->next=*q_front;
    *q_rear= *q_front;
    return *q_rear;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    * q_front= (queue*) malloc(sizeof (struct node));
    Don't cast malloc - it may hide problems, and nothing good ever comes from it.

    Code:
    *q_rear= *q_front;
    Are you supposed to ALWAYS set this?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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