MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / deleteLast

Method deleteLast

Java/design-circular-deque.java:98–121  ·  view source on GitHub ↗

Deletes an item from the rear of Deque. Return true if the operation is successful.

()

Source from the content-addressed store, hash-verified

96
97 /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
98 public boolean deleteLast() {
99
100 if(this.size == 0) // means queue is empty
101 return false;
102
103 if(this.size == 1) { // if there is only one element, then remove it(mark it as 0) and
104 arr[rear] = 0; // mark rear and front as -1(like it was initially)...
105 rear = -1;
106 front = -1;
107 this.size --; // also decrease the size to 0
108 return true; // and return true to mark this operation as successful ...
109 }
110
111 if(rear == 0) { // if there are more than one element in the array,
112 arr[rear] = 0; // and rear is at 0, then delete the element and reduce rear by 1
113 rear = this.capacity - 1; // so basically rear will fall back to last position...
114 this.size --;
115 }else {
116 arr[rear] = 0; // if rear is not at 0, then decrease rear by 1
117 rear --;
118 this.size --;
119 }
120 return true; // return true to mark the operation as successful
121 }
122
123 /** Get the front item from the deque. */
124 public int getFront() {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected