Write bool expression to tell if a list is increasing

I have the program below given and I was able to complete the assignment to a certain point except in adding a menu option that gives a true/false return if the given list is increasing or decreasing
For example, for a list containing head-() (11) (8) (15) (3), isIncreasing() should return false. However, it would return true when working on a list containing head- () (7) (9) (15).

How would I look at the list and tell the program to compare the values and make this determination

The assignment tells us that this new operation should have the signature:

bool List<Object>::isIncreasing() const;

LIST.CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    #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 

IMPLEMENTATION
LISTMENU.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Menu.cpp : Defines the entry point for the console application.
//

    #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 );
    }
closed account (o3hC5Di1)
Hi there,

You should iterate your list, keeping a boolean variable.
Just a little mock up example, treating your list as an array:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool list::is_increasing()
{
    bool is_increasing = true;

    for (int i=0; i<list.size(); ++i)
    {
        if (i+1 < list.size()) //make sure we don't access out of bounds
        {
            is_increasing = (list[i] < list[i+1]);

            if (is_increasing == false)
                return false;
        }
    }

    return is_increasing;
}


Hope that makes sense, please do let us know if you have any further questions.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.