-
Notifications
You must be signed in to change notification settings - Fork 18
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
Conversation
redhawk2002
commented
Oct 6, 2021
- added README.md file containing Dynamic Stack content.
- added dynamic_stacks_using_array_and_template.cpp file.
- updated README.md file of Open-DSA.
…and_dynamicStack.cpp to Data Structures/Stacks/dynamic stack/dynamic_stacks_using_array_and_template.cpp
@abxhr can you review this? |
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave a blank line after the ending of each section
|
||
- 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]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- For the titles (eg:
pop()
), try to mention them through backticks, to format them better. Do this for all the function titles.
eg:
`pop()`
is rendered as pop()
- The code snippets are not indented well. Try to indent them from the left margin
- The code snippet for
top()
is not closed.
## Applications: | ||
|
||
- expression evaluation. | ||
- check parenthesis matching in an expression. | ||
- Conversion from one form of expression to another. | ||
- Memory Management. | ||
- backtracking problems. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first character of each sentence is not capitalised.
@abxhr I have resolved the errors mentioned. |
LGTM ✅ |