Toqeer Ahmed DSA Assignment 2
Toqeer Ahmed DSA Assignment 2
class StackNode {
public:
int value;
StackNode* next;
StackNode(int val) {
value = val;
next = nullptr;
};
class LinkedStack {
StackNode* top;
public:
LinkedStack() {
top = nullptr;
bool isEmpty() {
node->next = top;
top = node;
void pop() {
if (isEmpty()) {
return;
top = top->next;
delete temp;
int peek() {
if (isEmpty()) {
return INT_MIN;
return top->value;
void display() {
if (isEmpty()) {
return;
}
StackNode* temp = top;
temp = temp->next;
};
int main() {
LinkedStack stk;
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
stk.display();
stk.pop();
stk.pop();
stk.display();
return 0;
}
Output:
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* frontPtr;
Node* rearPtr;
public:
if (rearPtr == nullptr) {
} else {
rearPtr->next = newNode;
rearPtr = newNode;
void dequeue() {
if (frontPtr == nullptr) {
return;
frontPtr = frontPtr->next;
if (frontPtr == nullptr) {
rearPtr = nullptr;
delete temp;
}
int front() {
if (frontPtr == nullptr) {
return -1;
return frontPtr->data;
void display() {
if (!temp) {
return;
temp = temp->next;
bool isEmpty() {
~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: