Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name = Abdulmohsen ali asiri
- * ID = 36110218
- */
- package testintegerdll;
- public class DLL<T> {
- private DLLNode<T> head, tail;
- public DLL() {
- head = tail = null;
- }
- public boolean isEmpty() {
- return head == null;
- }
- public void clear() {
- head = tail = null;
- }
- public T getFirst() {
- if (head != null) {
- return head.info;
- } else {
- return null;
- }
- }
- public void addToHead(T el) {
- if (head != null) {
- head = new DLLNode<T>(el, head, null);
- head.next.prev = head;
- } else {
- head = tail = new DLLNode<T>(el);
- }
- }
- public void addToTail(T el) {
- if (tail != null) {
- tail = new DLLNode<T>(el, null, tail);
- tail.prev.next = tail;
- } else {
- head = tail = new DLLNode<T>(el);
- }
- }
- public T deleteFromHead() {
- if (isEmpty()) {
- return null;
- }
- T el = head.info;
- if (head == tail) // if only one node on the list;
- {
- head = tail = null;
- } else { // if more than one node in the list;
- head = head.next;
- head.prev = null;
- }
- return el;
- }
- public T deleteFromTail() {
- if (isEmpty()) {
- return null;
- }
- T el = tail.info;
- if (head == tail) // if only one node on the list;
- {
- head = tail = null;
- } else { // if more than one node in the list;
- tail = tail.prev;
- tail.next = null;
- }
- return el;
- }
- public void delete(T el) { // delete the node with an element el;
- DLLNode<T> tmp;
- for (tmp = head; tmp != null && !el.equals(tmp.info); tmp = tmp.next); //locate the item
- if (tmp != null) { // item found
- if (head == tail) //the found item was the only one
- {
- head = tail = null;
- } else if (head == tmp) { //the found item is the first
- head = head.next;
- head.prev = null;
- } else if (tail == tmp) { // the found item is the last
- tail = tail.prev;
- tail.next = null;
- } else { // the found item is in the middle
- tmp.prev.next = tmp.next;
- tmp.next.prev = tmp.prev;
- }
- }
- }
- public void printAll() {
- for (DLLNode<T> tmp = head; tmp != null; tmp = tmp.next) {
- System.out.print(tmp.info + " ");
- }
- }
- public T find(T el) {
- DLLNode<T> tmp;
- for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
- if (tmp == null) {
- return null;
- } else {
- return tmp.info;
- }
- }
- // node of generic doubly linked list class
- class DLLNode<T> {
- public T info;
- public DLLNode<T> next, prev;
- public DLLNode() {
- next = null;
- prev = null;
- }
- public DLLNode(T el) {
- info = el;
- next = null;
- prev = null;
- }
- public DLLNode(T el, DLLNode<T> n, DLLNode<T> p) {
- info = el;
- next = n;
- prev = p;
- }
- }
- public int length() {
- int count = 0;
- DLLNode<T> tmp = head;
- for (tmp = head; tmp != null; tmp = tmp.next, count++);
- return count;
- }
- //start of assignment
- public T get(int index) {
- DLLNode<T> tmp = head;
- int count = 0;
- if (index < 0 || index > length() - 1) {
- return null;
- } else {
- for (tmp = head; count != index; tmp = tmp.next, count++);
- return tmp.info;
- }
- }
- public int indexOf(T el) {
- DLLNode<T> tmp = head;
- int count = 0;
- for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next, count++);
- if (tmp == null) {
- return -1;
- } else {
- return count;
- }
- }
- public void replace(int index, T el) {
- DLLNode<T> tmp = head;
- int count = 0;
- for (tmp = head; tmp != null && count != index; tmp = tmp.next, count++);
- if (tmp != null) {
- tmp.info = el;
- }
- }
- public void insertAt(int index, T el) {
- DLLNode<T> tmp = head;
- int count = 0;
- if (index >= 0 && index <= length()) {
- if (index == 0) {
- addToHead(el);
- }
- else if (index == length()) {
- addToTail(el);
- }
- else {
- for (tmp = head; count != index; tmp = tmp.next, count++);
- tmp.prev.next = tmp.prev = new DLLNode<T>(el, tmp, tmp.prev);
- }
- }
- }
- public T deleteAt(int index) {
- DLLNode<T> tmp = head;
- int count = 0;
- if (index < 0 || index > length() - 1) {
- return null;
- } else if (isEmpty()) {
- return null;
- } else if (length() == 1) {
- T el = head.info;
- clear();
- return el;
- } else if (index == 0) {
- T el = tmp.info;
- head = tmp.next;
- head.prev = null;
- return el;
- } else if (index == length() - 1) {
- T el = tail.info;
- tail = tail.prev;
- tail.next = null;
- return el;
- } else {
- for (tmp = head; count != index; tmp = tmp.next, count++);
- T el = tmp.info;
- tmp.next.prev = tmp.prev;
- tmp.prev.next = tmp.next;
- return el;
- }
- }
- }
Add Comment
Please, Sign In to add comment