Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. //Algemeiner Code für eine Verketette Liste
  2.  
  3.  
  4. public class RingBuffer {
  5. public static void main(String[] args){
  6. RingBuffer rb = new RingBuffer(3);
  7. rb.put(1);
  8. rb.put(2);
  9. rb.put(3);
  10. rb.put(4);
  11. rb.put(5);
  12. rb.put(6);
  13. rb.put(7);
  14. rb.put(8);
  15. rb.put(9);
  16. System.out.println(rb.getHead());
  17.  
  18.  
  19. System.out.println(rb.getSum());
  20. rb.print();
  21.  
  22. }
  23.  
  24. public Cons head;
  25. public int capacity = 1; // Übrige Kapazizät
  26. public int currentCapacity = 0;
  27. public int maxCapacity = 1;
  28.  
  29. public RingBuffer(){
  30. }
  31.  
  32. public RingBuffer(int capacity){
  33. this.capacity = capacity;
  34. this.maxCapacity = capacity;
  35. }
  36.  
  37. public void put(int i){
  38.  
  39. // Neue Zelle mit Inhalt erstellen.
  40. Cons cell = new Cons(i);
  41.  
  42. // Alle fälle > 0. Wenn Also noch Kapazität vorhanden ist.
  43. if (this.capacity > 0){
  44. cell.next = this.head; // Alle vorherigen Zellen werden in die neue Zelle geschrieben.
  45. this.head = cell; // Aktuelle Zelle(Head) wird mit neuer Zelle überschrieben.
  46. // Diese wird dann selbst zum Kopf.
  47. this.capacity--;
  48. this.currentCapacity++;
  49. }
  50. else { // Hier wird der Kopf überschrieben anstatt neu hinzugefügt.
  51. cell.next = head.next; //Neue Zelle übernimmt alle darauf folgenden Zellen vom Kopf
  52. this.head = cell; //Kopf Inhalt wird überschrieben, aber alle darauf folgenden Zellen bleiben erhalten ^.
  53. }
  54. }
  55. public boolean isEmpty(){
  56. return this.head == null;
  57. }
  58. public int getHead(){
  59. if (isEmpty()){
  60. return -1;
  61. }
  62. else{
  63. return this.head.zahl;
  64. }
  65. }
  66. public int getSum(){
  67. int sumStepCounter = 0;
  68. int sum = 0;
  69. return getSum(this.head, sum, sumStepCounter++);
  70. }
  71. protected int getSum(Cons cell, int sum, int sumStepCounter){
  72. if (cell == null)
  73. {
  74. return sum;
  75. }
  76. else {
  77. sum += cell.zahl;
  78. if (sumStepCounter == currentCapacity){
  79. return sum;
  80. }
  81. return getSum(cell.next, sum, sumStepCounter++);
  82. }
  83. }
  84.  
  85. public void print(){
  86. int printStepCounter = 0;
  87. System.out.print("[ ");
  88. print(this.head, printStepCounter);
  89. System.out.print(" ]");
  90.  
  91. }
  92. protected void print(Cons cell, int counter){
  93. if (counter == currentCapacity){
  94. return;
  95. }
  96. else{
  97. System.out.print(cell.zahl);
  98. print(cell.next, counter++);
  99. }
  100.  
  101. }
  102.  
  103. }
  104.  
  105. class Cons{
  106. public int zahl; // Das Objekt in dieser Zelle
  107. public Cons next; // Verweis auf die nächste Zelle
  108. public Cons(int z){ // Konstruktor
  109. zahl = z;
  110. next = null;
  111. }
  112. }
  113.  
  114. class ConsList{
  115. public Cons head, foot;
  116. public ConsList(){ // Konstruktor
  117. head = null;
  118. foot = null; // Neue Leere Liste //Foot ist "Optional"
  119. }
  120.  
  121. public void insert(int z){
  122. Cons cons = new Cons(z); // neue Zelle mit Wert erstellen
  123. cons.next = head; // Neue Zelle zeigt auf Kopf
  124. head = cons; // Neue Zelle wird selbst zum Kopf
  125. if (foot == null)
  126. foot = cons;
  127. }
  128.  
  129. public void append(int z){
  130. Cons cons = new Cons(z); // Neue Zelle
  131. if (foot == null){
  132. head = foot = cons; // genau eine Cons Zelle
  133. }
  134. else{ // Hinten anfügen und Fuß anpassen
  135. foot.next = cons;
  136. foot = cons;
  137. }
  138. }
  139.  
  140. public void remove(int z){
  141. if (head == null) return;
  142. if (head.zahl == z){
  143. if (head == foot){
  144. foot = head = null;
  145. }
  146. else {
  147. head = head.next; //erste Zelle entfernen
  148. }
  149. }
  150. else remove(head, head.next, z);
  151. }
  152.  
  153. //Hilfsmethode
  154. protected void remove(Cons prev, Cons cons, int z){
  155. if (cons == null) return;
  156. if (cons.zahl == z){
  157. // voherige Cons Zelle auf Nachfolgende zeigen lassen.
  158. // somit fällt 'cons' aus der Liste
  159. prev.next = cons.next;
  160. if (foot == cons){ //evtl. Fuß anpassen
  161. foot = prev;
  162. }
  163. else remove(cons, cons.next, z);
  164. }
  165. }
  166.  
  167. //public void print(){
  168. // System.out.print("Liste [");
  169. // print(head);
  170. // System.out.print("]");
  171. //}
  172. //protected void print(Cons cons){
  173. // if (cons == null) return; // Letzte Zelle erreicht
  174. // System.out.print(cons.zahl);
  175. // if (cons.next != null){
  176. // System.out.print(", ");
  177. // print(cons.next);
  178. // }
  179. //}
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement