














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of sequences, including ranked sequences, positions, positional sequences, and general sequences. It provides an overview of the ranked sequence adt and its various methods, such as elematrank, replaceelematrank, insertelematrank, and removeelematrank. The document also covers the array-based implementation of the ranked sequence adt, including the time complexity of the methods. Additionally, it explores the implementation of sequences using a doubly linked list, including the java implementation and the comparison of sequence implementations. Finally, the document introduces the bubble sort algorithm and its implementation for an array-based sequence.
Typology: Summaries
1 / 22
This page cannot be seen from the preview
Don't miss anything!















header
Baltimore Paris Providence
trailer
header
Baltimore Paris Providence
trailer
New York
public class NodeRankedSequence extends MyDeque implements Deque, RankedSequence{ public void insertElemAtRank ( int rank, Object element) throws BoundaryViolationException { if (rank != size()) //rank size() is OK for //insertion checkRank(rank); DLNode next = nodeAtRank(rank); // the new node //will be right before this DLNode prev = next.getPrev(); // the new node //will be right after this DLNode node = new DLNode(element, prev, next); next.setPrev(node); prev.setNext(node); size++; } public Object removeElemAtRank (int rank) throws BoundaryViolationException { checkRank(rank); DLNode node = nodeAtRank(rank); // node to //be removed DLNode next = node.getNext(); //node before it
DLNode prev = node.getPrev(); // node after it prev.setNext(next); next.setPrev(prev); size--; return node.getElement(); } private DLNode nodeAtRank (int rank) { // auxiliary method to find the node of the //element with the given rank DLNode node; if (rank <= size()/2) { //scan forward from head node = header.getNext(); for ( int i=0; i < rank; i++) node = node.getNext(); } else { // scan backward from the tail node = trailer.getPrev(); for ( int i=0; i < size()-rank-1 ; i++) node = node.getPrev(); } return node; }
return cont; } public Object element() throws InvalidPositionException { if (cont == null ) throw new InvalidPositionException (“Position has no container!”); return element; } // Accesor methods NSNode getNext() { return next; } NSNode getPrev() { return prev; } void setNext(NSNode newNext) { next = newNext; } // Update methods void setPrev(NSNode newPrev) { prev = newPrev; } void setElement(Object newElement) { element = newElement; } void setContainer(Container newCont) { cont = newCont; } }
throw new InvalidPositionException(“Head of the sequence is not a valid position”);
throw new InvalidPositionException (“Tail of the sequence is not a valid position”); }
public interface PositionalSequence extends PositionalContainer {
/* *********** Accessor Methods *********** */
public Position first() throws EmptyContainerException;
public Position last() throws EmptyContainerException;
throws InvalidPositionException, BoundaryViolationException;
throws InvalidPositionException, BoundaryViolationException;
/* *********** More Update Methods *********** */
throws InvalidPositionException;
throws InvalidPositionException;
throws InvalidPositionException;
throws InvalidPositionException;
throws InvalidPositionException; }