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

DSA Lab10

The document contains code for 4 tasks that demonstrate using queues in C++. Task 1 shows adding elements to a queue. Task 2 shows removing elements from the front and back. Task 3 uses a priority queue. Task 4 removes an element by value from an array.

Uploaded by

raonetflix1999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

DSA Lab10

The document contains code for 4 tasks that demonstrate using queues in C++. Task 1 shows adding elements to a queue. Task 2 shows removing elements from the front and back. Task 3 uses a priority queue. Task 4 removes an element by value from an array.

Uploaded by

raonetflix1999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Name: Rao Humza

Roll no: 02-132212-010

Task 1

#include <iostream>

#include <queue>

void Enqueue(std::queue<int>& q, int num) {

q.push(num);

int main() {

std::queue<int> myQueue;

int num;

std::cout << "Enter elements to add to the queue (enter 0 to stop): " << std::endl;

do {

std::cin >> num;

if (num != 0) {

Enqueue(myQueue, num);

} while (num != 0);


std::cout << "The queue contains:" << std::endl;

while (!myQueue.empty()) {

std::cout << myQueue.front() << std::endl;

myQueue.pop();

return 0;

Task 2
#include <iostream>

#include <queue>

void dequeue(std::queue<int>& q) {

std::cout << "Removing element from front of queue: " << q.front() << std::endl;

q.pop();

std::cout << "Removing element from back of queue: " << q.back() << std::endl;

q.pop();

int main() {

std::queue<int> myQueue;

int num;

std::cout << "Enter elements to add to the queue (enter 0 to stop): " << std::endl;

do {

std::cin >> num;

if (num != 0) {

myQueue.push(num);

} while (num != 0);


dequeue(myQueue);

return 0;

Task 3

#include <iostream>

#include <queue>

int main() {

std::priority_queue<int> myQueue;

int num;
std::cout << "Enter elements to add to the queue (enter 0 to stop): " << std::endl;

do {

std::cin >> num;

if (num != 0) {

myQueue.push(num);

} while (num != 0);

std::cout << "The queue contains:" << std::endl;

while (!myQueue.empty()) {

std::cout << myQueue.top() << std::endl;

myQueue.pop();

return 0;

}
Task 4

#include <iostream>

using namespace std;

void removeByPriority(int value, int array[], int& length)

int j = 0;

for (int i = 0; i < length; i++)

if (array[i] != value)
{

array[j] = array[i];

j++;

length = j;

void print(int array[], int length)

for (int i = 0; i < length; i++)

cout << array[i] << ", ";

cout << endl;

int main()

int array[] = {1,2,3,4,5,6,7};

int length = 7;

cout << "Original Array: ";

print(array, length);

cout << endl;


cout << "Enter value to remove\n";

int valueToRemove;

cin >> valueToRemove;

removeByPriority(valueToRemove, array, length);

cout << "Array after removing " << valueToRemove << ": ";

print(array, length);

return 0;

You might also like