Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.franklinhool.lab2;
- import java.util.Collection;
- /**
- * Course: EECS 114 Fall 2015
- *
- * First Name: Franklin Last Name: Hool Lab Section: 1A email address:
- * fhool@uci.edu
- *
- * Assignment: Lab 2 Filename : DoublyLinkedList
- *
- * I hereby certify that the contents of this file represent my own original
- * individual work. Nowhere herein is there code from any outside resources such
- * as another individual, a website, or publishings unless specifically
- * designated as permissible by the instructor or TA.
- *
- * @param <E>
- */
- public class DoublyLinkedList<T> {
- Node head;
- Node tail;
- class Node<T> {
- // TODO: add fields here
- private T t;
- private Node<T> next;
- private Node<T> prev;
- public Node() {
- // TODO Auto-generated constructor stub
- this(null);
- }
- public Node(T t) {
- this.t = t;
- }
- }
- public <T> DoublyLinkedList() {
- this.head = null;
- this.tail = null;
- }
- public DoublyLinkedList(Collection<? extends T> c) {
- }
- public void add(int index, T t) {
- Node<T> temp = new Node<>(t);
- if (index == 0) {
- temp.prev = null;
- temp.next = this.head;
- this.head.prev = temp;
- this.head = temp;
- } else if (index == -1) {
- temp.prev = this.tail;
- temp.next = null;
- this.tail.next = temp;
- this.tail = temp;
- }
- }
- public T remove(int index) {
- if (index == 0) {
- this.head = this.head.next;
- } else if (index == -1) {
- this.tail = this.tail.prev;
- }
- return null;
- }
- public T remove(Object o) {
- Node<T> temp = this.head;
- while ((temp = temp.next) != null) {
- if (temp.equals(o)) {
- temp.prev.next = temp.next;
- temp.next.prev = temp.prev;
- temp.next = null;
- temp.prev = null;
- break;
- }
- }
- return null;
- }
- /**
- * * Returns the element at the specified position. * * @param index the
- * position in which to look for. * @return the element at the given
- * position in the list. If the index is out of range (index < 0 || index
- * >=size())
- */
- public T get(int index) {
- Node<T> temp = this.head;
- int count=0;
- while ((temp = temp.next) != null) {
- if (index == count) {
- return temp.t;
- }
- count++;
- }
- return null;
- }
- public int size() {
- Node<T> temp = this.head;
- int count=0;
- while ((temp = temp.next) != null) {
- count++;
- }
- return count;
- }
- public boolean isEmpty() {
- if(this.head==null || this.tail==null)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement