-
-
Save Mostafayehya/cc612a085fa6ad700bd6616555f488e4 to your computer and use it in GitHub Desktop.
Simple Java implementation of a circular buffer allowing appends and removes.
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
import java.util.Arrays; | |
import java.util.Scanner; | |
/** | |
* Runs commands for circular buffer | |
* @author Hardik Vala | |
*/ | |
class CircBufferRunner { | |
/** Command Designations */ | |
private static final char APPEND = 'A'; | |
private static final char REMOVE = 'R'; | |
private static final char LIST = 'L'; | |
private static final char QUIT = 'Q'; | |
/** | |
* Runs commands from input | |
* @precondition Input must be properly formatted | |
*/ | |
public static void main(String[] args) { | |
try (Scanner input = new Scanner(System.in)) { | |
int n = Integer.parseInt(input.nextLine()); | |
CircBuffer cb = new CircBuffer(n); | |
boolean quit = false; | |
String line; | |
do { | |
line = input.nextLine().trim(); | |
switch((Character.toUpperCase(line.charAt(0)))) { | |
case APPEND: | |
int numToAppend = Integer.parseInt(line.substring(1).trim()); | |
for (int i = 0; i < numToAppend; i++) cb.append(input.nextLine().trim()); | |
break; | |
case REMOVE: | |
int numToRemove = Integer.parseInt(line.substring(1).trim()); | |
for (int i = 0; i < numToRemove; i++) cb.remove(); | |
break; | |
case LIST: | |
System.out.print(cb.list()); | |
break; | |
case QUIT: | |
quit = true; | |
break; | |
default: | |
continue; | |
} | |
} while (!quit); | |
} | |
} | |
} | |
/** | |
* Circular buffer | |
* @author Hardik Vala | |
*/ | |
class CircBuffer { | |
/** Circular buffer */ | |
private String[] buffer; | |
/** n: Size of buffer, head: Index of first element in buffer, tail: Index of next | |
* element to append */ | |
private int n, head, tail; | |
/** <tt>true</tt> if buffer is full to capacity, and <tt>false</tt> otherwise */ | |
boolean isFull; | |
/** | |
* Constructor | |
* @param n Size of buffer | |
* @precondition n > 0 | |
*/ | |
public CircBuffer (int n) { | |
this.buffer = new String[n]; | |
this.n = n; | |
this.head = this.tail = 0; | |
this.isFull = false; | |
} | |
/** | |
* Checks whether buffer is empty | |
* @return <tt>true</tt> if empty, <tt>false</tt> otherwise | |
*/ | |
public boolean isEmpty () { | |
return !this.isFull && (this.head == this.tail); | |
} | |
/** | |
* Appends <tt>String</tt> item to buffer (If buffer is full, oldest entry is | |
* replaced) | |
* @param s <tt>String</tt> item to append | |
*/ | |
public void append (String s) { | |
if (this.isFull) this.head = (this.head + 1) % this.n; | |
this.buffer[this.tail] = s; | |
this.tail = (this.tail + 1) % this.n; | |
if (this.tail == this.head) this.isFull = true; | |
} | |
/** | |
* Removes oldest item from buffer (If buffer is empty, no action is taken) | |
*/ | |
public void remove () { | |
if (!this.isEmpty()) { | |
this.head = (this.head + 1) % this.n; | |
this.isFull = false; | |
} | |
} | |
/** | |
* Returns <tt>String</tt> of buffer items in order of their insertion time | |
* @return <tt>String</tt> of buffer items | |
*/ | |
public String list () { | |
StringBuilder sb = new StringBuilder(); | |
int numElements = (this.isFull) ? this.n : ((this.head > this.tail) ? (this.tail + this.n) - this.head : this.tail - this.head); | |
for (int i = 0; i < numElements; i++) sb.append(this.buffer[(this.head + i) % this.n] + "\n"); | |
return sb.toString(); | |
} | |
public String toString () { | |
return "Buffer: " + Arrays.toString(this.buffer) + '\n' + | |
"N = " + this.n + ", Head Index: " + this.head + ", Tail Index: " + this.tail + "\n" + | |
((this.isFull) ? "Buffer full" : "Buffer not full"); | |
} | |
} |
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
10 | |
A 6 | |
Fee | |
Fi | |
Fo | |
Fum | |
Gee | |
Gi | |
A 6 | |
Go | |
Gum | |
Bee | |
Bi | |
Bo | |
Bum | |
R 4 | |
L | |
Q |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment