Sep 5, 2015 at 8:29pm UTC
Can any pros/senpai in c++ notify me why I could not call the display function to show the random numbers and sorted array? I squeeze out my brain juice yet still I could not figure it out why. Really need helps badly
Assignment01.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
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
class mergenode{
public :
struct node
{
int data;
struct node* next;
};
typedef struct node* nodePtr;
nodePtr curr;
nodePtr temp;
nodePtr head;
nodePtr Node;
mergenode();
void addNode(int data);
bool Empty();
struct node* SortedMerge(node* a, node* b);
void display(struct node* counter);;
void MergeSort(struct node** Head);
private :
int calculateSize();
void Split(node* givennumber, node** Front, node** Back);
};
mergenode::mergenode(){
curr= NULL;
head=NULL;
temp=NULL;
Node=NULL;
}
bool mergenode:: Empty() {
return head == NULL;
}
void mergenode:: addNode(int data ){
nodePtr n = new node;
n->next = NULL;
n->data = data;
if (!Empty()){
curr = head;
while (curr->next !=NULL){
curr = curr->next;
}
curr->next = n;
}
else {
head = n;
}
}
int mergenode:: calculateSize()
{
int count = 0;
Node= head;
while (Node!=NULL){
count++;
Node = Node->next;
}
return count;
}
void mergenode::display(struct node* counter){
while (counter != NULL)
{
cout<<counter->data<<" " ;
counter = counter-> next;
}
cout<<endl;
}
/* sorts the linked list by changing next pointers (not data) */
void mergenode::MergeSort(struct node** Head)
{
node* head = *Head;
node* a;
node* b;
if ((head == NULL) || (head->next == NULL))
{
return ;
}
Split(head, &a, &b);
MergeSort(&a);
MergeSort(&b);
*Head = SortedMerge(a, b);
}
/* merge the sorted linked lists */
struct mergenode:: node*mergenode:: SortedMerge(struct node* a, struct node* b)
{
node* result = NULL;
if (a == NULL)
return b;
else if (b==NULL)
return a;
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
display(a);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
display(b);
}
return result;
}
/* Split the nodes of the given list into front and back halves*/
void mergenode::Split(node* givennumber, node** Front, node** Back)
{
node* fast;
node* slow;
if (givennumber==NULL || givennumber->next==NULL)
{
*Front = givennumber;
*Back = NULL;
}
else
{
slow = givennumber;
fast = givennumber->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
*Front = givennumber;
*Back = slow->next;
slow->next = NULL;
display(*Front);
display(*Back);
}
}
Last edited on Sep 5, 2015 at 8:33pm UTC
Sep 5, 2015 at 8:29pm UTC
versiongui.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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "Assignment01.cpp"
using namespace std;
int main()
{
mergenode mdata;
selectionNode theData;
srand(time(NULL));
InsertionNode getdata;
char choicesort;
cout << "Select TypeOfSort" << endl;
cout << "(a) Bubble Sort" << endl;
cout << "(b) Selection Sort" << endl;
cout << "(c) Insertion Sort" << endl;
cout << "(d) Merge Sort" << endl;
cin >> choicesort;
cout << endl;
switch (choicesort)
{
//bubble sort
case 'a' :
case 'A' :
{
char choicebubble;
cout << "Select number::" << endl;
cout << "(a) 20" << endl;
cout << "(b) 200" << endl;
cout << "(c) 2000" << endl;
cout << "Choice: " ;
cin >> choicebubble;
cout << endl;
switch (choicebubble)
{
case 'a' : //bubble sort20
case 'A' : //bubble sort20
{
bubbleNode node;
bubbleNode();
node.add20item();
cout << "Random number :: " ;
node.display20();
node.bubblesort20();
cout << "Answer :: " ;
node.display20();
system("pause" );
return 0;
}
break ;
case 'b' : //bubble sort200
case 'B' : //bubble sort200
{
bubbleNode node;
bubbleNode();
node.add200item();
cout << "Random number :: " ;
node.display200();
node.bubblesort200();
cout << "\nAnswer :: " ;
node.display200();
system("pause" );
return 0;
}
break ;
case 'c' : //bubble sort2000
case 'C' : //bubble sort2000
{
bubbleNode node;
bubbleNode();
node.add2000item();
cout << "Random number :: " ;
node.display2000();
node.bubblesort2000();
cout << "\nAnswer :: " ;
node.display2000();
system("pause" );
return 0;
}
break ;
default :
cout << "Invalid choice" ;
}
} // end of bubble sort
case 'b' :
case 'B' :
{
char chNum;
cout << "Select number::" << endl;
cout << "(a) 20" << endl;
cout << "(b) 200" << endl;
cout << "(c) 2000" << endl;
cout << "Choice: " ;
cin >> chNum;
cout << endl;
switch (chNum)
{
case 'a' : //SS sort20
case 'A' : //SS sort20
{
for (int i=0 ; i<20 ; i++){
int random= rand() % 99;
theData.addNode(random);
}
cout << "Random number : " ;
theData.display();
theData.selectionSort();
cout<<"Sorted : " ;
theData.display();
system("pause" );
return 0;
}
break ;
case 'b' : //SS 200
case 'B' : //SS 200
{
for (int i=0 ; i<200 ; i++){
int random= rand() % 999;
theData.addNode(random);
}
cout << "Random number : " ;
theData.display();
theData.selectionSortV2();
cout<<"Sorted : " ;
theData.display();
system("pause" );
return 0;
}
break ;
case 'c' : //SS 2000
case 'C' : //SS 2000
{
for (int i=0 ; i<2000 ; i++){
int random= rand() % 9999;
theData.addNode(random);
}
cout << "Random number : " ;
theData.display();
theData.selectionSortV2();
cout<<"Sorted : " ;
theData.display();
system("pause" );
return 0;
}
break ;
}
//ADD case B/C/D for another sort
}// end of choice sort
case 'c' :
case 'C' :
{
char choiceinsertion;
cout << "Select number::" << endl;
cout << "(a) 20" << endl;
cout << "(b) 200" << endl;
cout << "(c) 2000" << endl;
cout << "Choice: " ;
cin >> choiceinsertion;
cout << endl;
switch (choiceinsertion)
{
case 'a' : //SS sort20
case 'A' : //SS sort20
{
for (int i=0 ; i<20 ; i++){
int random= rand() % 99;
getdata.addNode(random);
}
cout << "Random number : " ;
getdata.display();
getdata.InsertionSort();
cout<<"Sorted : " ;
getdata.display();
system("pause" );
return 0;
}
break ;
case 'b' : //SS 200
case 'B' : //SS 200
{
for (int i=0 ; i<200 ; i++){
int random= rand() % 999;
getdata.addNode(random);
}
cout << "Random number : " ;
getdata.display();
getdata.InsertionSortX();
cout<<"Sorted : " ;
getdata.display();
system("pause" );
return 0;
}
break ;
case 'c' : //SS 2000
case 'C' : //SS 2000
{
for (int i=0 ; i<2000 ; i++){
int random= rand() % 9999;
getdata.addNode(random);
}
cout << "Random number : " ;
getdata.display();
getdata.InsertionSortX();
cout<<"Sorted : " ;
getdata.display();
system("pause" );
return 0;
}
break ;
}
}
case 'd' :
case 'D' :
{
char choicemerge;
cout << "Select number::" << endl;
cout << "(a) 20" << endl;
cout << "(b) 200" << endl;
cout << "(c) 2000" << endl;
cout << "Choice: " ;
cin >> choicemerge;
cout << endl;
switch (choicemerge)
{
case 'a' : //SS sort20
case 'A' : //SS sort20
{
for (int i=0 ; i<20 ; i++){
int random= rand() % 99;
mdata.addNode(random);
}
cout << "Random number : " ;
mdata.display();
mdata.MergeSort();
cout<<"Sorted : " ;
mdata.display();
system("pause" );
return 0;
}
break ;
case 'b' : //SS 200
case 'B' : //SS 200
{
for (int i=0 ; i<200 ; i++){
int random= rand() % 999;
mdata.addNode(random);
}
cout << "Random number : " ;
mdata.display();
mdata.MergeSort();
cout<<"Sorted : " ;
mdata.display();
system("pause" );
return 0;
}
break ;
case 'c' : //SS 2000
case 'C' : //SS 2000
{
for (int i=0 ; i<2000 ; i++){
int random= rand() % 9999;
mdata.addNode(random);
}
cout << "Random number : " ;
mdata.display();
mdata.MergeSort();
cout<<"Sorted : " ;
mdata.display();
system("pause" );
return 0;
}
break ;
}
}
}
}
Error at line 256, 257, 259, 273, 274, 276, 290, 291 and 293
P.S: ignore the selection, insertion and bubble sort.
Last edited on Sep 5, 2015 at 8:35pm UTC
Sep 5, 2015 at 10:12pm UTC
Last edited on Sep 5, 2015 at 10:13pm UTC
Sep 6, 2015 at 1:48am UTC
> Error at line 256, 257, 259, 273, 274, 276, 290, 291 and 293
Unable to call the functions
> ignore the selection, insertion and bubble sort.
Cause this is the combination of my friends and my works, they did their selection, bubble and insertion sort. So it should be inside
> #include "Assignment01.cpp"
What's the problem of it?
Sep 6, 2015 at 6:27pm UTC
okay i changed it to another
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node* next;
};
typedef struct node* nodePtr;
nodePtr curr;
nodePtr temp;
nodePtr head;
nodePtr Node;
nodePtr counter;
void addNode(int data);
bool Empty();
struct node* SortedMerge(node* a, node* b);
void display_m(struct node* counter);
void MergeSort(struct node** Head);
void Split(node* givennumber, node** Front, node** Back);
bool Empty() {
return head == NULL;
}
void addNode(int data ){
nodePtr n = new node;
n->next = NULL;
n->data = data;
if (!Empty()){
curr = head;
while (curr->next !=NULL){
curr = curr->next;
}
curr->next = n;
}
else {
head = n;
}
}
void display(){
counter = head;
while (counter != NULL)
{
cout<<counter->data<<" " ;
counter = counter-> next;
}
cout<<endl;
}
void display_m(struct node* counter){
while (counter != NULL)
{
cout<<counter->data<<" " ;
counter = counter-> next;
}
cout<<endl;
}
void MergeSort(struct node** Head)
{
node* head = *Head;
node* a;
node* b;
if ((head == NULL) || (head->next == NULL))
{
return ;
}
Split(head, &a, &b);
MergeSort(&a);
MergeSort(&b);
*Head = SortedMerge(a, b);
}
void MergeSort_2(struct node** Head)
{
node* head = *Head;
node* a;
node* b;
if ((head == NULL) || (head->next == NULL))
{
return ;
}
Split(head, &a, &b);
MergeSort_2(&a);
MergeSort_2(&b);
*Head = SortedMerge(a, b);
}
struct node* SortedMerge(struct node* a, struct node* b)
{
node* result = NULL;
if (a == NULL)
return b;
else if (b==NULL)
return a;
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
display_m(a);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
display_m(b);
}
return result;
}
void Split(node* givennumber, node** Front, node** Back)
{
node* fast;
node* slow;
if (givennumber==NULL || givennumber->next==NULL)
{
*Front = givennumber;
*Back = NULL;
}
else
{
slow = givennumber;
fast = givennumber->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
*Front = givennumber;
*Back = slow->next;
slow->next = NULL;
display_m(*Front);
display_m(*Back);
}
}
int main()
{
int i;
char choice;
cout << "Merge Sort Selection: " << endl;
cout << "[1] 20 random numbers" << endl;
cout << "[2] 200 random numbers" << endl;
cout << "[3] 2000 random numbers" << endl;
cin >> choice;
cout << endl;
switch (choice){
case '1' :
{
for (int i=0 ; i <20 ; i++){
int random = rand() % 99 ;
addNode(random);
}
cout << "20 Random Numbers: " ;
display();
cout<<"Sorting data: \n" ;
MergeSort(&head);
cout<< "\nResult: \n" ;
display_m(head);
system ("pause" );
return 0;
}
break ;
case '2' :
{
for (int i=0 ; i <200 ; i++){
int random = rand() % 99 ;
addNode(random);
}
cout << "200 Random Numbers: " ;
display();
cout<<"Sorting data: \n" ;
MergeSort(&head);
cout<< "\nResult: \n" ;
display_m(head);
system ("pause" );
return 0;
}
break ;
case '3' :
{
for (int i=0 ; i <2000 ; i++){
int random = rand() % 99 ;
addNode(random);
}
cout << "2000 Random Numbers: " ;
display();
cout<<"Sorting data: \n" ;
MergeSort(&head);
cout<< "\nResult: \n" ;
display_m(head);
system ("pause" );
return 0;
}
break ;
}
}
what if I wanted to list the result of Choice 2 and 3 without so much of steps that take years to show out the result?
Is there any problem in my void MergeSort?
Last edited on Sep 6, 2015 at 6:35pm UTC
Sep 6, 2015 at 9:43pm UTC
merge sort is O(n lg n) if the merge part can be done in O(n)
The `display_m()' in your `SortedMerge()' are making it O(n^2), as you are printing a whole list after resolving one conflict.
I don't understand why you print you much, just don't.
Also, your code is hideous. You may use a `list' class, and avoid all those globals.
Your `addNode()' is O(n) when it could easily be O(1)