Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package blatt7;
- public class HeadList {
- Entry head;
- /**
- * constructor empty HeadList
- */
- public HeadList() {
- head = null;
- }
- /**
- * Appends a new element with value info to the end of this list
- *
- * @param info value of the new element
- */
- public void add(int info) {
- if (head != null) {
- add(head, info);
- } else {
- head = new Entry(null, null, info);
- head.first = head;
- }
- }
- private void add(Entry node, int info) {
- if (node.next == null) {
- node.next = new Entry(node.first, null, info);
- } else {
- add(node.next, info);
- }
- }
- /**
- * Removes and returns the element at position index from this list.
- *
- * @param index position of the element that is removed
- * @return value of the removed element
- */
- public int remove(int index) {
- int counter=0;
- Entry current = head;
- Entry current3 = head;
- while (current3.next != null) {
- current3=current3.next;
- counter++;
- }
- if (index>counter) {
- return Integer.MIN_VALUE;
- }
- if (index < 0 ) {
- return Integer.MIN_VALUE;
- }
- Entry current1 = head;
- if (index == 0) {
- head = head.next;
- current1.next = null;
- head.first = head;
- setHead(head);
- //czy zmieniam wszystkie first czy tylko dla nastepnego?
- return current.elem;
- }
- if (index == 1){
- for (int i = 0; i < index; i++) {
- current = current.next;
- }
- current1.next = current1.next.next;
- current.next = null;
- //setHead(head.next);
- return current.elem;}
- for (int i = 0; i < index; i++) {
- current1 = current1.next;
- }
- for (int i = 0; i < index-1; i++) {
- current = current.next;
- }
- current.next=current.next.next;
- current1.next=null;
- return current1.elem;
- }
- /**
- * sets the head of each list element to newHead
- *
- * @param newHead reference to the new head
- */
- private void setHead(Entry newHead) {
- int counter = 0;
- Entry current = head;
- Entry current1 = head;
- current.first = newHead;
- System.out.println(current.first);
- while (current1.next != null) {
- current1=current1.next;
- counter++;
- }
- for (int i = 0; i < counter; i++) {
- current = current.next;
- current.first = newHead;
- System.out.println(current.first);
- }
- }
- private Entry getTail() {
- Entry entry = head;
- while (entry.next != null) {
- entry = entry.next;
- }
- return entry;
- }
- /**
- * reverse the list example: [1,2,3,4,5] --> [5,4,3,2,1], [] --> [], [1] -->
- * [1]
- */
- public void reverse() {
- if (head!=null){
- Entry prev = null;
- Entry current = head;
- Entry next = current.next;
- while (current != null) {
- current.next=prev;
- prev=current;
- current=next;
- next = next == null ? null : next.next;
- }
- head=prev;
- setHead(getTail());}
- }
- @Override
- public String toString() {
- String out = "[";
- if (head != null) {
- out += head.elem;
- Entry tmp = head.next;
- while (tmp != null) {
- out = out + "," + tmp.elem;
- tmp = tmp.next;
- }
- }
- out += "]";
- return out;
- }
- public static void main(String[] args) {
- HeadList l = new HeadList();
- l.add(3);
- l.add(4);
- l.add(5);
- l.add(6);
- l.add(7);
- //System.out.println(l.remove(555));
- l.reverse();
- //l.getTail();
- System.out.println("empty list: " + l);
- // Test implementation
- }
- class Entry {
- Entry first;
- Entry next;
- int elem;
- public Entry(Entry first, Entry next, int elem) {
- this.first = first;
- this.next = next;
- this.elem = elem;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement