-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99acf18
commit b9a4835
Showing
5 changed files
with
165 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useState } from "react"; | ||
|
||
/** | ||
* Custom hook for creating and managing Stack. | ||
* | ||
* @param {Array} initialValue Initial value of the stack. | ||
* @return {Array} Array containing the stack, push and pop functions respectively. | ||
*/ | ||
const useStack = (initialValue) => { | ||
const [stack, setStack] = useState(initialValue); | ||
|
||
/** | ||
* | ||
* Function to add a value to the top of the stack. | ||
* | ||
* @param {any} value Value to be added to the stack. | ||
* @returns {undefined} This function returns nothing. | ||
*/ | ||
const push = (value) => { | ||
setStack([...stack, value]); | ||
}; | ||
|
||
/** | ||
* | ||
* Function to remove the value from the top of the stack. | ||
* | ||
* @returns {any} The value removed from the stack. | ||
*/ | ||
const pop = () => { | ||
if (stack.length > 0) { | ||
let newStack = [...stack]; | ||
const poppedValue = newStack.pop(); | ||
setStack(newStack); | ||
return poppedValue; | ||
} else return undefined; | ||
}; | ||
|
||
return [stack, push, pop]; | ||
}; | ||
|
||
export default useStack; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters