310 likes | 516 Views
8-1. Or p.setNext (q);. :Object. :Object. public class List { private node head; private node tail; private String Name; public List() { head = tail = null; Name = “No Name” ; } public List(String name) { head = tail = null; Name = name; }
E N D
:Object :Object
public class List { private node head; private node tail; private String Name; public List() { head = tail = null; Name = “No Name”; } public List(String name) { head = tail = null; Name = name; } public booleanisEmpty() { return head == null; } }
Method insertAtFront • The Node constructor call sets data to refer to the new object passed as an argument and sets reference next to null. • Set reference next of the new Node object to the current head node of the list. • Call isEmpty to determine whether the list is empty • If the list is empty, assign head and tail to the new Node object. • If the list is not empty, assign head to the new Node object and. public Node (Object obj){ data=obj; next=null }
Method insertAtBack • The Node constructor call sets data to refer to the new object passed as an argument and sets reference next to null • Call isEmpty to determine whether the list is empty • If the list is empty, assign head and tail to the new Node object. • If the list is not empty, find the last node by traversing “walking through the list” (or use the tail Node object). • Set reference next of the last Node object to the new Node Object. • Set tail to the new Node Object. public Node (Object obj){ data=obj; next=null }
Linked List InsertAtBack (cont) public void insertAtBack(Object obj) { Node newnode = new Node(obj); if (isEmpty()) head = tail = newnode; else { Node current = head; // Start at head of list while (current.getNext() != null) // Find the end of the list current = current.getNext(); current.setNext(newnode)// Insert the newObj tail=newnode; } } // insertAtRear} Another solution using the tail public void insertAtBack(Object obj) { Node newnode = new Node(obj); if(isempty()) head = tail = newnode; else{ tail.setNext(newnode); tail=newnode; } } Order is important in these two statements
Method removeFromFront • Call isEmpty to determine whether the list is empty. • If the list is empty, return null. • If the list is not empty, save a reference to the head node (call it first for example). • If there is only one node (i.e. head==tail), set both head and tail to null. • If there is more than one node, make head point to its next node. • Return the data of the saved first node (the previous list head).
Method removeFromBack • Call isEmpty to determine whether the list is empty. • If the list is empty, return null. • If there is only one node, set both head and tail to null. • If there is more than one node, create a Node reference current and assign it to the list’s head. • Using current, “Walk through” the list, each time moving current to the next node and saving the previous node, until you reach the last node in the list (i.e. current.next = null). • The last node is now current, and the node before last is previous. • Set the next node of previous to null and set tail to previous. • Return the data of the current node (the previously last node).
Stacks and Queues • A Stack is a Last in First out (LIFO) data structure. • We can use a linked list implementation of stacks. • We use the methods insertAtFront and removeFromFront to insert and remove items from the stack.
Stacks and Queues • A Queue is a First in First out (FIFO) data structure. • We can use a linked list implementation of a Queue. • We use the methods insertAtBack and removeFromFront to insert and remove items from the Queue.