ece 242 data structures
play

ECE 242 Data Structures Lecture 6 Linked Lists September 21, 2009 - PDF document

ECE 242 Data Structures Lecture 6 Linked Lists September 21, 2009 ECE242 L6: Linked Lists Overview Problem: Can we implement data structures using something other than arrays? Individual objects can be more flexible Use references


  1. ECE 242 Data Structures Lecture 6 Linked Lists September 21, 2009 ECE242 L6: Linked Lists Overview ° Problem: Can we implement data structures using something other than arrays? • Individual objects can be more flexible • Use references to find “neighbors” ° Iterators will be important tools for linked lists • Search through the list ° Easier to implement than arrays. A little harder to visualize conceptually September 21, 2009 ECE242 L6: Linked Lists

  2. ArrayList Implementation - Delete An Item Operation: delete(2) maxsize-1 maxsize-1 Count=5 Count=4 4 E Remove an item D E 3 3 pos=2 pos at position 2 2 C 2 D 1 B 1 B 0 A 0 A Implementation is slow and costly Need to know max size of array September 21, 2009 ECE242 L6: Linked Lists Another Implementation of List ° Linked List a b c null ° Take as much memory as needed • no more and no less • allocate the memory dynamically if needed ° No need to move items around September 21, 2009 ECE242 L6: Linked Lists

  3. Advantage of Linked List ° Nodes can be located anywhere in the memory. ° Getting from one node to another by storing the reference of the next node ° Insertion/removal does not require moving other items Object a b c d e null September 21, 2009 ECE242 L6: Linked Lists Simple Linked List ° Singly Linked List (SLList) • A sequence of nodes ° Each node contains two fields • Item • Reference to next node a b c null head Node: its value is “a”, this node also has a link which points to its succeeding node September 21, 2009 ECE242 L6: Linked Lists

  4. Node Definition ° Define an abstract object Node: private class Node { private Object item; Node next; } ° Example for Employee Node: private class Node { Employee e; Node next; } September 21, 2009 ECE242 L6: Linked Lists Singly Linked List --- head/tail ° Important variables in Singly Linked List • head Indicates the beginning of the List • tail Indicates the end of the List tail head node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists

  5. Search() Method In Listed Link ° For an empty list, return empty list ° For a nonempty list • Search for the wantedNode from head, print the record if matched tail head wantedNode node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists Size() Method In Listed Link ° For an empty list, return size 0 ° For a nonempty list • count the total number of nodes in the Linked List tail head node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists

  6. Add New Node ° Case 1: for an empty list • set head and tail to newNode head newNode tail ° Case 2: for a non-empty list, find where newNode should be inserted • (a) insert in front of head • (b) insert in the middle of list • (c) insert after tail tail head node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists Add New Node --- Case 2 (a) ° (a) Insert newNode in front of head tail head head newNode node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists

  7. Add New Node --- Case 2 (b) • (b) Insert newNode in the middle of list – Suppose newNode needs to be inserted between node 2 and node 3 tail head node1 node2 node3 node4 September 21, 2009 ECE242 L6: Linked Lists Add New Node --- Case 2 (b) • (b) Insert newNode in the middle of list – Suppose newNode needs to be inserted between node 2 and node 3 tail head node1 node2 node3 node4 newNode September 21, 2009 ECE242 L6: Linked Lists

  8. Add New Node --- Case 2 (b) • (b) Insert newNode in the middle of list – Suppose newNode needs to be inserted between node 2 and node 3 tail head node1 node2 node3 node4 newNode September 21, 2009 ECE242 L6: Linked Lists Add New Node --- Case 2 (b) • (b) Insert newNode in the middle of list – Suppose newNode needs to be inserted between node 2 and node 3 tail head node1 node2 node3 node4 newNode September 21, 2009 ECE242 L6: Linked Lists

  9. Add New Node --- Case 2 (c) ° (c) Insert newNode after tail head tail node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists Add New Node --- Case 2 (c) ° (c) Insert newNode after tail head tail node1 node2 node3 newNode September 21, 2009 ECE242 L6: Linked Lists

  10. Add New Node --- Case 2 (c) ° (c) Insert newNode after tail head tail node1 node2 node3 newNode September 21, 2009 ECE242 L6: Linked Lists Add New Node --- Case 2 (c) ° (c) Insert newNode after tail head tail tail node1 node2 node3 newNode September 21, 2009 ECE242 L6: Linked Lists

  11. Delete Node --- Case 2 (a) ° (a): wantedNode is both head and tail head head=null after deletion node 1 tail tail=null September 21, 2009 ECE242 L6: Linked Lists Delete Method --- Case 2(b) ° (b): wantedNode is head, but not tail • Suppose node 1 needs to be deleted tail head node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists

  12. Delete Method --- Case 2(b) ° (b): wantedNode is head, but not tail • Suppose node 1 needs to be deleted tail head node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists Delete Method --- Case 2(b) ° (b): wantedNode is head, but not tail tail head node2 node3 September 21, 2009 ECE242 L6: Linked Lists

  13. Delete Method --- Case 2(c) ° (c): wantedNode is not head, not tail • Suppose node2 needs to be deleted tail head node1 node2 node3 To be deleted September 21, 2009 ECE242 L6: Linked Lists Delete Method --- Case 2(c) ° (c): wantedNode is not head, not tail • Suppose node2 needs to be deleted tail head node1 node2 node3 To be deleted September 21, 2009 ECE242 L6: Linked Lists

  14. Delete Method --- Case 2(c) ° (c): wantedNode is not head, not tail • Suppose node2 needs to be deleted tail head node1 node3 September 21, 2009 ECE242 L6: Linked Lists Delete Method --- Case 2(d) ° (d): wantedNode is not head, but tail tail head node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists

  15. Delete Method --- Case 2(d) ° (d): wantedNode is not head, but tail tail head node1 node2 node3 September 21, 2009 ECE242 L6: Linked Lists Delete Method --- Case 2(d) ° (d): wantedNode is not head, but tail tail head node1 node2 September 21, 2009 ECE242 L6: Linked Lists

  16. Summary ° Size grows as needed ° Data manipulation is efficient • Add, remove, etc. ° Linked List implementation is more efficient than array ° We will lots more implementations and uses of linked lists in subsequent lectures September 21, 2009 ECE242 L6: Linked Lists

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend