Data Abstraction & Problem Solving with JAVA: Chapter 5 - Linked List

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Equality operators

(== and !=) This compares the values of the reference variables, not the objects that they reference

How to insert a new node into a linked list?

-Determine the point of insertion -Create a new node and store the new data in it -Connect the new node to the linked list by changing references

What are three steps to delete a node from a linked list?

-Locate the node that you want to delete -Disconnect this node from the linked list by changing references -Return the node to the system

How to delete a specified node from a linked list which "curr: reference?

-Set next in the node that precedes N to reference the node that follows N prev.next = curr.next; We are creating another reference variable called prev which keeps track of previous node in the linked list. Initially prev.next = curr. But if we want to delete the current node then prev.next should be pointing to curr.next.

What does curr.next do?

Accesses the address of the next node and makes it the current node.

An array of objects is...

Acctually an array of references to the objects -Example Integer[] scores = new Integer[30]; This first line of code actually creates an array of size 30. scores contains the address of the location where that array is stored. The content of the array is a bunch of reference variables which are initially null, since first only declares it. -Instantiating Integer objects for each array reference scores[0] = new Integer(7); scores[1] = new Integer(9); // and so on ... This second line starts initializing the array one by one. Each index will have address of the location, where that Integer is stored(since this is an array of objects)

The "new" operator can be used to do what?

Allocate memory dynamically for both an array and linked list? The size of a linked list can be increased one node at a time more efficiently than that of an array.

What does a dummy head node do?

Allows you to insert/delete a node at the beginning without having to do the special case for head node insertion/deletion

What is a resizeable array?

An array that grows and shrinks as the program executes. if your array reaches the maximum size then you can create a new array with a size which is twice the size of the original array, then copy each element of the original array into the new array.

Reference-based implementation use what?

An explicit ordering scheme

Array-based implementation uses what?

An implicit ordering scheme

Array-Based vs Reference-Based. Access time.

Array base has constant access time no matter the size Reference base - to access ith node depends on i

Array-Based vs Reference-Based. Storage.

Array-Based require less memory No need to store explict information about where to find the next data item Reference-base require more storage, mostly because it requires a reference to the next item in the list

Array-Based vs Reference-Based. Insertion/Deletion.

Array-based requires you to shift data Reference based does not require you to shift data Requires a list traversal

Arrays vs Linked Lists

Arrays are physically sequentially Linked list each node itself is an object which holds reference to the next member of that collection. so linked list is not physical, but nodes can exist anywhere and can be accessed through references. Insertion/deletion not tedious. all you have to do is change the references.

Array-Based vs Reference-Based. Size.

Arrays have a fixed size Resizing the array can waste storage and time Reference Based does not have a fixed size Will not waste storage

dot.equals method

Compares the objects field by field

Object References - Reference variable

Contains the location an object Example - Intgeger intRef = new Intgeger(5);

How can you access an array?

Directly

What does a doubly linked list do?

Each node consist of two references curr.preceding and prev.prececding Allows you to delete without using a trailing reference and changing it at every iteration.

What does an iterator do?

Gives you the ability to cycle through items in a collection Access next item in a collection by using ier.next();

Using a circular doubly linked list allows you to not?

Have to perform insertion and deletion operations with a special case.

By setting the last node as null, you are...

Indicating the end of the linked list.

A reference variable that no longer references any object is...

Marked for garbage collection

Node class example

Node n = new Node (new Integer(6)); Node first = new Node (new Integer(9), n); When you create object n of node class , you are creating a node with data as 6, but since you dont know the address of the next node you will keep it null. When you create a second object called first, you are adding a node right before n. The data in there is 9 and next is given the address of previously created node which is n.

Data field "next" in the last node is set to?

Null

The Node class contains what two data fields?

Object item; Node next; The actual contents of the object and the location in memory of the next Node.

Object references is when...

One reference variable is assigned to another reference variable, both references then refer to the same object Integer p, q; p = new Integer(6); q = p;

What ADTs operations/methods are in the reference-based implementation?

PUBLIC •isEmpty •size •add •remove •get •removeAll PRIVATE find

What do tail references do?

Refereence where the end of the linked list is Adds a node to the end of the linked list

How to display the data portion of the current node?

System.out.println(curr.item);

Nodes contain?

The actual content and the address to the location of the next node

References contains not...

The actual content but the address of the location where the content is stored.

Preceding reference of the dummy head node references to?

The last node

What does a circular linked list do?

The last node does not reference the first node in the list The last node references the first node Every node has a successor

What is a "head" reference variable?

The lilst first node Always exist even if the list is empty

When an object is passed to a method as an argument...

The reference to the object is copied to method's formal parameter

How can you access a linked list?

Traversal

Reference-based ADT implementations and data structres use java reference. True/False.

True

True or false. Items can be inserted or delete from reference based linked list without shifting data?

True. An array would require you to shift the data to the left or right.

How do you display the contents of a linked list?

Use the "curr" reference variable -References the current node -Initially references the first node

Inserting a node in the beginning is a special case because...

You dont have a previous pointer. In this case , head is pointing to newnode and newnode.next is pointing to the head node

How do you create a Node object?

You have to have a class which has the two things that we mentioned above. The access modifier for this class is default , which means that only classes in the same package can access it. same thing for the datafields of this class.

a linked list consists of

a finite set of nodes

The number of references in Java array is..

a fixed size

Object references - Integer example

a) Declaring reference variables; b) allocating an object; c) allocating another object, with the dereferenced object marked for garbage collection

Object references - Integer example continued

allocating an object; f) assigning null to a reference variable; g) assigning a reference with a null value

How to advance the current position to the next node

curr = curr.next;

How to return a node that is no longer needed to the system?

curr.next = null; curr = null;

What is the code to delete a node that curr referecnes in a doubly linked list?

curr.preceding.next = curr.next; curr.next.preceding = curr.preceding;

How to determine the point of insertion or deletion for a sorted linked list of objects?

for ( prev = null, curr = head; (curr != null) && (newValue.compareTo(curr.item) > 0); prev = curr, curr = curr.next )

How to display all the data items in a linked list?

for (Node curr = head; curr != null; curr = curr.next) { System.out.println(curr.item);

How to delete the first node from a linked list?

head = head.next; Your pointer is at "curr" so when you do head.next - thats really curr.next

Example of assigning head reference to null and a lost node

head = new Node(); // Don't really need to use new here head = null; // since we lose the new Node object here

Recursion has occurred in a linked list if what?

head = null; head.next = null; head.item < head.next.item; and head.next references a sorted linked list.

Circular list traversal code

if(list != null){ Node first = list.next; Node curr = first; do { System.out.println(curr.item); curr = curr.next; } while(curr != first);

A reference-based ADT list has what data fields in the default constuctor?

int numItems; Node head;

How to create a node for the new item?

newNode = new Node (item);

What is the code to insert a new node that newNode reference before the node referenced by curr?

newNode.next = curr; newNode.preceding = curr.preceding; curr.preceding = newNode; newNode.preceding.next = newNode;

How to insert a node between two nodes?

newNode.next = curr; prev.next = newNode;

How to insert a node at the beginning of a linked list?

newNode.next = head; head = newNode;

What is the code for tail referencing?

tail.next = new node (data, null);

Example of displaying the contents of linked list?

to traverse through the linked list you do need additional reference variable which maintains the current position in the traversal.

A local reference variable to a method

•Does not have a default value •Can be datafield of a class.

Options for implementing an ADT - Arrays

•Has a fixed size •Data must be shifted during insertions and deletions

As a data field of class

•Has the default value null

How to insert a node at the end of a linked list?

•Inserting at the end of a linked list is not a special case if curr is null newNode.next = curr; prev.next = newNode;

Options for implementing an ADT - Linked list

•Is able to grow in size as needed •Does not require the shifting of items during insertions and deletions

Disadvantages of using array for ADT implementation

•It has a fixed size so you need to know handle those out of bound exceptions. •Everytime there is insertion/deletion data needs to be shifted(time consuming)


संबंधित स्टडी सेट्स

AP Chemistry 1st Semester Exam Study Guide

View Set

Chapter 21: Gynecologic Emergencies

View Set

Baroque Final Exam Monument List

View Set

Legal Terminology Mid-Term (Ch. 17)

View Set

Intro To African American Studies

View Set

Chapter 1 Module 3: Types of Insurers

View Set