1 / 20

CS2006- Data Structures I

CS2006- Data Structures I. Chapter 5 Linked Lists III. Linked Lists. Types of lists: Singly-Linked: Lists with only one “pointer” per node. Doubly-Linked: Lists with two pointers per node. Multi-Linked: Lists with two or more pointers per node Circular: Last node points to the head.

Télécharger la présentation

CS2006- Data Structures I

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. CS2006- Data Structures I Chapter 5Linked Lists III

  2. Linked Lists • Types of lists: • Singly-Linked: • Lists with only one “pointer” per node. • Doubly-Linked: • Lists with two pointers per node. • Multi-Linked: • Lists with two or more pointers per node • Circular: • Last node points to the head

  3. Variations of Linked Lists • Circularly-Linked Lists • Every node points to its successor • An external pointer points to the list’s head • Exercise: • Write the algorithm to traverse a circular list • What are the modification to be done to the original algorithm?

  4. 20 45 51 76 20 45 51 76 List or head head Variations of Linked Lists • Circular Linked list • Circular list with pointer to last node How to display all the contents in each node?

  5. Head 20 76 51 45 Variations of Linked Lists • Doubly-Linked Lists • Each node has two references, one for predecessor (prev) and another for successor (next) (how to change the code?) • Allows deleting without traversing the list to find the previous node • Insertion is more involved than singly-linked list • Special case?

  6. Head 76 51 45 Dummy Head node Variations of Linked Lists • Circular doubly-linked list • Using dummy Head

  7. Doubly Linked List Implementation public class Node { private Object item; private Node next; private Node prev; public Node(Object newItem) { item = newItem; next = null; prev = null; } // end constructor public Node(Object newItem, Node nextNode, Node prevNode) { item = newItem; next = nextNode; prev = prevNode; } // end constructor

  8. Doubly Linked List Implementation (2) • Assume: • add set and get methods for prev reference to the Node class. • possibly change the insertion and deletion methods in the List class.

  9. Doubly Linked List Insertion • Node Insertion (add after curr): • Change the next pointer of the node pointed by curr • Change the prev pointer of the new node to point to the preceding node of curr • Set the Prev pointer in the node following the new node to point to the new node • Set the next pointer in the preceding node to point to the new node

  10. Doubly Linked List Deletion • Node Deletion (delete the node pointed by curr): • Change the next pointer of the node that precedes curr • Change the prev pointer of the node that follows curr to point to the preceding node of curr • (cur.getPrev()). getNext ()= cur.getNext(); • (cur.getNext()).getPrev()= cur.getPrev();

  11. Review • An array-based implementation of an ADT list ______. • requires less memory to store an item than a reference-based implementation • is not a good choice for a small list • has a variable size • has items which explicitly reference the next items

  12. Review • In a reference-based implementation of an ADT list ______. • increasing the size of the list can waste storage and time • less memory is required to store an item than in an array-based implementation • an item explicitly references the next item • the location of the item after a particular item is implied

  13. Review • In a linear linked list, ______. • the next reference of each node has the value null • the last node references the first node • the precede reference of the dummy head node references the last node • the next reference of the last node has the value null

  14. Review • In all circular linked lists, ______. • every node references a predecessor • every node references a successor • the next reference of the last node has the value null • each node references both its predecessor and its successor

  15. Review • Which of the following is NOT true about all circular linked lists? • every node references a successor • the last node references the first node • the precede reference of each node references the node that precedes it • no node contains null in its next reference

  16. Review • Which of the following is true about all doubly linked lists? • each node references both its predecessor and its successor • the precede reference of the last node has the value null • the last node references the first node • the precede reference of the first node references the last node

  17. Review • A dummy head node ______. • facilitates adding nodes at the end the linked list • is used to store the first item in the linked list • is the second node in the linked list • is always present, even when the linked list is empty

  18. Assignment 3 • Write a program to manipulate the bill info • Using linked List • Each node contains one bill record • Loop • Read from file • Insert into the list • Print the list • Prompt to the user for serial number of a bill • If not found, ask for more info, to insert • If found, ask if a deletion is needed

  19. Structure Charts • Structure chart: Bill List Prompt for input Read Bill Info print Bills Insert Bill Into LL Read Bill

  20. Assignment 3 • class BillNode contains Serial Number, Deno, Month, Date. • class BillList contains head of LL, those methods. • Class Assignment3 contains the main ()

More Related