Last active
December 17, 2015 15:29
-
-
Save dtak1114/5632118 to your computer and use it in GitHub Desktop.
2011-Q2
This file contains hidden or 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
public class Stack { | |
int stack[]; | |
int unsigned nelem; | |
public Stack(){ //Constructor | |
nelem = 0; | |
stack = new int[1000]; | |
} | |
public synchronized void push(int d){ | |
stack[nelem] = d; | |
nelem++; | |
notify(); | |
} | |
public synchronized int pop(){ | |
while( nelem == 0 ){ | |
wait(); | |
} | |
/* | |
following was wrong. nelem points where there is no element yet,ready to be pushed. | |
int ret = stack[nelem]; | |
nelem--; | |
*/ | |
nelem--; | |
int ret = stack[nelem]; | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment