Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * LinkedList
- *
- * Many thanks to the inspectors who vastly improved this document. It
- * has been made a much better product as a result.
- *
- * This code implements the following features:
- * Head and Tail - This list accurately keeps track of both head and tail
- * Doubly Linked List - The nodes support a notion of containing pointers
- * that aim to the next node and the previous node.
- * The concept of a "Current" Pointer - that node we're
- * currently aimed at is supported by an intNode class that maintains
- * both forward and backward pointers.
- * Here are the properties of the Current Pointer:
- * -- The Current Pointer can be in a number of states. These states
- * will be represented by an integer when the method
- * getCurrentPointerState() asks for the state of the Current Pointer,
- * represented in a particular way, but internally the states may be
- * represented in any fashion desired.
- * The states are:
- * Head - this state means that the Current Pointer is at the
- * head of the list - it is NOT at the first node.
- * Tail - this state means that the Current Pointer is at the
- * tail of the list - it is NOT at the last node.
- * Node Position - The Current Pointer has a state that's
- * represented as the position of a node on the list.
- * For instance if head --> NodeID=3 --> NodeID=4 --> null
- * Then when Current Pointer = 2, it represents the node with ID = 4.
- * Note use of the word "represents" rather than "points to" describe
- * this state.
- *
- * -- The Current Pointer supports sequential access; the Current
- * Pointer can be moved to a new state by using the methods
- * setCurrentAtHead()
- * setCurrentAtTail()
- * setCurrentToNext()
- * setCurrentToPrevious()
- * and the state of Current Pointer can be determined with
- * getCurrentPointerState()
- *
- * Additional properties of the linked list, as exemplified by
- * its methods, include these:
- * addNodeAfterCurrent()
- * getNodeAtCurrent()
- * removeNodeAtCurrent()
- * getListLength()
- * printLinkedList()
- * sort()
- * getNodeVersion()
- *
- * All these methods are described below.
- *
- * Pre-Release February 23, 2010
- * Inspected on February 25, 2010 by students in CS121
- * Released for use on February 26, 2010.
- * Authors Alex Bird, J. Breecher
- **/
- public class LinkedList {
- IntNode head = null;
- IntNode tail = null;
- int CurrentPosition = -1;
- /**
- * Constructor - defines a linked list with no nodes on the list
- * @param none </CODE>
- * <dt><b>Postcondition:</b><dd>
- * There are no nodes placed on the Linked List by this constructor.
- * The Current Pointer is set to the state -head-
- **/
- public LinkedList() {
- // Code will be added here by students
- head = null;
- tail = null;
- CurrentPosition = -1;
- }
- /**
- * Sets the Current Pointer to the head of the Linked List
- * @param None
- * <dt><b>Postcondition:</b><dd>
- * The Current Pointer is set in the state -head-
- **/
- public void setCurrentAtHead() {
- // Code will be added here by students
- CurrentPosition = -1;
- }
- /**
- * Sets the Current Pointer to the tail of the Linked List
- * @param none </CODE>
- * <dt><b>Postcondition:</b><dd>
- * The Current Pointer is set in the state -tail-
- **/
- public void setCurrentAtTail() {
- // Code will be added here by students
- CurrentPosition = -2;
- }
- /**
- * Sets the Current Pointer to the state of the next node on the List
- * @param None
- * <dt><b>Postcondition:</b><dd>
- * If Current Pointer has a state such that it can be legally moved,
- * it is placed in a state representing the next node
- * on the list.
- * If the result of advancing the Current Pointer would be to
- * position it after the last node, then the Current Pointer is
- * not moved but remains in its current state.
- * If the Current Pointer is -head- , and there are nodes on the
- * list, then the Current Pointer takes on a state representing
- * the first node on the list.
- * If the Current Pointer is -tail- , then the Current Pointer
- * is not legally moved.
- * @return
- * If Current Pointer has been legally moved, then return true.
- * otherwise return false.
- **/
- public Boolean setCurrentAtNext() {
- // Code will be added here by students
- if (CurrentPosition == -1 && getListLength() > 0) {
- CurrentPosition = 1;
- return true;
- }
- if (CurrentPosition == -2) {
- return false;
- }
- if (CurrentPosition >= 0 && CurrentPosition < getListLength()) {
- CurrentPosition++;
- return true;
- } else {
- return false;
- }
- }
- /**
- * Sets the Current Pointer to the state of the previous node on the List
- * @param none </CODE>
- * <dt><b>Postcondition:</b><dd>
- * If Current Pointer has a state such that it can be legally moved,
- * it is placed in a state representing the previous node
- * on the list.
- * If the result of "backwarding" the Current Pointer would be to
- * position it before the last node, then the Current Pointer is
- * not moved but remains in its current state.
- * If the Current Pointer is -tail- , and there are nodes on the
- * list, then the Current Pointer takes on a state representing
- * the last node on the list.
- * If the Current Pointer is -head- , then the Current Pointer
- * is not legally moved.
- * @return
- * If Current Pointer has been legally moved, then return true.
- * otherwise return false.
- **/
- public Boolean setCurrentAtPrevious() {
- // Code will be added here by students
- if (CurrentPosition == -2 && getListLength() > 0) {
- CurrentPosition = getListLength();
- return true;
- }
- if (CurrentPosition == -1) {
- return false;
- }
- if (CurrentPosition > 1) {
- CurrentPosition--;
- return true;
- } else {
- return false;
- }
- }
- /**
- * Returns the state of the Current Pointer. State means one of the
- * states that the pointer can occupy.
- * See the initial comments for this class to see the possible
- * states.
- * <dt><b>Postcondition:</b><dd>
- * The Current Pointer is not modified.
- * @return
- * If the Current Pointer is -head- , then a -1 is returned.
- * If the Current Pointer is -tail- , then a -2 is returned.
- * If the Current Pointer represents the position of a node,
- * then the position of that node in the linked list is returned.
- **/
- public int getCurrentPointerState() {
- // Code will be added here by students
- return CurrentPosition;
- }
- /**
- * Add a node based on the state of the Current Pointer
- * @param newNodeID</CODE>
- * This is the NodeID that will be used in the new node
- * @param newNodeData</CODE>
- * This is the data that will be used in the new node
- * <dt><b>Precondition:</b><dd>
- * none
- * <dt><b>Postcondition:</b><dd>
- * If Current Pointer is -head-, then the new Node is added
- * as the first node on the list.
- * If Current Pointer represents a node position, then the
- * New Node is added after the current node.
- * If Current Pointer is -tail-, then no action is performed.
- * If the addition occurs, the Current Pointer is set to
- * the location of the new node, otherwise the CP is not
- * modified.
- * @return
- * If addition occurs, then true is returned. Else false is returned.
- **/
- public Boolean addNodeAfterCurrent(int newNodeID, int newData) {
- // Code will be added here by students
- while (head == null) {
- IntNode originalHead = head;
- head = new IntNode(newNodeID, newData, tail, null);
- // If the head was null, there was nothing on the linked list so tail = head
- if (originalHead == null) {
- tail = head;
- }
- CurrentPosition = 1;
- return true;
- }
- if(CurrentPosition == -2)
- return false;
- while (head != null) {
- IntNode next = head;
- IntNode afterThisOne = null; //The node after which we'll be adding
- IntNode afterAdd = null; //The node before which we'll be adding
- while (next != null) {
- if (next == getNodeAtCurrent()) {
- afterThisOne = next;
- afterAdd = afterThisOne.getForwardLink();
- break;
- } else {
- next = next.getForwardLink();
- }
- }
- if (afterThisOne != null) {
- IntNode newNode = new IntNode(newNodeID, newData, afterThisOne.getForwardLink(), afterThisOne);
- afterThisOne.setForwardLink(newNode);
- if (afterAdd != null) {
- afterAdd.setBackwardLink(newNode);
- }
- if (newNode.getForwardLink() == null) {
- tail = newNode;
- }
- if (newNode.getBackwardLink() == null) {
- newNode.setBackwardLink(afterThisOne);
- }
- }
- CurrentPosition++;
- return true;
- }
- return false;
- }
- /**
- * Remove a node based on the state of the Current Pointer
- * <dt><b>Postcondition:</b><dd>
- * If Current Pointer represents a node position, then the Node at
- * that location is removed.
- * if Current Pointer is -head- or -tail-, then no removal happens.
- * The Current Pointer takes on a new state after the removal.
- * If there is a node now remaining before the removed node,
- * then Current Pointer aims at that node.
- * If the removed node was the first node on the list, then
- * Current Pointer takes on the state -head-.
- * @return
- * If removal is performed then true is returned, else false is returned.
- *
- **/
- public Boolean removeNodeAtCurrent() {
- // Code will be added here by students
- while (getCurrentPointerState() <= 0) {
- return false;
- }
- while (head == null) {
- return false;
- }
- while (CurrentPosition == 1) {
- if (head != null) {
- head = head.getForwardLink();
- }
- if (head == null) {
- tail = head;
- }
- CurrentPosition = -1;
- return true;
- }
- IntNode next = head;
- IntNode afterNext = null; // The node which we'll be removing
- IntNode beforeNext = null;
- while (next != null) {
- if (next.getForwardLink() == getNodeAtCurrent()) {
- afterNext = next.getForwardLink();
- beforeNext = next.getBackwardLink();
- break;
- } else {
- next = next.getForwardLink();
- }
- }
- if (afterNext != null) {
- if (afterNext != tail) {
- next.setForwardLink(afterNext.getForwardLink());
- next.setBackwardLink(beforeNext);
- } else {
- next.setForwardLink(null);
- next.setBackwardLink(beforeNext);
- tail = next;
- }
- CurrentPosition--;
- return true;
- }
- return false;
- }
- /**
- * Returns The node represented by the State of Current Pointer
- * <dt><b>Postcondition:</b><dd>
- * The Current Pointer is not modified.
- * @return
- * If the Current Pointer state represents a node, that Node is returned.
- * If the Current Pointer state is -head- or -tail-, a null
- * is returned.
- **/
- public IntNode getNodeAtCurrent() {
- // Code will be added here by students
- IntNode NodeAtCurrent;
- if (CurrentPosition >= 0) {
- NodeAtCurrent = head;
- try {
- for (int i = 1; i < CurrentPosition; i++) {
- NodeAtCurrent = NodeAtCurrent.getForwardLink();
- }
- } catch (NullPointerException npe) {
- }
- } else {
- NodeAtCurrent = null;
- }
- return NodeAtCurrent;
- }
- /**
- * Returns the number of items on the Linked List
- * <dt><b>Postcondition:</b><dd>
- * Neither the Current Pointer nor any list items are modified.
- * @return
- * The number of nodes on the list. The head and tail are not nodes
- * even though they may point at nodes.
- **/
- public int getListLength() {
- // Code will be added here by students
- int len = 0;
- for (IntNode in = head; in != null; in = in.getForwardLink()) {
- len++;
- }
- return len;
- }
- /**
- * Takes a linked list of integers and rearranges the nodes so the integers
- * (data) stored are sorted into the order of smallest to largest, with
- * the smallest integer in the node at the head of the list.
- * If there are several Nodes have the same data values, then there is a
- * sub-sort so that the lowest NodeIDs come first.
- * Initial Data:
- * +-----+ +-----+ +-----+ +-----+ +-----+
- * ----> | ID=1|----> | ID=9|----> |ID=7 |---->|ID=6 |---->|ID=5 |
- * |dat=5| |dat=3| |dat=1| |dat=3| |dat=3|
- * +-----+ +-----+ +-----+ +-----+ +-----+
- * Sorted Data:
- * +-----+ +-----+ +-----+ +-----+ +-----+
- * ----> | ID=7|----> | ID=5|----> |ID=6 |---->|ID=9 |---->|ID=1 |
- * |dat=1| |dat=3| |dat=3| |dat=3| |dat=5|
- * +-----+ +-----+ +-----+ +-----+ +-----+
- * It is recommended that you look at Problem 4.7 on pages 239 - 240 of your text.
- * * <dt><b>Postcondition:</b><dd>
- * The return value is a head reference of a linked list with exactly the
- * same entries as the original list (including repetitions if any) but the
- * entries in this list are sorted from smallest to largest. The original
- * linked list is no longer available.
- * The Current Pointer is set to head.
- * @return
- * A pointer to the new Linked List is returned.
- **/
- public IntNode sort() {
- // Code will be added here by students
- //IntNode newList;
- IntNode previousNode = null;
- IntNode NextNode = head.getForwardLink();
- IntNode newHead = head;
- //newHead.setForwardLink(head.getForwardLink)
- int lowData = newHead.getData();
- int previousLowID = newHead.getNodeID();
- try {
- for (IntNode sorter = newHead; sorter != null; sorter = sorter.getForwardLink()) {
- NextNode = sorter.getForwardLink();
- previousNode = sorter.getBackwardLink();
- if (sorter.getData() > NextNode.getData()) {
- sorter.setForwardLink(NextNode.getForwardLink());
- previousNode.setForwardLink(NextNode);
- NextNode.setBackwardLink(previousNode);
- sorter.setBackwardLink(NextNode);
- NextNode.setForwardLink(sorter);
- sort();
- }
- // sort();
- }
- } catch (NullPointerException npe) {
- }
- //for(int i = 1; i < getListLength(); i++)
- //{
- //}
- //newList.printLinkedList();
- //CurrentPosition = -1;
- return newHead;
- }
- /**
- * Returns to the caller the version of the node as maintained by the node.
- * The Version number is a property of the Node class being used by this
- * Linked List. Should that Node change its properties, it would be
- * important to know what set of properties are embodied in the Node.
- * @return
- * The node version is returned.
- **/
- public String getNodeVersion() {
- // Code will be added here by students
- return IntNode.getNodeVersion();
- }
- /**
- * Print out the current state of the Linked List including the head,
- * all nodes on the list, and the tail.
- * <dt><b>Postcondition:</b><dd>
- * No list components are modified
- * There is a printout to output, but no return value from the method
- * THERE SHOULD BE NO NEED FOR STUDENTS TO MODIFY THIS METHOD
- **/
- public void printLinkedList() {
- // Get all the info necessary to print out the head
- IntNode next = head;
- String result = "";
- if (next == null) {
- result = "null";
- } else {
- result = " node " + next.getNodeID();
- }
- System.out.println("Head points to " + result);
- // Walk down the nodes and print out their info. Do this
- // until we get to a null pointer. Note that this uses
- // the toString method in the IntNode class.
- while (next != null) {
- System.out.println(next);
- next = next.getForwardLink();
- }
- // Get all the info necessary to print out the tail
- if (tail == null) {
- result = "null";
- } else {
- result = " node " + tail.getNodeID();
- }
- System.out.println("Tail points to " + result);
- System.out.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement