Ali Irtza Haider-231259-Queue and Stack
Ali Irtza Haider-231259-Queue and Stack
DSA LAB
Submitted By: Ali Irtza Haider(231259)
Submitted To: Mr. Abdullah Imran
BSCYS-3-B-F23
Task 1
Code:
#pragma once
#include <iostream>
struct Node {
int data;
Node* next;
class Stack {
private:
Node* top;
int size;
public:
Stack() : top(nullptr), size(0) {}
~Stack() {
while (top) {
Node* temp = top;
top = top->next;
delete temp;
}
}
int pop() {
if (!top) return -1;
int poppedData = top->data;
Node* temp = top;
top = top->next;
delete temp;
size--;
return poppedData;
}
Output:
Task 2:
#pragma once
#include <iostream>
struct Node {
int data;
Node* next;
class Queue {
private:
Node* front;
Node* rear;
int size;
public:
Queue() : front(nullptr), rear(nullptr), size(0) {}
~Queue() {
while (front) {
Node* temp = front;
front = front->next;
delete temp;
}
}
int dequeue() {
if (!front) return -1;
int dequeuedData = front->data;
Node* temp = front;
front = front->next;
if (!front) rear = nullptr;
delete temp;
size--;
return dequeuedData;
}