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

4678-Assignment-3

The document contains a C++ implementation of a linear queue with various operations such as enqueue, dequeue, peek, and checks for empty and full states. It includes a class definition for the queue, methods for managing the queue, and a main function that provides a menu for user interaction. The code is designed to handle basic queue functionalities and displays relevant messages based on user actions.

Uploaded by

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

4678-Assignment-3

The document contains a C++ implementation of a linear queue with various operations such as enqueue, dequeue, peek, and checks for empty and full states. It includes a class definition for the queue, methods for managing the queue, and a main function that provides a menu for user interaction. The code is designed to handle basic queue functionalities and displays relevant messages based on user actions.

Uploaded by

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

SUBMITTED BY: noor FaTIMa

SUBMITTED To: Dr. naDEEM


rEg no. 4678-Foc/BScS/F22

aSSIgnMEnT # 3
LInEar qUEUE IMpLEMEnTaTIon
SoUrcE coDE:
#include<iostream>
using namespace std;

class queue {
int *arr;
int front, rear, maxsize;

public:

queue() {
maxsize = 50;
arr = new int[maxsize];
front = -1;
rear = -1;
}
bool isfull() {
if (rear == maxsize - 1) {
return true;
}
else {
return false;
}
}

bool isempty() {

if ((front == -1 && rear == -1) || front > rear) {


return true;
}
else {
return false;
}
}
void enqueue(int data) {
if (isfull()) {
cout << "queue is full (overflow)." << endl;
return;
}
if (isempty()) {
front = 0;
rear = 0;
}
else {
rear++;
}
arr[rear] = data;
cout << "element " << data << " is inserted successfully." << endl;
}
int dequeue() {
if (isempty()) {
cout << " queue is empty (underflow)." << endl;
return -1;
}
int item = arr[front];
if (front == rear) { // if the queue has only one element
front = -1;
rear = -1;
}
else {
front++;
}
return item;
}

int peek() {
if (isempty()) {
cout << "queue is empty (underflow)." << endl;
return -1;
}
else {
return arr[front];
}
}
int totalelements() {
if (isempty()) {
cout << "queue is empty." << endl;
return -1;
}
return rear - front + 1;
}
void displayqueue() {
if (isempty()) {
cout << "queue is empty." << endl;
return;
}
cout << "queue elements: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};

int main() {
queue q;
int choice, data;

do {
cout << "\nqueue operations:" << endl;
cout << "1. enqueue" << endl;
cout << "2. dequeue" << endl;
cout << "3. peek" << endl;
cout << "4. check if empty" << endl;
cout << "5. check if full" << endl;
cout << "6. total elements" << endl;
cout << "7. display queue" << endl;
cout << "8. exit" << endl;
cout << "enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "enter data to enqueue: ";
cin >> data;
q.enqueue(data);
break;
case 2:
data = q.dequeue();
if (data != -1) {
cout << "dequeued: " << data << endl;
}
break;
case 3:
data = q.peek();
if (data != -1) {
cout << "front element: " << data << endl;
}
break;
case 4:
if (q.isempty()) {
cout << "queue is empty." << endl;
}
else {
cout << "queue is not empty." << endl;
}
break;
case 5:
if (q.isfull()) {
cout << "queue is full." << endl;
}
else {
cout << "queue is not full." << endl;
}
break;
case 6:
data = q.totalelements();
if (data != -1) {
cout << "total elements in queue: " << q.totalelements() << endl;
}
break;
case 7:
q.displayqueue();
break;
case 8:
cout << "exiting program." << endl;
break;
default:
cout << "invalid choice! please enter a valid option." << endl;
}
} while (choice != 8);

return 0;
}

oUTpUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy