Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Members of a DNode Object are used for Operations - Data Structures II | COMP 385, Study notes of Data Structures and Algorithms

Material Type: Notes; Class: DATA STRUCTURES II; Subject: Computer Science; University: Wentworth Institute of Technology; Term: Fall 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-ftn-1
koofers-user-ftn-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
private static class DNode<T>
{
// the members of a DNode object are used for operations within a
// doubly-linked list
public T nodeValue; // data value of the node
public DNode<T> prev; // previous node in the list
public DNode<T> next; // next node in the list
// default constructor. creates an object with the value set to null
// and whose references point to the node itself
public DNode()
{
nodeValue = null;
next = this; // the next node is the current node
prev = this; // the previous node is the current node
}
// creates object whose value is item and whose references
// point to the node itself
public DNode(T item)
{
nodeValue = item;
next = this; // the next node is the current node
prev = this; // the previous node is the current node
}
}
}
public class LinkedList<T>
implements List<T>, Iterable<T>, Cloneable, java.io.Serializable
{
// number of elements in the list
private int listSize;
// the doubly-linked list header node
transient private DNode<T> header;
// increases whenever the list changes. the class creates
// iterators whose variable expectedModCount equals the current
// value of modCount. for an iterator operation to be valid,
// modCount must equal expectedModCount
transient private int modCount;
/**
* Creates an empty list.
*/
public LinkedList()
{
header = new DNode<T>();
listSize = 0;
modCount = 0;
}
/app/work/qkd2k7-487187-2765053-linkedlistanddnodeportions-doc.doc 1 11/28/2020
pf2

Partial preview of the text

Download Members of a DNode Object are used for Operations - Data Structures II | COMP 385 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

private static class DNode { // the members of a DNode object are used for operations within a // doubly-linked list public T nodeValue; // data value of the node public DNode prev; // previous node in the list public DNode next; // next node in the list // default constructor. creates an object with the value set to null // and whose references point to the node itself public DNode() { nodeValue = null; next = this; // the next node is the current node prev = this; // the previous node is the current node } // creates object whose value is item and whose references // point to the node itself public DNode(T item) { nodeValue = item; next = this; // the next node is the current node prev = this; // the previous node is the current node } } } public class LinkedList implements List, Iterable, Cloneable, java.io.Serializable { // number of elements in the list private int listSize; // the doubly-linked list header node transient private DNode header; // increases whenever the list changes. the class creates // iterators whose variable expectedModCount equals the current // value of modCount. for an iterator operation to be valid, // modCount must equal expectedModCount transient private int modCount; /**

  • Creates an empty list. */ public LinkedList() { header = new DNode(); listSize = 0; modCount = 0; } /app/work/qkd2k7-487187-2765053-linkedlistanddnodeportions-doc.doc 1 11/28/

// remove DNode referenced by curr private void remove(DNode curr) { // return if the list is empty if (curr.next == curr) return; // declare references for the predecessor and successor nodes DNode prevNode = curr.prev, succNode = curr.next; // update reference fields for predecessor and successor prevNode.next = succNode; succNode.prev = prevNode; } private DNode addBefore(DNode curr, T item) { // declare reference variables for new node and previous node DNode newNode, prevNode; // create new DNode with item as initial value newNode = new DNode(item); // assign prevNode the reference value of node before p prevNode = curr.prev; // update reference fields in newNode newNode.prev = prevNode; newNode.next = curr; // update curr and prevNode to point at newNode prevNode.next = newNode; curr.prev = newNode; return newNode; } /app/work/qkd2k7-487187-2765053-linkedlistanddnodeportions-doc.doc 2 11/28/