Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dynamic stack using array and template #2

Merged
merged 19 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Data Structures/Stacks/dynamic_stack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Dynamic Stack using Array

The idea of a dynamic stack is to allocate additional memory so that the "stack full" scenario does not occur frequently.
By allocating new memory that is bigger than the existing stack memory and copying elements from the old stack to the new stack, a Growable array-based Stack may be created. 

## Templates

The basic concept is to give the data type as a parameter, eliminating the need to write the same code for multiple data types. A software firm, for example, may require sort() for several data type. We can build a single sort() function and give data type as a parameter instead of creating and maintaining several codes.

## Operations

`size():`

```
return nextIndex;
```

`checkEmpty():`

```
return nextIndex==0;
```

`push(x):`

```
if(nextIndex==capacity){
T *newdata=new T[capacity*2];
for(int i=0;i<capacity;i++){
newdata[i]=data[i];
}
capacity*=2;
delete []data;
data=newdata;
}
data[nextIndex]=element;
nextIndex++;
```

`pop():`

```
if(checkEmpty()){
cout<<"stack is empty"<<endl;
return 0;
}
nextIndex--;
return data[nextIndex];
```

`top():`

```
return data[nextIndex-1];
```

## Errors:

- Underflow: An attempt was made to pop an empty stack.

## Applications:

- Expression evaluation.
- Check parenthesis matching in an expression.
- Conversion from one form of expression to another.
- Memory Management.
- Backtracking problems.




Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <iostream>
using namespace std;
template<typename T> //template
class StackUsingArray{
T *data;
int nextIndex;
int capacity;

public:
StackUsingArray(){
data= new T[4];
nextIndex=0;
capacity=4;
}

int size(){
return nextIndex;
}

bool checkEmpty(){
return nextIndex==0;
}

void push(T element){
if(nextIndex==capacity){
T *newdata=new T[capacity*2]; //dynamic_stack
for(int i=0;i<capacity;i++){
newdata[i]=data[i];
}
capacity*=2;
delete []data;
data=newdata;


}

data[nextIndex]=element;
nextIndex++;

}

int pop(){
if(checkEmpty()){
cout<<"stack is empty"<<endl;
return 0;
}
nextIndex--;
return data[nextIndex];
}

int top(){
return data[nextIndex-1];
}

void display(){
for(int i=0;i<nextIndex;i++){
cout<<data[i]<<endl;
}
}
};

int main(){
StackUsingArray<int> s;
int ch;

while(ch!=7)
{
cout<<"\n1. Push\n";
cout<<"2. Pop\n";
cout<<"3. top\n";
cout<<"4. size\n";
cout<<"5. check\n";
cout<<"6. display\n";
cout<<"\nEnter your Choice :: ";
cin>>ch;

switch(ch)
{
case 1:
int x;
cout<<"enter the element"<<endl;
cin>>x;
s.push(x);
break;

case 2:
s.pop();
break;

case 3:
cout<<s.top()<<endl;
break;

case 4:

s.size();

break;

case 5:
s.checkEmpty();
break;

case 6:
s.display();
break;

case 7:
ch=7;
cout<<"\nPress any key .. ";
break;

default:
cout<<"\nWrong Choice!! \n";
break;
}
}


return 0;
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A curated list of [Data Structres](Data%20Structures) and [Algorithms](Algorithm
- [Doubly Linked List](Data%20Structures/Linked%20List/DoublyLinkedList.cpp)
- [Stacks](Data%20Structures/Stacks)
- [Array Implementation](Data%20Structures/Stacks/Stacks_ArrayImplementation.cpp)
- [Dynamic Stack using Array and Template](Data%20Structures/Stacks/dynamic_stack/dynamic_stacks_using_array_and_template.cpp)
- [Linked List Implementation](Data%20Structures/Stacks/Stacks_LLImplementation.cpp)
- [Queues](Data%20Structures/Queues)
- [Array Implementation](Data%20Structures/Queues/Queue_Array-Imp.cpp)
Expand Down