Stack
Stack
push
#include <iostream>
using namespace std;
class Stack {
int arr[5];
int top;
public:
Stack() {
top = -1;
}
void display() {
for(int i = top; i >= 0; i--) {
cout << arr[i] << endl;
}
}
};
int main() {
Stack s;
[Link](10);
[Link](20);
[Link](30);
[Link]();
}
Pop
#include <iostream>
using namespace std;
class Stack {
int arr[5];
int top;
public:
Stack() {
top = -1;
}
void push(int value) {
arr[++top] = value;
}
void pop() {
cout << "Removed: " << arr[top] << endl;
top--;
}
};
int main() {
Stack s;
[Link](100);
[Link](200);
[Link]();
}
Peek
#include <iostream>
using namespace std;
class Stack {
int arr[5];
int top;
public:
// Constructor
Stack() {
top = -1;
}
// Push operation
void push(int value) {
if (top == 4) {
cout << "Stack Overflow" << endl;
return;
}
arr[++top] = value;
}
// Peek operation
void peek() {
if (top == -1) {
cout << "Stack is Empty" << endl;
return;
}
cout << "Top Element: " << arr[top] << endl;
}
};
int main() {
Stack s;
[Link](5);
[Link](15);
[Link]();
return 0;
}
Display
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() {
top = NULL;
}
newNode->data = value;
newNode->next = top;
top = newNode;
}
void display() {
Node* temp = top;
while(temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
};
int main() {
Stack s;
[Link](1);
[Link](2);
[Link](3);
[Link]();
}
Inbuilt stack
Push
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> s;
[Link](10);
[Link](20);
cout << [Link]();
}
Pop
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> s;
[Link](1);
[Link](2);
[Link]();
int main() {
stack<int> s;
if([Link]()) {
cout << "Stack is Empty";
}
}
Size of array
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> s;
[Link](5);
[Link](10);
[Link](15);
class Stack {
Node* top;
public:
Stack() {
top = NULL;
}
newNode->data = value;
newNode->next = top;
top = newNode;
}
void display() {
Node* temp = top;
while(temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
};
int main() {
Stack s;
[Link](10);
[Link](20);
[Link]();
}
Pop
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() {
top = NULL;
}
newNode->data = value;
newNode->next = top;
top = newNode;
}
void pop() {
Node* temp = top;
cout << "Deleted: " << temp->data;
top = top->next;
delete temp;
}
};
int main() {
Stack s;
[Link](5);
[Link](15);
[Link]();
}
Peek
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() {
top = NULL;
}
newNode->data = value;
newNode->next = top;
top = newNode;
}
void peek() {
cout << "Top: " << top->data;
}
};
int main() {
Stack s;
[Link](100);
[Link]();
}
Display
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() {
top = NULL;
}
void push(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = top;
top = newNode;
}
void display() {
Node* temp = top;
while(temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
};
int main() {
Stack s;
[Link](1);
[Link](2);
[Link](3);
[Link]();
}
Dynamic class
Dynamic Stack is a stack where memory is allocated dynamically
using new.
Size is given at runtime.
Memory is created dynamically.
Uses class and pointers.
Ex:
#include <iostream>
using namespace std;
class DynamicStack {
int *arr;
int top;
int size;
public:
// Constructor
DynamicStack(int s) {
size = s;
arr = new int[size];
top = -1;
}
// Push operation
void push(int value) {
if (top == size - 1) {
cout << "Stack Overflow" << endl;
return;
}
arr[++top] = value;
}
// Pop operation
void pop() {
if (top == -1) {
cout << "Stack Underflow" << endl;
return;
}
// Display operation
void display() {
if (top == -1) {
cout << "Stack is Empty" << endl;
return;
}
int main() {
DynamicStack s(5);
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]();
[Link]();
return 0;
}
Queue using array
#include <iostream>
using namespace std;
class Queue {
int arr[5];
int front, rear;
public:
Queue() {
front = 0;
rear = -1;
}
arr[++rear] = value;
}
void dequeue() {
if (front > rear) {
cout << "Queue Underflow" << endl;
return;
}
void peek() {
if (front > rear) {
cout << "Queue is Empty" << endl;
return;
}
void display() {
for (int i = front; i <= rear; i++) {
cout << arr[i] << endl;
}
}
};
int main() {
Queue q;
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]();
[Link]();
return 0;
}
Dynamic class
#include <iostream>
using namespace std;
class DynamicQueue {
int *arr;
int front, rear, size;
public:
DynamicQueue(int s) {
size = s;
arr = new int[size];
front = 0;
rear = -1;
}
arr[++rear] = value;
}
void dequeue() {
if (front > rear) {
cout << "Queue Underflow" << endl;
return;
}
void display() {
for (int i = front; i <= rear; i++) {
cout << arr[i] << endl;
}
}
~DynamicQueue() {
delete[] arr;
}
};
int main() {
DynamicQueue q(5);
[Link](100);
[Link](200);
[Link](300);
[Link]();
[Link]();
[Link]();
return 0;
}
Inbuilt
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
[Link](10);
[Link](20);
[Link](30);
cout << "Front Element: " << [Link]() << endl;
[Link]();
while (![Link]()) {
cout << [Link]() << endl;
[Link]();
}
return 0;
}
Linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Queue {
Node *front, *rear;
public:
Queue() {
front = rear = NULL;
}
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
void dequeue() {
if (front == NULL) {
cout << "Queue Underflow" << endl;
return;
}
front = front->next;
delete temp;
}
void peek() {
if (front == NULL) {
cout << "Queue is Empty" << endl;
return;
}
void display() {
Node* temp = front;
while (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
};
int main() {
Queue q;
[Link](5);
[Link](10);
[Link](15);
[Link]();
[Link]();
[Link]();
return 0;
}
Practice program
Reverse
#include <iostream>
#include <stack>
using namespace std;
int main() {
string str = "HELLO";
stack<char> s;
for(char ch : str) {
[Link](ch);
}
while(![Link]()) {
cout << [Link]();
[Link]();
}
return 0;
}
Palindrome
#include <iostream>
#include <stack>
using namespace std;
int main() {
string str = "madam";
stack<char> s;
for(char ch : str) {
[Link](ch);
}
while(![Link]()) {
rev += [Link]();
[Link]();
}
if(str == rev)
cout << "Palindrome";
else
cout << "Not Palindrome";
return 0;
}
Decimal to binary
#include <iostream>
#include <stack>
using namespace std;
int main() {
int n = 10;
stack<int> s;
while(n > 0) {
[Link](n % 2);
n = n / 2;
}
while(![Link]()) {
cout << [Link]();
[Link]();
}
return 0;
}