CS2006Ch04C

advertisement
CS2006- Data Structures I
Chapter 5
Linked Lists III
Linked Lists

Types of lists:

Singly-Linked:


Doubly-Linked:


Lists with two pointers per node.
Multi-Linked:


Lists with only one “pointer” per node.
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
head
20

45
51
76
Circular list with pointer to last node
20
45
51
How to display all the contents in each node?
List or
head
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
45
51
76
Variations of Linked Lists

Circular doubly-linked list

Using dummy Head
Head
Dummy Head
node
45
51
76
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.
2.
3.
4.
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):
1.
2.
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 ______.




11
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 ______.




12
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, ______.




13
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, ______.




14
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?




15
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?




16
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 ______.




17
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
Read Bill Info
Read Bill
print Bills
Insert Bill
Into LL
Prompt for input
Assignment 3



class BillNode contains Serial Number,
Deno, Month, Date.
class BillList contains head of LL, those
methods.
Class Assignment3 contains the main ()
Download