200 likes | 384 Views
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.
E N D
CS2006- Data Structures I Chapter 5Linked 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
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?
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?
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?
Head 76 51 45 Dummy Head node Variations of Linked Lists • Circular doubly-linked list • Using dummy Head
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
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.
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
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();
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
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
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
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
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
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
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
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
Structure Charts • Structure chart: Bill List Prompt for input Read Bill Info print Bills Insert Bill Into LL Read Bill
Assignment 3 • class BillNode contains Serial Number, Deno, Month, Date. • class BillList contains head of LL, those methods. • Class Assignment3 contains the main ()