Thread: What are steps to create queue with linked list

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

    What are steps to create queue with linked list

    What are the steps to create queue with linked list

    Step 1 : Create a new node
    Step 2 : Assign data to the data member of new node
    step 2 : if queue is empty go to step 3 else go to step 4
    step 3 : Make new node as front
    step 4 : Make temporary node as front until it reaches the last node bump the temporary node
    step 5 : make temporary node link as new node
    step 6 : end

    Code:
     struct QNode {     int key; 
        struct QNode* next; 
    };
    How to make function for enqueueig ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    You just need a linked list for which you keep track of both the head and the tail. To enqueue is to append a new node at the tail; to dequeue is to retrieve and remove the node at the head.
    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
    Jul 2018
    Posts
    80
    Quote Originally Posted by laserlight View Post
    You just need a linked list for which you keep track of both the head and the tail. To enqueue is to append a new node at the tail; to dequeue is to retrieve and remove the node at the head.
    okay
    This is linked list that can add new node
    Code:
    #include<stdio.h>#include<stdlib.h> 
         
    struct node{
      int Number;
      struct node *next;
    };
       
    struct node* newNode(int number, struct node *next) {
        struct node *new = malloc(sizeof(*new));
               new->Number = number;
               new->next = next;
        return new;
    }
       
    void show(struct node *head){
         struct node *c;
         c = head;
         while (c!=NULL){
               printf("%d\n",c->Number);
               c = c->next;
               }
        
         }
     
            
    int main (void ) {
       struct node *head = NULL;  
         
    head = newNode(10, head);
    head = newNode(20, head);
    head = newNode(30, head);
      
    show(head);
    
    
     return 0;
    }
    30
    20
    10

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    That's also a linked list that prepends to the head, which is not what I'm talking about, unless you want to invert it and say to enqueue to prepend to the head, and to dequeue is to retrieve and remove from the tail. It's up to you.

    If you really really don't want to keep track of the tail, go ahead, but then that means that instead of taking constant time, either enqueue or dequeue must take linear time.
    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

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    80
    Quote Originally Posted by laserlight View Post
    That's also a linked list that prepends to the head, which is not what I'm talking about, unless you want to invert it and say to enqueue to prepend to the head, and to dequeue is to retrieve and remove from the tail. It's up to you.

    If you really really don't want to keep track of the tail, go ahead, but then that means that instead of taking constant time, either enqueue or dequeue must take linear time.
    What do you mean ?Can you give pseudo code

    Two pointers called
    FRONT and REAR are used to keep track of the first and last elements in the queue.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by vajra11
    What do you mean ?Can you give pseudo code
    What do I mean concerning what?

    Quote Originally Posted by vajra11
    Two pointers called FRONT and REAR are used to keep track of the first and last elements in the queue.
    That's head and tail by different names, so refer to my post #2.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Queue
    By BretFlorida in forum C Programming
    Replies: 4
    Last Post: 04-16-2013, 10:19 AM
  2. Linked List Queue
    By BretFlorida in forum C Programming
    Replies: 9
    Last Post: 03-28-2013, 07:06 PM
  3. standard linked list or Queue in C??
    By dayalsoap in forum C Programming
    Replies: 1
    Last Post: 06-13-2011, 02:12 PM
  4. Is a queue essentially a linked-list?
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 03-17-2008, 07:54 AM
  5. linked-list queue
    By the_winky_files in forum C Programming
    Replies: 17
    Last Post: 11-21-2005, 03:57 PM

Tags for this Thread