forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1670.Design-Front-Middle-Back-Queue_v1.cpp
- Loading branch information
1 parent
52c17be
commit 3535e44
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
Linked_List/1670.Design-Front-Middle-Back-Queue/1670.Design-Front-Middle-Back-Queue_v1.cpp
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,69 @@ | ||
class FrontMiddleBackQueue { | ||
int arr[1000]; | ||
int n = 0; | ||
public: | ||
FrontMiddleBackQueue() { | ||
} | ||
|
||
void pushFront(int val) | ||
{ | ||
for (int i=n; i>=1; i--) | ||
arr[i] = arr[i-1]; | ||
arr[0] = val; | ||
n++; | ||
} | ||
|
||
void pushMiddle(int val) | ||
{ | ||
int i = n-1; | ||
for (int i=n; i>= max(1,n/2); i--) | ||
arr[i] = arr[i-1]; | ||
arr[n/2] = val; | ||
n++; | ||
} | ||
|
||
void pushBack(int val) | ||
{ | ||
arr[n] = val; | ||
n++; | ||
} | ||
|
||
int popFront() | ||
{ | ||
if (n==0) return -1; | ||
int ret = arr[0]; | ||
for (int i=0; i<n-1; i++) | ||
arr[i] = arr[i+1]; | ||
n--; | ||
return ret; | ||
} | ||
|
||
int popMiddle() | ||
{ | ||
if (n==0) return -1; | ||
int ret = arr[(n-1)/2]; | ||
for (int i=(n-1)/2; i<n; i++) | ||
arr[i] = arr[i+1]; | ||
n--; | ||
return ret; | ||
} | ||
|
||
int popBack() | ||
{ | ||
if (n==0) return -1; | ||
int ret = arr[n-1]; | ||
n--; | ||
return ret; | ||
} | ||
}; | ||
|
||
/** | ||
* Your FrontMiddleBackQueue object will be instantiated and called as such: | ||
* FrontMiddleBackQueue* obj = new FrontMiddleBackQueue(); | ||
* obj->pushFront(val); | ||
* obj->pushMiddle(val); | ||
* obj->pushBack(val); | ||
* int param_4 = obj->popFront(); | ||
* int param_5 = obj->popMiddle(); | ||
* int param_6 = obj->popBack(); | ||
*/ |