Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. class Stack {
  2. constructor () {
  3. this.data = [];
  4. }
  5.  
  6. pop() {
  7. // return top most element in the stack
  8. // and removes it from the stack
  9. // Underflow if stack is empty
  10. if (this.data.length == 0) {
  11. return "Underflow";
  12. } else {
  13. return this.data.pop();
  14. }
  15. }
  16.  
  17. top () {
  18. return this.data.length;
  19. }
  20.  
  21. push (...element) {
  22. for (var i of element) {
  23. return this.data.push(i)
  24. }
  25. }
  26.  
  27. // peek() method looks at the object at the top of this stack
  28. // without removing it from the stack.
  29. // The stack is not modified (it does not remove the element;
  30. // it only returns the element for information purposes).
  31.  
  32. peek () {
  33. return this.data[this.data.length - 1];
  34. }
  35.  
  36. clear () {
  37. return this.data = [];
  38. }
  39.  
  40. length(){
  41. return this.data.length;
  42. }
  43.  
  44. search (value) {
  45. for (let i = 0; i < this.data.length; i++) {
  46. if (this.data[i] === value) {
  47. return value;
  48. } else {
  49. return false;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement