Nothing Special   »   [go: up one dir, main page]

Stack Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

stack

In data structures, a stack is a linear data structure that follows the Last In, First Out (LIFO)
principle. It means that the last element added to the stack is the first one to be removed. The
operations on a stack typically include:

1. Push: Adding an element to the top of the stack.


2. Pop: Removing the top element from the stack.
3. Peek (or Top): Accessing the top element without removing it.
4. isEmpty: Checking if the stack is empty.
5. isFull: Checking if the stack is full (for fixed-size stacks).

This program defines a `Stack` class that uses an array to implement stack operations. It
includes methods like `push`, `pop`, `peek`, `isEmpty`, and `isFull` to manage elements in the
stack.
Example 1: A C++ program to implement a stack using an array:
#include <iostream>
using namespace std;
#define MAX_SIZE 100 // Maximum size of the stack
class Stack {
private:
int top; // Top of the stack
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top to -1 (empty stack)
}
bool isEmpty() {
return (top == -1); // Check if stack is empty
}
bool isFull() {
return (top == MAX_SIZE - 1); // Check if stack is full
}
void push(int value) {
if (isFull()) {
cout << "Stack Overflow: Cannot push element. Stack is full." << endl;
return;
}
arr[++top] = value; // Increment top and add element
cout << value << " pushed to stack." << endl;
}
void pop() {
if (isEmpty()) {
cout << "Stack Underflow: Cannot pop element. Stack is empty." << endl;
return;
}
cout << arr[top--] << " popped from stack." << endl; // Remove top element
}

int peek() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
return -1; // Return -1 for an empty stack
}
return arr[top]; // Return top element
}
};

int main() {
Stack myStack;

myStack.push(5);
myStack.push(10);
myStack.push(15);
cout << "Top element: " << myStack.peek() << endl;

myStack.pop();
myStack.pop();

cout << "Top element after pops: " << myStack.peek() << endl;

return 0;
}

Compile and run this code in a C++ compiler to see how a stack works using an array-based
implementation.

Example 2: A program that implements a stack using array is given as follows.


#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}

You might also like