Skip to content

Instantly share code, notes, and snippets.

@yukeehan
Created February 2, 2018 16:28
Show Gist options
  • Save yukeehan/5f3fa6d8e85ad11b71f098e9358e4a25 to your computer and use it in GitHub Desktop.
Save yukeehan/5f3fa6d8e85ad11b71f098e9358e4a25 to your computer and use it in GitHub Desktop.
class Queue{
char q[ ];
int putloc, getloc;
Queue(int size){
q = new char[size];
putloc = getloc = 0;
}
void put(char ch) {
if(putloc == q.length) {
System.out.println("queue is full");
return;
}
q[putloc++]=ch;
}
char get() {
if(getloc == putloc) {
System.out.println("queue is enmty");
return (char) 0;
}
return q[getloc++];
}
}
public class Try5_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Queue bigQ = new Queue(100);
Queue SmallQ = new Queue(4);
char ch;
int i;
System.out.println("use bigQ to stroe the alphabet.");
for(i=0;i<26;i++)
bigQ.put((char)('A'+i));
System.out.println("display of bigQ");
for(i=0;i<26;i++) {
ch = bigQ.get();
if(ch!= (char) 0) System.out.print(ch);
}
System.out.println("");
//using small q to generate some errors
for(i=0;i<5;i++) {
System.out.print("Attempting to store "+(char)('Z'-i));
SmallQ.put((char)('Z'-i));
System.out.println();
}
System.out.println();
System.out.print("Contents of smallQ");
for(i=0;i<5;i++) {
ch = SmallQ.get();
if(ch != (char) 0) System.out.print(ch);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment