Mar 27, 2017 at 1:16pm Mar 27, 2017 at 1:16pm UTC
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
#include <iostream>
#include <list>
#include <algorithm>
void sortList(std::list<int > &list);
void showList(const std::list<int > &);
int main(int argc, const char * argv[]) {
std::list<int > randomNumbers;
//fill list with random integers and display
srand(static_cast <unsigned int >(time(NULL)));
for (int i = 0; i < 20; i++){
randomNumbers.push_back(1 + rand() % 50);
}
//display unsorted list
std::cout << "Unsorted list:" << std::endl;
showList(randomNumbers);
// SORT LIST
sortList(randomNumbers);
// DISPLAY SORTED LIST
std::cout << "Sorted list:" << std::endl;
showList(randomNumbers);
return 0;
}
void showList(const std::list<int > &aList){
for (auto it: aList){
std::cout << it << ' ' ;
}
std::cout << '\n' ;
}
void sortList(std::list<int > &aList){
aList.sort();
}
Unsorted list:
20 16 42 40 41 38 26 40 27 12 36 15 44 42 28 13 32 16 49 15
Sorted list:
12 13 15 15 16 16 20 26 27 28 32 36 38 40 40 41 42 42 44 49
Program ended with exit code: 0
http://en.cppreference.com/w/cpp/container/list/sort
Last edited on Mar 27, 2017 at 1:23pm Mar 27, 2017 at 1:23pm UTC
Mar 27, 2017 at 3:51pm Mar 27, 2017 at 3:51pm UTC
Excellent! Thanks very much everyone for your help, not sure why I couldn't get i to work but it does now - thanks again!
Mar 27, 2017 at 4:40pm Mar 27, 2017 at 4:40pm UTC
just a few more things please - I'm trying to pehaps be a bit ambitious and over reach... Ive added some insert and erase functions, but they don't seem to have the desired effect!! It sjust something I want to try and get working
Thanks in advance
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
#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>
void sortList(std::list<int > &list);
void editList(std::list<int > &list);
void showList(std::list<int > &alist);
int main(int argc, const char * argv[]) {
std::list<int > randomNumbers;
srand(static_cast <unsigned int >(time(NULL)));
int sentinel = 0;
//fill list with random integers and display
for (int i = 0; i < 5; i++){
randomNumbers.push_back(1 + rand() % 50);
}
//display unsorted list
std::cout << "Unsorted list:" << std::endl;
for (std::list<int >::iterator it = randomNumbers.begin(); it != randomNumbers.end(); it++){
std::cout << *it << " " ;
}
do {
std::cout << "\n1 to sort list" << std::endl;
std::cout << "2 to display list" << std::endl;
std::cout << "3 to edit list" << std::endl;
std::cout << "-1 to end. Selection: " ;
std::cin >> sentinel;
while ( (!std::cin) || (sentinel < -1 || sentinel > 3 )){
std::cin.clear();
std::cin.ignore();
std::cout << "-1 to end. Please select: " ;
std::cin >> sentinel;
}
switch (sentinel) {
case 1:
std::cout << "case 1" << std::endl;
sortList(randomNumbers);
break ;
case 2:
std::cout << "case 2" << std::endl;
showList(randomNumbers);
break ;
case 3:
std::cout << "case 3" << std::endl;
editList(randomNumbers);
break ;
default :
std::cout << "Not a choice!" << std::endl;
break ;
}
}while (sentinel != -1);
return 0;
}
void sortList(std::list<int > &alist){
alist.sort();
}
void showList(std::list<int > &list){
std::cout << "\nThe list:" << std::endl;
for ( auto it : list){
std::cout << it << " " ;
}
std::cout << std::endl;
}
void editList(std::list<int > &aList){
int sentinel = 0;
std::cout << "\n1 to push_front" << std::endl;
std::cout << "2 to push_back" << std::endl;
std::cout << "3 to insert" << std::endl;
std::cout << "4 to erase" << std::endl;
std::cout << "-1 to end. Selection: " ;
std::cin >> sentinel;
while ( (!std::cin) || (sentinel < -1 || sentinel > 4 )){
std::cin.clear();
std::cin.ignore();
std::cout << "-1 to end. Please select: " ;
std::cin >> sentinel;
}
switch (sentinel) {
case 1:
std::cout << "Input number to push_front: " << std::endl;
int number;
std::cin >> number;
aList.push_front(number);
break ;
case 2:
std::cout << "Input number to push_back: " << std::endl;
std::cin >> number;
aList.push_back(number);
break ;
case 3:
std::cout << "Input position and number to insert: " << std::endl;
std::cout << "Postion: " ;
int position;
std::cin >> position;
std::cout << "Number: " ;
std::cin >> number;
for ( std::list<int >::iterator it = aList.begin(); it != aList.end(); it++){
if (*it == position){
aList.insert(it, number);
}
}
break ;
case 4:
std::cout << "Input position to erase: " << std::endl;
std::cout << "Postion: " ;
std::cin >> position;
for ( std::list<int >::iterator it = aList.begin(); it != aList.end();){
if (*it == position){
it = aList.erase(it);
}
else {
it++;
}
}
break ;
default :
std::cout << "Not a choice!" << std::endl;
break ;
}
}
Last edited on Mar 27, 2017 at 4:42pm Mar 27, 2017 at 4:42pm UTC
Mar 27, 2017 at 5:52pm Mar 27, 2017 at 5:52pm UTC
Sorry - I've been having a bad day. After a long hard look at what I was doing I have realised that my code is ok - just me that is wrong.
As a rule of thumb - I'm not usually that stupid 😂
Thanks again for all of your help.
Last edited on Mar 27, 2017 at 6:13pm Mar 27, 2017 at 6:13pm UTC