Presentation is loading. Please wait.

Presentation is loading. Please wait.

8-1.

Similar presentations


Presentation on theme: "8-1."— Presentation transcript:

1 8-1

2

3

4

5

6

7

8

9

10 Or p.setNext(q);

11

12

13

14

15 :Object :Object

16 public class List { private node head; private node tail; private String Name; public List() { head = tail = null; Name = “No Name”; } public List(String name) { Name = name; public boolean isEmpty() { return head == null;

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

18

19

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

21

22 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 if(isempty()) else{ tail.setNext(newnode); Order is important in these two statements

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

24

25 else

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

27 Previous

28

29

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

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


Download ppt "8-1."

Similar presentations


Ads by Google