1 / 31

8-1

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; }

jeb
Télécharger la présentation

8-1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 8-1

  2. Orp.setNext(q);

  3. :Object :Object

  4. 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; } }

  5. 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 }

  6. 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 }

  7. 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

  8. 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).

  9. 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).

  10. Previous

  11. 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.

  12. 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.

More Related