Skip to content

Instantly share code, notes, and snippets.

@dtak1114
Last active December 17, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtak1114/5632118 to your computer and use it in GitHub Desktop.
Save dtak1114/5632118 to your computer and use it in GitHub Desktop.
2011-Q2
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