Thread: empty queue

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    empty queue

    I want to write code for the function that create empty queue with no element

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    struct Queue {
        struct Node *first;
        struct Node *last;
        
    };
    
    void EmptyQueue(struct Queue *q) {
        q->front = NULL;
        q->last = NULL;
    }
    Is my function a correct ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Looks good.
    next step, I have written code for en queue functions

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    struct Node {
        int data;
        struct Node *next;
    };
      
    struct Queue {
        struct Node *first;
        struct Node *last;
         
    };
     
    void EmptyQueue(struct Queue *q) {
        q->front = NULL;
        q->last = NULL;
    }
    
    void enqueue(struct Queue *q, int data) {
        
     
        if (q->first == NULL) {
            q->first = (struct Node *) malloc(sizeof(struct Node));
            q->first->data = data;
            q->first->next = NULL;
            q->last = q->first;
        } 
       else {
            q->last->next = (struct Node *) malloc(sizeof(struct Node));
            q->last->next->data = data;
            q->last->next->next = NULL;
            q->last = q->last->next;
        }
    }
    Is my function for enqueue correct ?
    Last edited by vajra11; 12-28-2019 at 07:52 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you bother to test it?

    What tests have you done?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Did you bother to test it?

    What tests have you done?
    No I haven't, How do I check weather my function are correct or incorrect. I would need to other function two check queue. I wanted to make three function first empty queue, enqueue and dequeue

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well it might be an idea to implement say
    Code:
    void print_queue(struct Queue *q) {
    }
    Then in your main, say
    Code:
    int main ( ) {
        struct Queue queue;
        EmptyQueue(&queue);
        print_queue(&queue);  // What do you expect here?
        enqueue(&queue,1);
        print_queue(&queue);  // What do you expect here?
    }
    Then you start thinking up more test cases.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program to implement queue operations using linear queue
    By riteshkrjain in forum C Programming
    Replies: 2
    Last Post: 10-03-2017, 10:57 AM
  2. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  3. proble with an empty linked list queue
    By diablo_mabbs in forum C Programming
    Replies: 5
    Last Post: 04-22-2010, 06:36 PM
  4. "marker to indicate empty queue"?
    By blight2c in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2002, 10:43 PM
  5. Queue and Priority Queue
    By Pamela in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2001, 11:09 PM

Tags for this Thread