I have to write a code that accepts 5 grades and sorts them from smallest to greatest using linked list.This is what I have so far using examples from the book.

#include <iostream>
using namespace std;

struct List
    int value;
    List *next;

int main()
    int num, num1,num2,num3,num4;

    List *head;

    cout <<"Please enter 5 grades" <<endl;
    cin >>num;
    cin >>num1;
    cin >>num2;
    cin >>num3;
    cin >>num4;

head = new List;
head->value = num1;
second->next = NULL;
head->next = second;

List *second = new List;
second->value = num1;
second ->next = NULL;
head->next = second;

List *third = new List;
third->value = num2;
third->next = NULL;
head->next->next = third;

List *fourth = new List;
fourth->value = num3;
fourth->next = NULL;
head->next->next->next = fourth;

List *fifth = new List;
fifth->value = num4;
fifth->next = NULL:
head->next->next->next->next = fifth;

 //Print the list
 cout << "First grade is: "<<
 cout << "Second grade is: "<<
 cout << "Third grade is: "<<
 cout << "Fourth grade is: "<<
 cout << "Fifth grade is: "<<

Recommended Answers

All 2 Replies

im not sure how I should sort it

Okay. First, please format your code correctly using the

blocks that are given to us. Makes things a little easier to read.

I'm figuring your professor does not want want you to create 5 int variables in the main program and then add them to the list as you have done. Imagine if you had a ton numbers to enter. Yeah, that would be a lot of variables to make and keep them juggled properly to add them to a list. (hint: stick with one int variable).

Along with the previous notion of manually creating variables, adding the values to the linked list by manually typing next->next->next the number of times needed is a bit overwhelming when you lost your place after the 76th ->next, and needing to do it, 22 more times? Or was it 37 more times? Make a generic function to input your code.

To input your code, you can loop using a for-loop, or a while loop, or any method you'd like; but likely one you can count with. With each loop through when you create a new node, you do not need to create a new variable name for each new node, just link them together and keep going.

I'd get the previous part done prior to attempting a sorting function/algorithm. Once you have the input well in hand, the sorting function should look similar to any array sorting algorithm but with different syntax.

Once you have it sorted, you can loop through the first, then the second, then the third (hint: using a for-loop) until you get to the end of the linked list (hint: they end by having the last node linked to a NULL value). Until the end if found, output the current values of the node, increment the node pointer, check if it is NULL and if not output again, and continue...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.