Algorithm Pseudocodes Flashcards
Singly Linked List Node Class
public class SinglyLinkedList{private static Class Node{private E element;private Node next;public Node (E e, Node n){element = e;next = n;}public E getElement(){return element;}public Node getNext(){return next;}public void setNext(Node n){next = n;}}}
Singly Linked List removeFirst()
public E removeFirst(){if(isEmpty()) return null;E answer = head.getElement();head = head.getNext();size–;if (size == 0)tail = null;return answer;}
Doubly Linked List addInBetween() method
public void addInBetween(String data, Node node){ Node newNode = new Node(data, node.next, node); node.setNext(newNode); newNode.getNext().setPrev(newNode); }
Doubly Linked List removeFirst() method
public Node removeFirst(){ Node node = head.getNext(); head.setNext(node.getNext()); node.getNext().setPrev(node.getPrev()); size–; return node; }
Doubly Linked List removeLast() method
public Node removeLast(){ Node node = tail.getPrev(); tail.setPrev(node.getPrev()); node.getPrev().setNext(node.getNext()); size–; return node; }
Doubly Linked List toArrayFromFirst() method
public String[] toArrayFromFirst(){ String[] nodeArray = new String[size]; Node position = head.getNext(); for(int i = 0; i < size; i++){ nodeArray[i] = position.getData(); position = position.getNext(); } return nodeArray; }
Doubly Linked List toArrayFromLast() method
public String[] toArrayFromLast(){ String[] nodeArray = new String[size]; Node position = tail.getPrev(); for (int i = 0; i < size; i++){ nodeArray[i] = position.getData(); position = position.getPrev(); } return nodeArray; }
Doubly Linked List deepClone() method
public DoublyLinkedList deepClone(){ DoublyLinkedList List = new DoublyLinkedList(); Node position = head.getNext(); for(int i = 0; i < size(); i++){ List.addLast(position.getData()); position = position.getNext(); } return List;