p5.js | Pop Operation in Stack

The stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
Pop Operation on Stacks: Accessing the content while removing it from the top of the stack, is known as a Pop Operation. In an array implementation of pop() operation, the data element is not actually removed, instead top is decremented to a lower position in the stack to point to the next value. But in linked-list implementation, pop() actually removes data element and deallocates memory space.
Approach: A pop() operation may involve the following steps:
- Check if the stack is empty or not.
- If the stack is empty, produces an error and exit.
- If the stack is not empty, accesses the data element at which top is pointing.
- Delete the element, using array.pop() operation on buffer.
- Return success.
Example 1: This example describes only push operation on stack.
| <!DOCTYPE html> <html>  Â<head>     <title>Stack Data Structure</title>      Â    <meta charset="UTF-8">  Â    <script src=     type="text/javascript"></script>  Â    <style>         body {             padding: 0;             margin: 0;         }          canvas {             vertical-align: top;         }      </style> </head>  Â<body>     <script>      Â        // Define Stack function          functionStack(array) {             this.array = [];             if(array) this.array = array;         }          Â        // Add Get Buffer property to object         // constructor which slices the array         Stack.prototype.getBuffer = function() {             returnthis.array.slice();         }          Â        // Add isEmpty properties to object         // constructor which returns the         // length of the array         Stack.prototype.isEmpty = function() {             returnthis.array.length == 0;         }          Â        // Instance of the stack class         varstack1 = newStack(); //Stack { array: [] }          Â        // Add Push property to object constructor         // which push elements to the array         Stack.prototype.push = function(value) {             this.array.push(value);         }          Â        functionsetup() {          Â            // Create Canvas of size display width * 300             createCanvas(displayWidth, 300);         }          Â        functiondraw() {              Â            // Set background color             background("grey");              Â            // Set stroke weight             strokeWeight(3);              Â            // Set stroke color             stroke('black');             line(10, 45, 90, 45);             rect(10, 30, 40, 30);             noStroke();             text("TOP", 20, 50);              Â            // Display stack              for(vari = stack1['array'].length-1; i >= 0; i--) {                 varp = 10;                 translate(70, 0);                 strokeWeight(3);                 stroke('black');                 line(10+p, 45, p+80, 45);                 noStroke();                 rect(10+p, 30, 40+p, 30);                 text(stack1['array'][i], 40, 50);                 p += 10;             }              Â            textSize(16);             text("Current Top : "+ stack1.peek(), 0, 130);              textSize(13);         }          Â        // Peek Function         Stack.prototype.peek = function() {             returnthis.array[this.array.length-1];         }          Â        // Call to push operation         stack1.push(1);         stack1.push(2);         stack1.push(3);         stack1.push(19);         stack1.push(11);         stack1.push(12);     </script> </body>  Â</html>                           | 
Output:
Example 2: This example uses two Pop operations after pushing the element in the stack by calling stack1.pop() function.
| <!DOCTYPE html> <html>  Â<head>     <title>Stack Data Structure</title>      Â    <meta charset="UTF-8">  Â    <script src=     type="text/javascript"></script>  Â    <style>         body {             padding: 0;             margin: 0;         }          canvas {             vertical-align: top;         }      </style> </head>  Â<body>     <script>      Â        // Define Stack function          functionStack(array) {             this.array = [];             if(array) this.array = array;         }          Â        // Add Get Buffer property to object         // constructor which slices the array         Stack.prototype.getBuffer = function() {             returnthis.array.slice();         }          Â        // Add isEmpty properties to object         // constructor which returns the         // length of the array         Stack.prototype.isEmpty = function() {             returnthis.array.length == 0;         }          Â        // Instance of the stack class         varstack1 = newStack(); //Stack { array: [] }          Â        // Add Push property to object constructor         // which push elements to the array         Stack.prototype.push = function(value) {             this.array.push(value);         }          Â        functionsetup() {          Â            // Create Canvas of size display width * 300             createCanvas(displayWidth, 300);         }          Â        functiondraw() {              Â            // Set background color             background("grey");              Â            // Set stroke weight             strokeWeight(3);              Â            // Set stroke color             stroke('black');             line(10, 45, 90, 45);             rect(10, 30, 40, 30);             noStroke();             text("TOP", 20, 50);              Â            // Display stack              for(vari = stack1['array'].length-1; i >= 0; i--) {                 varp = 10;                 translate(70, 0);                 strokeWeight(3);                 stroke('black');                 line(10+p, 45, p+80, 45);                 noStroke();                 rect(10+p, 30, 40+p, 30);                 text(stack1['array'][i], 40, 50);                 p += 10;             }              Â            textSize(16);             text("Current Top : "+ stack1.peek(), 0, 130);              textSize(13);         }          Â        // Peek Function         Stack.prototype.peek = function() {             returnthis.array[this.array.length-1];         }          Â        // Pop operation         Stack.prototype.pop = function() {             returnthis.array.pop();         }          Â        // Call to push operation         stack1.push(1);         stack1.push(2);         stack1.push(3);         stack1.push(19);         stack1.push(11);         stack1.push(12);          Â        // Call to pop operation         stack1.pop();         stack1.pop();     </script> </body>  Â</html>                          | 
Output:
 
				 
					



