0% found this document useful (0 votes)
9 views8 pages

Muhammad Umar DSA Assignment 2

Uploaded by

ta9187208
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)
9 views8 pages

Muhammad Umar DSA Assignment 2

Uploaded by

ta9187208
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/ 8

Name : Muhammad Umar Rana

Roll no : FL23813
Sys ID : NUML-F23-20740
Course title : DSA
Submitted to : Sir Qaisar Shehzad

Assignment No 2
Task 1:
Link list implementation using stack .

#include <iostream>
using namespace std;

class StackNode {
public:
int value;
StackNode* next;

StackNode(int val) {
value = val;
next = nullptr;
}
};

class LinkedStack {
StackNode* top;

public:
LinkedStack() {
top = nullptr;
}

bool isEmpty() {
return top == nullptr;
}
void push(int val) {
StackNode* node = new StackNode(val);
node->next = top;
top = node;
}

void pop() {
if (isEmpty()) {
cout << "Stack is empty. Cannot pop.\n";
return;
}
StackNode* temp = top;
top = top->next;
delete temp;
}

int peek() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return INT_MIN;
}
return top->value;
}

void display() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
StackNode* temp = top;
cout << "Stack elements (top to bottom): ";
while (temp != nullptr) {
cout << temp->value << " ";
temp = temp->next;
}
cout << endl;
}
};

int main() {
LinkedStack stk;

stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);

stk.display();
cout << "Top element: " << stk.peek() << endl;

cout << "Removing two elements from the stack...\n";


stk.pop();
stk.pop();

stk.display();
cout << "Current top: " << stk.peek() << endl;

return 0;
}
OUTPUT:

Task 2:
Link list implementation using queue.
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};

class Queue {
private:
Node* frontPtr;
Node* rearPtr;

public:
Queue() : frontPtr(nullptr), rearPtr(nullptr) {}

void enqueue(int val) {


Node* newNode = new Node(val);
if (rearPtr == nullptr) {
frontPtr = rearPtr = newNode;
} else {
rearPtr->next = newNode;
rearPtr = newNode;
}
}

void dequeue() {
if (frontPtr == nullptr) {
cout << "Queue is empty. Cannot dequeue." << endl;
return;
}
Node* temp = frontPtr;
frontPtr = frontPtr->next;
if (frontPtr == nullptr) {
rearPtr = nullptr;
}
delete temp;
}
int front() {
if (frontPtr == nullptr) {
cout << "Queue is empty." << endl;
return -1;
}
return frontPtr->data;
}

void display() {
Node* temp = frontPtr;
if (!temp) {
cout << "Queue is empty." << endl;
return;
}
cout << "Queue: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

bool isEmpty() {
return frontPtr == nullptr;
}

~Queue() {
while (!isEmpty()) {
dequeue();
}
}
};

int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.display();
cout << "Front element is: " << q.front() << endl;
q.dequeue();
q.display();
cout << "Front element is: " << q.front() << endl;
return 0;
}
OUTPUT:

You might also like