Thread: Trying to make a Queue

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    2

    Trying to make a Queue

    (Sorry if my english is terrible.)

    Hey guys,

    I'm really new to this world of programming. I have this project that I need to do in school. And I have to do some queues. I already searched a bit on the Internet and most of them use the FIFO method. The thing is that, no one explains how to remove one of the components in the middle of the queue. Not the first one, or the last one. Imagine I have 5 people in queue, and the 3rd guy wants to leave.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That isn't how the queue abstract data type works: you can only enter from one end and exit from the other, hence FIFO. If you're in the middle and want to leave, so sorry, you have to wait for your turn.

    Are you thinking of a priority queue instead? Yet, even in a priority queue, items in the middle still cannot just leave: only those with the highest priority get to leave first.

    If you want to model a "real world queue" instead, then you shouldn't put too much focus on the abstract data types called queue and priority queue. You could for example go with a linked list and just remove a node in the middle, or go with an array and shift elements when an element in the middle is "removed".
    Last edited by laserlight; 12-17-2018 at 08:09 PM.
    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
    Dec 2018
    Posts
    2
    Thanks for quick reply.
    This one question is pretty stupid but still...
    Imagine I make a small struct of a client with his Name, Phone Number and age. I fill all the information of Client1
    If I want to make a function just for the sake of change one of the components of Client1. How do I do that?

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by MrCricket View Post
    Thanks for quick reply.
    This one question is pretty stupid but still...
    Imagine I make a small struct of a client with his Name, Phone Number and age. I fill all the information of Client1
    If I want to make a function just for the sake of change one of the components of Client1. How do I do that?
    Consider this code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    #define NAME_LEN 10
    
    typedef struct
    {
        char name[NAME_LEN];
        uint64_t phone_num;
        unsigned age;
    }Person;
    
    Person change_age(Person p, unsigned new_age)
    {
        p.age=new_age;
        
        return p;
    }
    
    int main(void)
    {
        Person p1={.name="Bob", .phone_num=111111, .age=30};
        p1 = change_age(p1,20);
        
        printf("p1.age==%u\n",p1.age);
    
        return EXIT_SUCCESS;
    }

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. Edit stack code to make queue
    By mikeman in forum C++ Programming
    Replies: 1
    Last Post: 02-15-2010, 09:49 AM
  4. Trying to make a queue!
    By Nakeerb in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2002, 12:51 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