Professional Documents
Culture Documents
#include <iostream>
struct Node {
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() : top(nullptr) {}
bool isEmpty() {
top = newNode;
int pop() {
if (isEmpty()) {
top = top->next;
delete temp;
return value;
int peek() {
if (isEmpty()) {
return top->data;
void display() {
current = current->next;
};
int main() {
Stack myStack;
myStack.push(5);
myStack.push(10);
myStack.push(15);
std::cout << "Top of the stack: " << myStack.peek() << "\n";
myStack.display();
myStack.pop();
std::cout << "Top of the stack after pop: " << myStack.peek() << "\n";
myStack.display();
return 0;
Queue
#include <iostream>
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* front;
Node* rear;
public:
bool isEmpty() {
if (isEmpty()) {
} else {
rear->next = newNode;
rear = newNode;
int dequeue() {
if (isEmpty()) {
front = front->next;
delete temp;
if (front == nullptr) {
rear = nullptr;
return value;
int peek() {
if (isEmpty()) {
return front->data;
void display() {
current = current->next;
};
int main() {
Queue myQueue;
myQueue.enqueue(5);
myQueue.enqueue(10);
myQueue.enqueue(15);
std::cout << "Front of the queue: " << myQueue.peek() << "\n";
myQueue.display();
myQueue.dequeue();
std::cout << "Front of the queue after dequeue: " << myQueue.peek() << "\n";
myQueue.display();
return 0;