chore(CPlusPlus): add stack using array (#349)

pull/352/head^2
Ujjwal 2021-06-13 01:42:58 +05:30 committed by GitHub
parent 0a4015ef93
commit 550b3fb9b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 141 additions and 4 deletions

View File

@ -15,10 +15,9 @@
1. [Bellman Ford Algorithm](Graphs/bellmam-ford.cpp)
2. [kruskal Algorithm](Graphs/kruskal-algorithm.cpp)
## Multiplication
1. [Karatsuba](Multiplication/karatsuba.cpp)
## Linked Lists
1. [All possible insertions](Linked-Lists/all-possible-insertion.cpp)
2. [Singly linked lists](Linked-Lists/singly.cpp)
@ -37,7 +36,8 @@
## Stacks
1. [Balancing Parenthesis](Stacks/balanced-parenthesis.cpp)
2. [Stack using Array](Stacks/stack-using-array.cpp)
## Sorting
1. [Bubble Sort](Sorting/bubble-sort.cpp)
2. [Insertion Sort](Sorting/insertion-sort.cpp)
@ -52,7 +52,7 @@
11. [Shell Sort](Sorting/shell-sort.cpp)
12. [Binary Insertion Sort](Sorting/binary-insertion-sort.cpp)
13. [Merge Sort](Sorting/merge-sort.cpp)
## Strings
1. [Rabin-Karp pattern search algo](Strings/rabin-karp.cpp)
2. [All subsequence of a string (Recursion) ](Strings/sequence.cpp)

View File

@ -0,0 +1,137 @@
/* Program to implement stack using array */
#include<iostream>
using namespace std;
class Stack
{
int cur ,capacity ,*arr;
public:
// constructor which takes capacity as argument
Stack(int capacity)
{
this->capacity = capacity;
cur = -1;
// dynamically allocating memory
arr=new int[capacity];
// if arr contain null
if(arr == NULL)
{
cout << "Memory not located";
exit(0);
}
}
~Stack()
{
delete []arr;
}
void push(int);
void pop();
int top();
int empty();
};
// function to push data into stack
void Stack::push(int data)
{
if(cur == capacity-1)
cout << "Stack is full";
else
{
cur++;
arr[cur] = data;
}
}
// function to pop data from stack
void Stack::pop()
{
cur--;
}
// function to get the top most element of the stack
int Stack::top()
{
return arr[cur];
}
// function to check whether the stack is empty of not
int Stack::empty()
{
if(cur == -1)
return 1;
else
return 0;
}
// function to clear screen
void clear_screen()
{
#ifdef _WIN32
system("cls");
#elif __unix__
system("clear");
#else
cout << "clear screen not supports";
#endif
}
//driver code
int main()
{
int cap;
int choice ,data;
cout << "Enter the size: ";
cin >> cap;
Stack s(cap);
while(1)
{
cout << "1: Push\n2: Pop\n3: Top\n4:Exit\n\nEnter your choice ";
cin >> choice;
switch(choice)
{
// push data into stack
case 1:
cout << "Enter element: ";
cin >> data;
s.push(data);
break;
case 2:
// pop data from stack
if(s.empty())
cout << "Stack is empty";
else
{
data = s.top();
s.pop();
cout << "Popped element: " << data;
}
break;
// get top element from stack
case 3:
if(!s.empty())
{
data = s.top();
cout << "Top element: "<< data;
}
else
cout << "Stack is empty";
break;
case 4:
// exit
exit(0);
default:
// run default case when none one the above case runs
cout << "Invalid choice";
}
// clear the screen
clear_screen();
}
}
/*
Input: Enter the size: 5
Push -1
Push 2
Push 3
Output:
Pop 3
Top 2
Pop 2
Pop -1
Pop Stack is empty
Time complexity: O(1) for all push,pop,top,empty
*/