Answer to Question #290423 in C for Srop

Question #290423

Design and implement ordinary queue program using fixed size array to hold student data

with two fields – ID and Name ( ID- int and Name- char)


1
Expert's answer
2022-01-25T07:51:33-0500
#include <stdio.h>
#include <cstring>

const int M = 3;
struct Queue {
    
    struct Student
    {
        int ID;
        char name[10];
        Student(int i, const char* name)
        {
            ID = i;
            strcpy(this->name, name);
        }
        Student()
        {
        }
    };
private:
    int top;
    int bottom;
    static int c;
    Student st[M];
public:
    Queue();
    void push(Student value);
    Student pop ();
};
int Queue::c = 0;

Queue::Queue():top(2), bottom(2) {}

void Queue::push(Student value) {
    if(c > M-1){
        printf("Queque is full");
    }
    st[top--] = value;
    ++c;
    if (top==-1) {
        bottom = 2;
    }
}

Queue::Student Queue::pop() {
    if(c == 0) {
        printf("Queue is empty");
    }
    if(bottom==0) {
        top = 2;
        bottom = 2;
        c--;
        return st[0];
    }
    else c--;
    return st[bottom--];
}

int main()
{
    Queue q;
    q.push(Queue::Student(2, "Stepan"));
    q.push(Queue::Student(1, "Yuliia"));
    q.pop();
    
    
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS