Upload
katrina-freeman
View
212
Download
0
Embed Size (px)
Citation preview
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?
Variations of Linked Lists Circular Linked list
Circular list with pointer to last node
How to display all the contents in each node?
List or head
20 45 51 76
head
20 45 51 76
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
20 765145
Variations of Linked Lists
Circular doubly-linked list Using dummy Head
Head
765145Dummy
Head node
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): 1. Change the next pointer of the node pointed
by curr2. Change the prev pointer of the new node to
point to the preceding node of curr3. Set the Prev pointer in the node following
the new node to point to the new node4. 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):
1. Change the next pointer of the node that precedes curr
2. 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
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 inputprint BillsRead Bill Info
Read BillInsert Bill
Into LL
Assignment 3
class BillNode contains Serial Number, Deno, Month, Date.
class BillList contains head of LL, those methods.
Class Assignment3 contains the main ()