Presentation is loading. Please wait.

Presentation is loading. Please wait.

M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.

Similar presentations


Presentation on theme: "M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1."— Presentation transcript:

1 M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1

2 Implementing a Linked List So what would a Linked List implementation look like? What happens if we want to –Delete the first item? –Insert an item before the first item? class MyLinkedList { // Field ListNode first; // Methods void insert(DataType x, ???); void delete(DataType x, ???); void append(DataType x);... }

3 Header Nodes Deletion of first item and insertion of new first item are special cases. Can be avoided by using header node; –contains no data, but serves to ensure that first "real" node in linked has a predecessor. –To go to the first element, set current to header.next; –List is empty if header.next == null; Searching routines will skip header. A1A2A3 header

4 Linked Lists: List Implementation (partial) public class LinkedList implements List { ListNode header; // Constructor public LinkedList() { header = new ListNode (null); } // Test if the list is logically empty. public boolean isEmpty( ) { return header.next == null; } // Make the list logically empty public void makeEmpty( ) { header.next = null; } }

5 Representing the “current” position How do we specify where an operation should occur? –Index position (int?) –ListNode // Methods void insert(DataType x, ???); void delete(DataType x, ???); void insert(DataType x, int current); void delete(DataType x, int current); void insert(DataType x, ListNode current); void delete(DataType x, ListNode current); a x current

6 List Iterator Class Maintains a notion of the current position The List class provides methods that do not depend on any position (such as isEmpty, and makeEmpty ). A List iterator ( ListItr ) provides other methods such which act on the current position stored in the iterator: –next() / advance() –hasNext() / isValid() –retrieve()

7 Linked List Iterator: Implementation public class ListItr { ListNode current; // Current position ListItr(ListNode node) { } public boolean hasNext() { } public void next() { } public DataType retrieve() { } }

8 Example Usage A method that computes the number of elements in any list: public static int listSize (List theList) { int size = 0; ListItr itr; for(itr=theList.first();itr.hasNext();itr.next()) size++; return size; }

9 Linked Lists: Implementation public class List { // Header node private ListNode header; // Check if list is empty boolean isEmpty() {???} // Make the list empty void makeEmpty () {???} // Cursor to header node public ListItr zeroth() {???} // Cursor to first node public ListItr first() {???} // Cursor to (first) node containing x public ListItr find(T x) {???} // Cursor to node before node containing x public ListItr findPrevious(T x) {???} // Insert x after current cursor position public void insert(T x, ListItr current) {???} // Remove (first) node containing x public void remove(T x) {???} }

10 Print all elements of Linked List Method 1: Without Iterator, Simple Looping public class LinkedList { public void print() { // step through list, outputting each item ListNode p = header.next; while (p != null) { System.out.println (p.data); p = p.next; }

11 Print all elements of Linked List Method 4: Using Iterator class LinkedList { public void print() { ListItr > itr = first(); while(itr.hasNext()) { itr.next(); System.out.println(itr.retrieve()); }

12 Doubly-linked lists: Each list node stores both the previous and next nodes in the list. Useful for traversing linked lists in both directions. Circular-linked lists: Last node's next references the first node. Works with or without headers. Other Linked Lists A headtail prev next A B C first prev next

13 Doubly-linked lists: Wrong InsertNext newNode = new DoublyLinkedListNode ( x ); 1newNode.prev = current; 2 newNode.prev.next = newNode; … x b a 1 2

14 Doubly-linked lists: insertNext 1 newNode = new DoublyLinkedListNode (x); 2 newNode.prev = current; 3 newNode.next = current.next; 4 newNode.prev.next = newNode; 5 newNode.next.prev = newNode; 6 current = newNode; A B current X prev next newNode 1 3 25 4 6

15 Doubly-linked lists: DeleteCurrent 1.current.prev.next = current.next; 2.current.next.prev = current.prev; 3.current = current.prev; x b a current 1 2 3

16 16 DLLs compared to SLLs Advantages: –Can be traversed in either direction (may be essential for some programs) –Some operations, such as deletion and inserting before a node, become easier Disadvantages: –Requires more space –List manipulations are slower (because more links must be changed) –Greater chance of having bugs (because more links must be manipulated)


Download ppt "M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1."

Similar presentations


Ads by Google