Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datastructure.LinkedList.arrayedStack.Display;
- import lists.ListInter;
- public class DoubleLinkedList<T>
- implements ListInter<T> {
- protected Node<T> current = null;
- protected Node<T> first = null;
- protected int currentPos = 0;
- protected int size = 0;
- protected class Node<T> {
- public T entery;
- public Node<T> next;
- public Node<T> before;
- public Node() {
- }
- public Node(T entery) {
- this();
- this.entery = entery;
- }
- public Node(T entery, Node<T> next) {
- this(entery);
- this.next = next;
- }
- public Node(T entery, Node<T> next, Node<T> before) {
- this(entery, next);
- this.before = before;
- }
- }
- public DoubleLinkedList() {
- }
- /**
- * p begin from 0 to 9 system
- * reach before off place "p"
- * @param p should be integer number from 1 to size-1
- * it mean it should be from second element to last element
- * take care p must be <= size -1 && >=1
- * it throws OutBoundException if "p" does not satisfy conditions above
- * you should not call it for reaching to (p= size-1 ) as "reachLast" is more efficient for that
- * @post:the pointer "current" move to "p-1" place and "currentPos" equal p-1(before p)
- * and current go ahead or go back so currentPos get increased or get decreased or remain if p=currentPos+1
- */
- protected void reachBefore (int p){
- if (p<1||p>size-1)throw new IndexOutOfBoundsException("index "+p+" is not true index should be at level [1,size-1]");
- final int MAX_LOOP=--p-currentPos;// --p to reach p place now
- int i = 0;
- if (MAX_LOOP>0)
- for (; i < MAX_LOOP; i++)
- current=current.next;
- else
- for (; i < Math.abs(MAX_LOOP); i++) {
- current = current.before;
- }
- currentPos = p;
- }
- protected void reachFirst(){
- current=first;
- currentPos=0;
- }
- protected void reachLast(){
- current =first.before;
- currentPos=size-1;
- }
- public boolean listEmpty() {
- return size == 0;
- }
- public boolean listFull() {
- return false;
- }
- public int listSize() {
- return size;
- }
- @Override
- public boolean destroyList() {
- return false;
- }
- @Override
- public void insertList(T element, int p) {
- if (p==0) {
- push(element);
- return;
- }
- else if (p==size) {
- reachLast();
- }
- else
- reachBefore(p);
- connect(element);
- }
- protected void connect(T element){
- Node<T> node=new Node<>(element);
- node.before=current;
- node.next=current.next;
- current.next.before=node;
- current.next=node;
- size++;
- }
- public void push(T element){
- reachFirst();
- Node<T> node=new Node<>(element);
- first=node;
- if (current==null) {
- current = first;
- current.next=node;
- }
- else {
- node.next = current;
- node.before = current.before;
- current.before.next = node;
- }
- current.before = node;
- size++;
- }
- public void append(T element){
- reachLast();
- connect(element);
- }
- public void deleteList(int p) {
- if (p==0) {
- reachFirst();
- destroyAndConnect(current);
- }
- else if(p==size-1) {
- reachLast();
- destroyAndConnect(current);
- }
- else {
- reachBefore(p);
- destroyAndConnect(current.next);
- }
- }
- public T pop(){
- reachLast();
- T element=first.before.entery;
- return element;
- }
- public T serve(){
- reachFirst();
- T rt=first.entery;
- return rt;
- }
- @Override
- public T retreiveList(int p) {
- if (p==0)
- return serve();
- else if (p==size-1)
- return pop();
- else{
- reachBefore(p);
- return current.next.entery;
- }
- }
- /**
- * destroy element the current point at
- * this method make all the node content point to null to help GC
- * current must not refer to null (it's empty)
- * else it cause NullPointerException Error
- */
- private void destroyAndConnect(Node<T>pn){
- current=pn.next;
- ++currentPos;
- current.before=pn.before;
- pn.before.next=current;
- if (pn==first) {
- first = current;
- currentPos--;
- }
- pn.next=null;
- pn.entery=null;
- pn.before=null;
- size--;
- }
- @Override
- public boolean replaceList(T newValue, int p) {
- if (p==0) {
- reachFirst();
- current.entery=newValue;
- }
- else if (p==size-1) {
- reachLast();
- current.entery=newValue;
- }
- else {
- reachBefore(p);
- current.next.entery=newValue;
- }
- return true ;
- }
- @Override
- public void traverseList(Display<T> d) {
- Node<T> pq=first;
- for (int i=0;i<size;i++,pq=pq.next){
- d.display(pq.entery);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment