Guest User

Untitled

a guest
Apr 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. function Stack() {
  2. this.dataStore = [];
  3. this.top = 0;
  4. this.push = push;
  5. this.pop = pop;
  6. this.peek = peek;
  7. this.clear = clear;
  8. this.length = length;
  9. }
  10.  
  11. function push(element) {
  12. this.dataStore[this.top++] = element;
  13. }
  14.  
  15. function peek() {
  16. return this.dataStore[this.top - 1];
  17. }
  18.  
  19. function pop() {
  20. return this.dataStore[--this.top];
  21. }
  22.  
  23. function clear() {
  24. this.top = 0;
  25. }
  26.  
  27. function length() {
  28. return this.top;
  29. }
  30.  
  31. var s = new Stack();
  32. s.push("Derrick");
  33. s.push("ReeRee");
  34. s.push("Laynee");
  35. console.log("length: " + s.length());
  36. console.log(s.peek());
  37. var popped = s.pop();
  38. console.log("The popped element is: " + popped);
  39. console.log(s.peek());
  40. s.push("Cynthia");
  41. console.log(s.peek());
  42. s.clear();
  43. console.log("length: " + s.length());
  44. console.log(s.peek());
  45. s.push("Clayton");
  46. console.log(s.peek());
Add Comment
Please, Sign In to add comment