0% found this document useful (0 votes)
13 views

Assignment 02

This document describes an assignment submission for a computer science class. It includes code for a circular queue data structure with functions for insertion, removal, and checking if the queue is full or empty. The code is tested by inserting and removing various elements from the queue.

Uploaded by

Israr Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment 02

This document describes an assignment submission for a computer science class. It includes code for a circular queue data structure with functions for insertion, removal, and checking if the queue is full or empty. The code is tested by inserting and removing various elements from the queue.

Uploaded by

Israr Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NATIONAL UNIVERSITY OF TECHNOLOGY

Main I.J.P Road, Sector I-12 Islamabad

Submitted to: Ma’am Saman Riaz


Department: Computer Science
Batch: CS 2022
Submitted by: Israr Qayyum
Student ID: F22605004
ASSIGNMENT-02
QNO1:

CODE:
#include <iostream>

using namespace std;

class CircularQueue {

private:

int front, rear, size, capacity;

int* array;

public:

CircularQueue(int capacity) {

this->capacity = capacity;

size = 0;

front = rear = -1;

array = new int[capacity];

bool isFull() {

return (front == 0 && rear == capacity - 1) || (rear == (front - 1) % (capacity - 1));

bool isEmpty() {

return front == -1;


}

void insert(int value) {

if (isFull()) {

cout << "Queue is full. Cannot insert " << value << endl;

return;

if (isEmpty()) {

front = rear = 0;

} else {

rear = (rear + 1) % capacity;

array[rear] = value;

size++;

cout << "Inserted " << value << " into the queue." << endl;

void remove() {

if (isEmpty()) {

cout << "Queue is empty. Cannot remove." << endl;

return;

int removedValue = array[front];

if (front == rear) {

front = rear = -1;

} else {
front = (front + 1) % capacity;

size--;

cout << "Removed " << removedValue << " from the queue." << endl;

};

int main() {

CircularQueue s(5);

s.insert(1);

s.insert(2);

s.remove();

s.insert(3);

s.insert(5);

s.insert(7);

s.remove();

s.remove();

s.insert(10);

s.insert(20);

return 0;

OUTPUT SCREENSHOT:
Q.NO2
Initial state:

front = -1, rear = -1

array = [ _, _, _, _, _ ]

s.insert(1):

front = 0, rear = 0

array = [ 1, _, _, _, _ ]

s.insert(2):

front = 0, rear = 1

array = [ 1, 2, _, _, _ ]

s.remove():

front = 1, rear = 1

array = [ _, 2, _, _, _ ]

s.insert(3):

front = 1, rear = 2

array = [ _, 2, 3, _, _ ]

s.insert(5):

front = 1, rear = 3

array = [ _, 2, 3, 5, _ ]
s.insert(7):

front = 1, rear = 4

array = [ _, 2, 3, 5, 7 ]

s.remove():

front = 2, rear = 4

array = [ _, _, 3, 5, 7 ]

s.remove():

front = 3, rear = 4

array = [ _, _, _, 5, 7 ]

s.insert(10):

front = 3, rear = 0

array = [ 10, _, _, 5, 7 ]

s.insert(20):

front = 3, rear = 1

array = [ 10, 20, _, 5, 7 ]

You might also like