ผลต่างระหว่างรุ่นของ "01204212/codes/linked lists"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 6 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 10: แถว 10:
 
public class Node {
 
public class Node {
 
private int val;
 
private int val;
private Node next = null;
+
protected Node next = null;
  
 
public Node(int val) {
 
public Node(int val) {
แถว 44: แถว 44:
  
 
public boolean isEmpty() {
 
public boolean isEmpty() {
return tail != null;
+
return tail == null;
 
}
 
}
  
แถว 58: แถว 58:
 
newNode.next = head;
 
newNode.next = head;
 
head = newNode;
 
head = newNode;
 +
size++;
 
return newNode;
 
return newNode;
 
}
 
}
แถว 103: แถว 104:
 
public class Node {
 
public class Node {
 
private T val;
 
private T val;
private Node next = null;
+
protected Node next = null;
  
 
public Node(T val) {
 
public Node(T val) {
แถว 137: แถว 138:
  
 
public boolean isEmpty() {
 
public boolean isEmpty() {
return tail != null;
+
return tail == null;
 
}
 
}
  
แถว 151: แถว 152:
 
newNode.next = head;
 
newNode.next = head;
 
head = newNode;
 
head = newNode;
 +
size++;
 
return newNode;
 
return newNode;
 
}
 
}
แถว 196: แถว 198:
 
private class Node {
 
private class Node {
 
private T val;
 
private T val;
private Node next = null;
+
protected Node next = null;
  
 
public Node(T val) {
 
public Node(T val) {
แถว 229: แถว 231:
 
}
 
}
 
 
T getVal() {
+
public T getVal() {
 
return currentNode.getVal();
 
return currentNode.getVal();
 +
}
 +
 +
public void setVal(T v) {
 +
currentNode.setVal(v);
 
}
 
}
 
 
void next() {
+
public void next() {
 
currentNode = currentNode.getNext();
 
currentNode = currentNode.getNext();
 +
}
 +
 +
public boolean isEnded() {
 +
return currentNode != null;
 
}
 
}
  
แถว 247: แถว 257:
  
 
public void removeAfter() {
 
public void removeAfter() {
 +
// ...
 
}
 
}
 
}
 
}
แถว 260: แถว 271:
  
 
public boolean isEmpty() {
 
public boolean isEmpty() {
return tail != null;
+
return tail == null;
 
}
 
}
  
แถว 267: แถว 278:
 
}
 
}
  
public Node addHead(T v) {
+
public Iterator addHead(T v) {
 
if (isEmpty()) {
 
if (isEmpty()) {
 
return add(v);
 
return add(v);
แถว 274: แถว 285:
 
newNode.next = head;
 
newNode.next = head;
 
head = newNode;
 
head = newNode;
return newNode;
+
return new Iterator(newNode, this);
 
}
 
}
 
}
 
}
  
 
public void removeHead() {
 
public void removeHead() {
if (head != null) {
+
// ...
head = head.next;
 
size--;
 
 
 
if (head == null) {
 
tail = null;
 
}
 
}
 
 
}
 
}
  
public Node add(T v) {
+
public Iterator add(T v) {
 
Node newNode = new Node(v);
 
Node newNode = new Node(v);
  
แถว 300: แถว 304:
  
 
size++;
 
size++;
return newNode;
+
return new Iterator(newNode, this);
 
}
 
}
  

รุ่นแก้ไขปัจจุบันเมื่อ 14:14, 13 กันยายน 2559

from 01204212

LinkedList for integers

Methods removeHead and addAfter are left out as exercises.

public class LinkedList {

	public class Node {
		private int val;
		protected Node next = null;

		public Node(int val) {
			this.val = val;
			this.next = null;
		}

		public Node() {
			this(0);
		}

		public Node getNext() {
			return next;
		}

		public int getVal() {
			return val;
		}

		public void setVal(int v) {
			val = v;
		}
	}

	private Node head = null;
	private Node tail = null;
	private int size = 0;

	public LinkedList() {
		head = tail = null;
		size = 0;
	}

	public boolean isEmpty() {
		return tail == null;
	}

	public int size() {
		return size;
	}

	public Node addHead(int v) {
		if (isEmpty()) {
			return add(v);
		} else {
			Node newNode = new Node(v);
			newNode.next = head;
			head = newNode;
			size++;
			return newNode;
		}
	}

	public void removeHead() {
		// ... 
	}

	public Node add(int v) {
		Node newNode = new Node(v);

		if (!isEmpty()) {
			tail.next = newNode;
			tail = newNode;
		} else {
			head = tail = newNode;
		}

		size++;
		return newNode;
	}

	public Node addAfter(Node node, int v) {
		// ... 
	}

	public void removeAfter(Node node) {
		if (node.next != null) {
			node.next = node.next.next;
			size--;
		}
	}

	public Node getHead() {
		return head;
	}
}

Generic linked lists (no iterator)

public class LinkedList<T> {

	public class Node {
		private T val;
		protected Node next = null;

		public Node(T val) {
			this.val = val;
			this.next = null;
		}

		public Node() {
			this(null);
		}

		public Node getNext() {
			return next;
		}

		public T getVal() {
			return val;
		}

		public void setVal(T v) {
			val = v;
		}
	}

	private Node head = null;
	private Node tail = null;
	private int size = 0;

	public LinkedList() {
		head = tail = null;
		size = 0;
	}

	public boolean isEmpty() {
		return tail == null;
	}

	public int size() {
		return size;
	}

	public Node addHead(T v) {
		if (isEmpty()) {
			return add(v);
		} else {
			Node newNode = new Node(v);
			newNode.next = head;
			head = newNode;
			size++;
			return newNode;
		}
	}

	public void removeHead() {
		// ... 
	}

	public Node add(T v) {
		Node newNode = new Node(v);

		if (!isEmpty()) {
			tail.next = newNode;
			tail = newNode;
		} else {
			head = tail = newNode;
		}

		size++;
		return newNode;
	}

	public Node addAfter(Node node, T v) {
		// ...
	}

	public void removeAfter(Node node) {
		if (node.next != null) {
			node.next = node.next.next;
			size--;
		}
	}

	public Node getHead() {
		return head;
	}
}

Generic list (with iterator)

public class LinkedList<T> {

	private class Node {
		private T val;
		protected Node next = null;

		public Node(T val) {
			this.val = val;
			this.next = null;
		}

		public Node() {
			this(null);
		}

		public Node getNext() {
			return next;
		}

		public T getVal() {
			return val;
		}

		public void setVal(T v) {
			val = v;
		}
	}

	public class Iterator {
		private Node currentNode;
		private LinkedList<T> list;
		
		Iterator(Node node, LinkedList<T> list) {
			currentNode = node;
			this.list = list;
		}
		
		public T getVal() {
			return currentNode.getVal();
		}

		public void setVal(T v) {
			currentNode.setVal(v);
		}
		
		public void next() {
			currentNode = currentNode.getNext();
		}

		public boolean isEnded() {
			return currentNode != null;
		}

		public Iterator add(T v) {
			Node newNode = new Node(v);
			newNode.next = currentNode.next;
			currentNode.next = newNode;

			list.size++;
			return new Iterator(newNode, list);
		}

		public void removeAfter() {
			// ...
		}	
	}
	
	private Node head = null;
	private Node tail = null;
	private int size = 0;

	public LinkedList() {
		head = tail = null;
		size = 0;
	}

	public boolean isEmpty() {
		return tail == null;
	}

	public int size() {
		return size;
	}

	public Iterator addHead(T v) {
		if (isEmpty()) {
			return add(v);
		} else {
			Node newNode = new Node(v);
			newNode.next = head;
			head = newNode;
			return new Iterator(newNode, this);
		}
	}

	public void removeHead() {
		// ...
	}

	public Iterator add(T v) {
		Node newNode = new Node(v);

		if (!isEmpty()) {
			tail.next = newNode;
			tail = newNode;
		} else {
			head = tail = newNode;
		}

		size++;
		return new Iterator(newNode, this);
	}

	public Iterator iterator() {
		return new Iterator(head, this);
	}
}