0% found this document useful (0 votes)
1 views34 pages

Stack

The document contains multiple C++ implementations of stack and queue data structures using arrays, linked lists, and dynamic memory allocation. It includes operations such as push, pop, peek, and display for both stacks and queues, along with examples of using the standard library's inbuilt stack and queue classes. Additionally, it features practice programs for reversing strings, checking for palindromes, and converting decimal numbers to binary using stacks.

Uploaded by

yogeesh2006r
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views34 pages

Stack

The document contains multiple C++ implementations of stack and queue data structures using arrays, linked lists, and dynamic memory allocation. It includes operations such as push, pop, peek, and display for both stacks and queues, along with examples of using the standard library's inbuilt stack and queue classes. Additionally, it features practice programs for reversing strings, checking for palindromes, and converting decimal numbers to binary using stacks.

Uploaded by

yogeesh2006r
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stack using array

push
#include <iostream>
using namespace std;

class Stack {
int arr[5];
int top;

public:
Stack() {
top = -1;
}

void push(int value) {


top++;
arr[top] = value;
}

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;
}

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]();
}
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]();

cout << [Link]();


}
Check stack condition
#include <iostream>
#include <stack>
using namespace std;

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);

cout << "Size: " << [Link]();


}
Stack using linked list
Push
#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](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;
}

void push(int value) {


Node* newNode = new Node();

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;
}

void push(int value) {


Node* newNode = new Node();

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;
}

cout << "Deleted Element: " << arr[top] << endl;


top--;
}
// Peek operation
void peek() {
if (top == -1) {
cout << "Stack is Empty" << endl;
return;
}

cout << "Top Element: " << arr[top] << endl;


}

// Display operation
void display() {
if (top == -1) {
cout << "Stack is Empty" << endl;
return;
}

cout << "Stack Elements:" << endl;

for (int i = top; i >= 0; i--) {


cout << arr[i] << endl;
}
}
// Destructor
~DynamicStack() {
delete[] arr;
}
};

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;
}

void enqueue(int value) {


if (rear == 4) {
cout << "Queue Overflow" << endl;
return;
}

arr[++rear] = value;
}

void dequeue() {
if (front > rear) {
cout << "Queue Underflow" << endl;
return;
}

cout << "Deleted Element: " << arr[front] << endl;


front++;
}

void peek() {
if (front > rear) {
cout << "Queue is Empty" << endl;
return;
}

cout << "Front Element: " << arr[front] << endl;


}

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;
}

void enqueue(int value) {


if (rear == size - 1) {
cout << "Queue Overflow" << endl;
return;
}

arr[++rear] = value;
}

void dequeue() {
if (front > rear) {
cout << "Queue Underflow" << endl;
return;
}

cout << "Deleted: " << arr[front] << endl;


front++;
}
void peek() {
if (front > rear) {
cout << "Queue is Empty" << endl;
return;
}

cout << "Front Element: " << arr[front] << endl;


}

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]();

cout << "After Dequeue:" << endl;

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;
}

void enqueue(int value) {


Node* newNode = new Node();

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;
}

Node* temp = front;

cout << "Deleted: " << temp->data << endl;

front = front->next;

delete temp;
}

void peek() {
if (front == NULL) {
cout << "Queue is Empty" << endl;
return;
}

cout << "Front Element: " << front->data << endl;


}

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);
}

cout << "Reversed String: ";

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);
}

string rev = "";

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;
}

cout << "Binary: ";

while(![Link]()) {
cout << [Link]();
[Link]();
}
return 0;
}

You might also like