Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Algemeiner Code für eine Verketette Liste
- public class RingBuffer {
- public static void main(String[] args){
- RingBuffer rb = new RingBuffer(3);
- rb.put(1);
- rb.put(2);
- rb.put(3);
- rb.put(4);
- rb.put(5);
- rb.put(6);
- rb.put(7);
- rb.put(8);
- rb.put(9);
- System.out.println(rb.getHead());
- System.out.println(rb.getSum());
- rb.print();
- }
- public Cons head;
- public int capacity = 1; // Übrige Kapazizät
- public int currentCapacity = 0;
- public int maxCapacity = 1;
- public RingBuffer(){
- }
- public RingBuffer(int capacity){
- this.capacity = capacity;
- this.maxCapacity = capacity;
- }
- public void put(int i){
- // Neue Zelle mit Inhalt erstellen.
- Cons cell = new Cons(i);
- // Alle fälle > 0. Wenn Also noch Kapazität vorhanden ist.
- if (this.capacity > 0){
- cell.next = this.head; // Alle vorherigen Zellen werden in die neue Zelle geschrieben.
- this.head = cell; // Aktuelle Zelle(Head) wird mit neuer Zelle überschrieben.
- // Diese wird dann selbst zum Kopf.
- this.capacity--;
- this.currentCapacity++;
- }
- else { // Hier wird der Kopf überschrieben anstatt neu hinzugefügt.
- cell.next = head.next; //Neue Zelle übernimmt alle darauf folgenden Zellen vom Kopf
- this.head = cell; //Kopf Inhalt wird überschrieben, aber alle darauf folgenden Zellen bleiben erhalten ^.
- }
- }
- public boolean isEmpty(){
- return this.head == null;
- }
- public int getHead(){
- if (isEmpty()){
- return -1;
- }
- else{
- return this.head.zahl;
- }
- }
- public int getSum(){
- int sumStepCounter = 0;
- int sum = 0;
- return getSum(this.head, sum, sumStepCounter++);
- }
- protected int getSum(Cons cell, int sum, int sumStepCounter){
- if (cell == null)
- {
- return sum;
- }
- else {
- sum += cell.zahl;
- if (sumStepCounter == currentCapacity){
- return sum;
- }
- return getSum(cell.next, sum, sumStepCounter++);
- }
- }
- public void print(){
- int printStepCounter = 0;
- System.out.print("[ ");
- print(this.head, printStepCounter);
- System.out.print(" ]");
- }
- protected void print(Cons cell, int counter){
- if (counter == currentCapacity){
- return;
- }
- else{
- System.out.print(cell.zahl);
- print(cell.next, counter++);
- }
- }
- }
- class Cons{
- public int zahl; // Das Objekt in dieser Zelle
- public Cons next; // Verweis auf die nächste Zelle
- public Cons(int z){ // Konstruktor
- zahl = z;
- next = null;
- }
- }
- class ConsList{
- public Cons head, foot;
- public ConsList(){ // Konstruktor
- head = null;
- foot = null; // Neue Leere Liste //Foot ist "Optional"
- }
- public void insert(int z){
- Cons cons = new Cons(z); // neue Zelle mit Wert erstellen
- cons.next = head; // Neue Zelle zeigt auf Kopf
- head = cons; // Neue Zelle wird selbst zum Kopf
- if (foot == null)
- foot = cons;
- }
- public void append(int z){
- Cons cons = new Cons(z); // Neue Zelle
- if (foot == null){
- head = foot = cons; // genau eine Cons Zelle
- }
- else{ // Hinten anfügen und Fuß anpassen
- foot.next = cons;
- foot = cons;
- }
- }
- public void remove(int z){
- if (head == null) return;
- if (head.zahl == z){
- if (head == foot){
- foot = head = null;
- }
- else {
- head = head.next; //erste Zelle entfernen
- }
- }
- else remove(head, head.next, z);
- }
- //Hilfsmethode
- protected void remove(Cons prev, Cons cons, int z){
- if (cons == null) return;
- if (cons.zahl == z){
- // voherige Cons Zelle auf Nachfolgende zeigen lassen.
- // somit fällt 'cons' aus der Liste
- prev.next = cons.next;
- if (foot == cons){ //evtl. Fuß anpassen
- foot = prev;
- }
- else remove(cons, cons.next, z);
- }
- }
- //public void print(){
- // System.out.print("Liste [");
- // print(head);
- // System.out.print("]");
- //}
- //protected void print(Cons cons){
- // if (cons == null) return; // Letzte Zelle erreicht
- // System.out.print(cons.zahl);
- // if (cons.next != null){
- // System.out.print(", ");
- // print(cons.next);
- // }
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement