【问题标题】:Write bool expression to tell if a list is increasing编写 bool 表达式来判断列表是否在增加
【发布时间】:2013-10-21 23:28:36
【问题描述】:

我有下面给出的程序,我能够完成对某个点的分配,除了添加一个菜单选项,如果给定列表增加或减少,则返回真/假 例如,对于包含 head-() (11) (8) (15) (3) 的列表,isIncreasing() 应该返回 false。但是,在处理包含 head- () (7) (9) (15) 的列表时,它会返回 true。

我将如何查看列表并告诉程序比较值并做出决定

赋值告诉我们这个新操作应该有签名:

bool List::isIncreasing() const;

LIST.H

#ifndef LIST_H
#define LIST_H
#include <iostream>
#include "ListNode.h"
#include "ListIterator.h"

namespace cs20 {

template <class Object>
class List {
public:
    List();
    List( const List& rhs );
    ~List();

    bool isEmpty() const;
    void makeEmpty();
    ListIterator<Object> zeroth() const;
    ListIterator<Object> first() const;
    void insert( const Object& data,
                 const ListIterator<Object> &iter );
    void insert( const Object& data );
    ListIterator<Object> findPrevious( const Object& data ) const;
    void remove( const Object& data );

    const List& operator =( const List& rhs );
private:
    ListNode<Object> * head;

};

}
#endif

LIST.CPP

#ifndef LIST_CPP
#define LIST_CPP

#include "List.h"

namespace cs20 {
template <class Object>
List<Object>::List() {
    head = new ListNode<Object>;
}

template <class Object>
List<Object>::List( const List<Object>& rhs ) {
    head = new ListNode<Object>;
    *this = rhs;
}

template <class Object>
List<Object>::~List() {
    makeEmpty();
    delete head;
}

template <class Object>
bool List<Object>::isEmpty() const {
    return( head->nextIsNull() );
}

template <class Object>
void List<Object>::makeEmpty() {
    while (!isEmpty()) {
        remove( first().retrieve() );
    }
}

template <class Object>
ListIterator<Object> List<Object>::zeroth() const {
    return( ListIterator<Object>( head ) );
}

template <class Object>
ListIterator<Object> List<Object>::first() const {
    return( ListIterator<Object>( head->getNext() ) );
}

template <class Object>
void List<Object>::insert( const Object& data,
                           const ListIterator<Object> &iter ) {
    if (iter.isValid()) {
        ListNode<Object>* newnode = new ListNode<Object>( data, iter.current->getNext() );
        iter.current->setNext( newnode );
    }
}

template <class Object>
void List<Object>::insert( const Object& data ) {
    // insert after the header node
    ListNode<Object>* newnode = new ListNode<Object>( data, head->getNext() );
    head->setNext( newnode );
}

template <class Object>
ListIterator<Object> List<Object>::findPrevious( const Object& data ) const {
    ListNode<Object>* node = head;
    while( node->getNext() != NULL && node->getNext()->getElement() != data ) {
        node = node->getNext();
    }
    if (node->getNext() == NULL) {
        node = NULL;
    }
    return ListIterator<Object>( node );
}

template <class Object>
void List<Object>::remove( const Object& data ) {
    ListIterator<Object> iter = findPrevious( data );
    if (iter.isValid()) {
        ListNode<Object>* node = findPrevious( data ).current;
        if (node->getNext() != NULL) {
            ListNode<Object> *oldNode = node->getNext();
            node->setNext( node->getNext()->getNext() );  // Skip oldNode
            delete oldNode;
        }
    }
}

// Deep copy of linked list
template <class Object>
const List<Object>& List<Object>::operator =( const List<Object>& rhs ) {
    if (this != &rhs) {
        makeEmpty();

        ListIterator<Object> rightiter = rhs.first( );
        ListIterator<Object> myiterator = zeroth();
        while( rightiter.isValid() ) {
            insert( rightiter.retrieve(), myiterator );
            rightiter.advance();
            myiterator.advance();
        }
    }
    return( *this );
}

}

#endif

实施 列表菜单.CPP

// Menu.cpp : 定义控制台应用程序的入口点。 //

#include <iostream>
#include "List.h"
#include "ListNode.h"
#include "ListIterator.h"
#include "List.cpp"
#include "ListNode.cpp"
#include "ListIterator.cpp"

using namespace std;
using namespace cs20;

enum CHOICE {MAKEEMPTY, REMOVE, ISEMPTY, FINDPREVIOUS, INSERT, QUIT, PRINT };

CHOICE menu();
void printList( const List<int>& l );

int main(int argc, char* argv[]) {
    int value;
    List<int> list;
    ListIterator<int> iter;

    CHOICE choice;
    do {
        choice = menu();
        switch( choice ) {
        case MAKEEMPTY:
            list.makeEmpty();
            break;
        case ISEMPTY:
            if (list.isEmpty()) {
                cout << "list is empty" << endl;
            }
            else {
                cout << "list is not empty" << endl;
            }
            break;
        case REMOVE:
            cout << "Please provide int to remove: ";
            cin  >> value; 
            list.remove( value );
            break;
        case INSERT:
            cout << "Please provide int to insert: ";
            cin  >> value; 
            list.insert( value );
            break;
        case FINDPREVIOUS:
            cout << "Please provide int to find: ";
            cin  >> value; 
            iter = list.findPrevious( value );
            if (iter.isValid()) {
                cout << "previous element = " << iter.retrieve() << endl;
            }
            else {
                cout << "data element was not found!" << endl;
            }
            break;
        case PRINT:
            printList( list );
            break;
        case QUIT:
            break;
    }   

    } while (choice != QUIT);

    return( 0 );
}

int sample() {
    cout << "Forming Lists" << endl;
    int one = 1, two = 2;
    List<int> l1 = List<int>();
    List<int> l2 = List<int>();

    l1.insert( one );
    l1.insert( two );

    cout << "print l1" << endl;
    printList( l1 );

    cout << "l2 = l1" << endl;
    l2 = l1;

    cout << "print l2" << endl;
    printList( l2 );    

    cout << "l1.remove(one)" << endl;
    l1.remove( one );

    cout << "print l1" << endl;
    printList( l1 );

    cout << "print l2" << endl;
    printList( l2 );
    cout << "findPrevious 1 in l2" << endl;
    ListIterator<int> iter = l2.findPrevious( one );
    if (iter.isValid()) {
        cout << "--iter valid" << endl;
        cout << iter.retrieve() << endl;
    }
    else {
        cout << "--iter not valid" << endl;
    }

    cout << "findPrevious 2 in l2" << endl;
    iter = l2.findPrevious( two );
    if (iter.isValid()) {
        cout << "--iter valid" << endl;
        cout << iter.retrieve() << endl;
    }
    else {
        cout << "--iter not valid" << endl;
    }

    cout << "findPrevious 1 in l1" << endl;
    iter = l1.findPrevious( one );
    if (iter.isValid()) {
        cout << "--iter valid" << endl;
        cout << iter.retrieve() << endl;
    }
    else {
        cout << "--iter not valid" << endl;
    }

    cout << "findPrevious 2 in l1" << endl;
    iter = l1.findPrevious( two );
    if (iter.isValid()) {
        cout << "--iter valid" << endl;
        cout << iter.retrieve() << endl;
    }
    else {
        cout << "--iter not valid" << endl;
    }

    cout << "print l1" << endl;
    printList( l1 );    

        // you can remove whatever you want, whether it exists or not
    cout << "l1.remove(one)" << endl;
    l1.remove( one );

    cout << "print l1" << endl;
    printList( l1 );    

    return( 0 );
}

void printList( const List<int>& l ) {
    if (l.isEmpty())
        cout << "Empty list" << endl;
    else {
        ListIterator<int> iter = l.first();
        while (iter.isValid()) {
            cout << iter.retrieve() << " -> ";
            iter.advance();
        }
        cout << "NULL";
        cout << endl;
    }
}

CHOICE menu() {
    char choice;
    CHOICE result;
    cout << "(M)akeEmpty I(s)Empty (R)emove (I)nsert (F)indPrevious (P)rint (Q)uit: " << endl;
    cin  >> choice;
    switch( choice ) {
    case 'M':
    case 'm':
        result = MAKEEMPTY;
        break;
    case 'S':
    case 's':
        result = ISEMPTY;
        break;
    case 'R':
    case 'r':
        result = REMOVE;
        break;
    case 'I':
    case 'i':
        result = INSERT;
        break;
    case 'F':
    case 'f':
        result = FINDPREVIOUS;
        break;
    case 'Q':
    case 'q':
        result = QUIT;
        break;
    case 'P':
    case 'p':
        result = PRINT;
        break;
    }

    return( result );
}

【问题讨论】:

  • 首先,您不能在与使用它们的位置不同的翻译单元中定义这些模板,并且从用户文件中包含 cpp 文件是非常奇怪的。如果您希望它们在 cpp 中,请将其包含在标题的底部。其次,提供迭代器并使用std::is_sorted
  • 它可以在 O(1) 中完成 - 只需检查插入的项目是否大于或等于前一个项目并且小于或等于下一个项目。删除项目不会破坏列表的排序不变量。
  • @chris 我该如何实现呢?
  • 基本上你需要一个返回迭代器的begin()end() 方法。

标签: c++ list linked-list singly-linked-list


【解决方案1】:

从第一个链接开始:

  1. 记录当前链接的值

  2. 迭代到下一个链接

  3. 测试是否大于(或等于)上一个链接的记录值

  4. 如果是,从 1 开始重复。

  5. 否则返回false

如果您一直通过列表,您就知道列表一直在增加。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2021-03-29
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多