In: Computer Science
C++
You are to input the following numbers from a file:
75
80
90
95
100
100
70
30
20
70
Place them into a linked list (Like the one I uploaded on this homework). The file I uploaded shows how to sort the numbers using bubble sort. You are to sort them based on selection sort!
Only sort the values, do not modify the pointers. Use the file I uploaded as a good guide!
#include <iostream> #include <fstream> using namespace std; struct ListNode { double value; ListNode *next; ListNode(double value1, ListNode *next1 = NULL) { value = value1; next = next1; } }; int main() { ifstream inFile; inFile.open("Grades.txt"); ListNode *head= NULL; double value; for (int x=0; x<10; x++) { inFile>>value; head = new ListNode(value,head); } ListNode *ptr1; double temp; for (int x=0; x<10; x++) { ptr1=head; for (int y=0; y<10-1-x; y++) { if (ptr1->value > ptr1->next->value) { temp = ptr1->value; ptr1->value = ptr1->next->value; ptr1->next->value = temp; } ptr1=ptr1->next; } } ListNode *ptr = head; for (int x=0; x<10; x++) { cout<<ptr->value<<endl; ptr=ptr->next; } return 0; }
#include <iostream>
#include <iostream>
#include <fstream>
using namespace std;
struct ListNode
{
double value;
ListNode *next;
ListNode(double value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
int main()
{
ifstream inFile;
inFile.open("Grades.txt");
ListNode *head= NULL;
double value;
for (int x=0; x<10; x++)
{
inFile>>value;
head = new ListNode(value,head);
}
ListNode *ptr1;
double temp;
ListNode * curr = head;
while (curr!= NULL) {
ListNode* min = curr;
ListNode* r = curr->next;
while (r != NULL) {
if (min->value > r->value)
min = r;
r = r->next;
}
int x = curr->value;
curr->value = min->value;
min->value = x;
curr = curr->next;
}
ListNode *ptr = head;
for (int x=0; x<10; x++)
{
cout<<ptr->value<<endl;
ptr=ptr->next;
}
return 0;
}
===========================================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern. PLEASE
UPVOTE