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)
#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;
}
Comments
Leave a comment