doubly

Upload: ktik11

Post on 14-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Doubly

    1/7

    Doubly-linked List TemplateThis project involves implementing a fairly standard doubly-linked list as a C++ template.

    Because this assignment will be

    auto-graded using a test harness I will provide, your implementation must conform to the

    public interfaces below, and include

    at least the private members that are shown.

    Important: note the proper use of conditional compilation directives in the sample code

    below. The preprocessor for the g++

    compiler contains a serious flaw, and may be hung if circular inclusions arise. Since you have

    no control over how client

    code may use your files, you must prevent circular inclusion problems by using conditional

    compilation directives. Any

    student who fails to do so, and causes a problem, will forfeit all his/her remaining

    submissions for this assignment.

    // DNodeT.h

    //

    #ifndefDNODET_H

    #define DNODET_H

    template class DNodeT {

    public:

    T Element;

    DNodeT* Prev;

    DNodeT* Next;DNodeT();

    DNodeT(const T& Elem, DNodeT* P = NULL, DNodeT* N = NULL);

    };

    // . . . implementation follows in this file

    // DListT.h

    //

    #ifndefDLISTT_H

    #define DLISTT_H

    #include

    #include

    #include

    #include

    using namespace std;

    #include "DNodeT.h"

    class IllegalAccess : public exception {}; // thrown by iterator objects

    template class DListT {

    friend class Javert; // diagnostic test class

    private:

  • 7/30/2019 Doubly

    2/7

    DNodeT* Head; // handle on first data node (if any)

    DNodeT* Tail; // handle on last data node (if any)

    DNodeT* Fore; // handle on leading sentinel node

    DNodeT* Aft; // handle on trailing sentinel node

    public:DListT(); // make empty list

    DListT(const DListT& Source); // deep copy support

    DListT& operator=(const DListT& Source);

    ~DListT(); // deallocate all nodes

    bool isEmpty() const; // true iff list has no nodes

    void Clear(); // restore to empty state

    void Display(ostream& Out) const; // display formatted contents

    // . . . implementation continues in next display . . .

    CS 2604 Minor Project 1 Spring 2005

    1/27/2005 10:37 AM 2

    ////////////////////////////////////////// iterator class

    class iterator {

    private:

    DNodeT* Position;

    iterator(DNodeT* P) {

    Position = P;

    }public:

    iterator() { Position = NULL; }

    iterator operator++() {

    // step to next data node (if any) or to trailing sentinel

    }

    iterator operator++(int Dummy) {

    // step to next data node (if any) or to trailing sentinel

    }

    iterator operator--() {// step to previous data node (if any) or to leading sentinel

    }

    iterator operator--(int Dummy) {

    // step to previous data node (if any) or to leading sentinel

    }

    bool operator==(const iterator& RHS) const {

    // iterators are equal iff they have the same target

    }

    bool operator!=(const iterator& RHS) const {

  • 7/30/2019 Doubly

    3/7

    // complement of operator==()

    }

    T& operator*() throw (range_error) {

    // return reference to data in iterator's target,

    // throw exception if no target or target not a data node}

    friend class DListT; // to use private constructor

    friend class Javert; // give test harness access

    friend class const_iterator; // for conversions

    }; // end iterator

    ////////////////////////////////////////// const_iterator class

    class const_iterator {

    private:

    DNodeT* Position;

    const_iterator(DNodeT* P) {

    Position = P;

    }

    public:

    const_iterator() { Position = NULL; }

    const_iterator operator++() {

    // step to next data node (if any) or to trailing sentinel

    }const_iterator operator++(int Dummy) {

    // step to next data node (if any) or to trailing sentinel

    }

    const_iterator operator--() {

    // step to previous data node (if any) or to leading sentinel

    }

    const_iterator operator--(int Dummy) {

    // step to previous data node (if any) or to leading sentinel

    }// . . . implementation continues in next display . . .

    CS 2604 Minor Project 1 Spring 2005

    1/27/2005 10:37 AM 3

    bool operator==(const const_iterator& RHS) const {

    // iterators are equal iff they have the same target

    }

    bool operator!=(const const_iterator& RHS) const {

    // complement of operator==()

    }

  • 7/30/2019 Doubly

    4/7

    const T& operator*() const throw (range_error) {

    // return constant reference to data in iterator's target,

    // throw exception if no target or target not a data node

    }

    const_iterator(const iterator& It) {// create constant iterator from regular iterator

    }

    const_iterator& operator=(const iterator& It) {

    // assign regular iterator to constant iterator

    }

    friend class DListT; // to use private constructor

    friend class Javert; // give test harness access

    }; // end const_iterator

    iterator begin(); // return iterator to head node, if any

    // otherwise return end()

    iterator rbegin(); // return iterator to tail node

    iterator end(); // return iterator to trailing sentinel

    iterator rend(); // return iterator to leading sentinel

    const_iterator begin() const; // analogous to previous fns

    const_iterator rbegin() const;

    const_iterator end() const;

    const_iterator rend() const;iterator Insert(iterator It, const T& Elem); // insert before target of

    // iterator, if any;

    // if iterator is end(),

    // append value to list;

    // if iterator target is not

    // a data node or the

    // trailing sentinel,

    // behavior is undefined

    iterator Find(const T& Elem); // return iterator to matching databool Delete(iterator It) throw (range_error); // delete target of iterator,

    // if it's a data node;

    // otherwise throw exception

    };

    // . . . non-inline implementation follows in this fileYou may safely add features to the given interface, but if you omit or modify members of the

    given interface you will almost

    certainly face compilation errors when you submit your implementation for testing. You must

    place the declaration of your

  • 7/30/2019 Doubly

    5/7

    linked list template in a header file named DListT.h because Javert.h (one of the test harness

    files) will explicitly

    #include that file.

    Be sure to read the embedded comments carefully. Most of them state operational

    requirements for the various operations,

    and the test harness will be implemented assuming that those requirements are met by yourimplementation.

    CS 2604 Minor Project 1 Spring 2005

    1/27/2005 10:37 AM 4

    Design and implementation requirements

    There are some explicit requirements, in addition to those on the Coding Stylepage of the

    course website:

    You must implement C++ templates for the linked list itself and also for the list nodes,

    conforming to the given

    interfaces.

    You must handle copy issues correctly. We will certainly test for this. Failures in your

    copy logic will almostcertainly result in program crashes and scores of zero.

    Your implementation must conform to the comments given in the class declarations.

    Your list implementation must use data-free sentinel nodes, as described in the course

    notes.

    operator*() for the iterator classes must throw a range_error object if invoked on an iterator

    that does not

    target a data-holding node.

    You may assume that any data type stored in your template will handle its own copy issues

    correctly (as it must).

    You may assume that any data type stored in your template will provide operator

  • 7/30/2019 Doubly

    6/7

    reliably under Windows, execute the following command in a shell in the directory

    containing your source files:

    cl /nologo /GX /o *.cpp

    This produces an executable containing essentially no debugging code, and is much more

    likely to exhibit bad behavior if

    your implementation contains bugs.Note that you may have to first execute the batch file vcvars32.bat that is included in the

    Visual C++ .NET installation.

    Evaluation:

    You should document your implementation in accordance with the Code Style Page on the

    course website. It is possible that

    your implementation will be evaluated for documentation and design, as well as for

    correctness of results. If so, your

    submission that achieved the highest score will be evaluated by a TA, who will assess a

    deduction (ideally zero) against your

    score from the Curator.

    Note well: if you make two or more submissions that are tied for the highest score, theearliest of those will be graded.

    There will be absolutely no exceptions to this policy!

    Moral: code and document to meet requirements from the beginning, rather than planning to

    retrofit documentation into a

    finished program.

    CS 2604 Minor Project 1 Spring 2005

    1/27/2005 10:37 AM 5

    Note that the evaluation of your project may depend substantially on the quality of your code

    and documentation.

    What to turn in and how:

    This assignment will be auto-graded using a test harness on the Curator system. The testing

    will be done under Mandrake

    Linux 10.0 using g++ 3.3.2. The Curator will not begin accepting submissions before January

    24.

    Submit a single gzip'd tar file containing the C++ header files for your template declarations

    and implementations to the

    Curator System. Submit only the template declarations and implementations. Submit nothing

    else. Be sure that your header

    file only contains include directives for Standard C++ header files; any other include

    directives will certainly result in

    compilation errors. The file you submit will be unpacked using the following command:gzip d c | tarx

    The unpacked files will then be compiled with the test harness using the following command:

    g++ o *.cpp

    Instructions, and the appropriate link, for submitting to the Curator are given in the Student

    Guide at the Curator website:

    http://www.cs.vt.edu/curator/.

    Since most of you have not used the Curator system in a previous course, it is imperative that

    you read the description of how

    the Curator will score your submission. Note that the Curator system is entirely separate (and

    different) from the web-CAT

    system you may have used earlier.

  • 7/30/2019 Doubly

    7/7

    You will be allowed to submit your solution up to ten times. Your highest score will be

    counted.

    Pledge:

    Each of your program submissions must be pledged to conform to the Honor Code

    requirements for this course. Specifically,

    you must include the pledge statement provided on the Programming Standards page in oneof your submitted files.