# How do I make this sort (smallest to largest) print then remove even numbers then print again?

0 votes
asked Nov 13, 2018
#include <iostream>

using namespace std;

struct link

{

int value;

link * next;

};

int main()

{

link *top;

link *current, *previous;

link *lptr, *temp, *step, *prev = NULL;

int ivalue, remove_value;

cout << "Input next value, -1 to terminate > " << flush;

cin >> ivalue;

top = 0;

// Build the list

while (ivalue != -1)

{

lptr = new link;

lptr->value = ivalue;

current = top;

previous = 0;

if (!previous)

{

lptr = new link;

lptr->value = ivalue;

lptr->next = top;

top = lptr;

current = top;

}

else

{

lptr = new link;

lptr->value = ivalue;

lptr->next = current;

previous->next = lptr;

current = lptr;

}

cout << "Input next value, -1 to terminate > " << flush;

cin >> ivalue;

}

lptr = top;

while (lptr)

{

cout << lptr->value << endl; lptr = lptr->next;

}

current = top;

previous = 0;

while (current)

{

/*Supply missing code */

}

lptr = top;

while (lptr)

{

cout << lptr->value << endl;

lptr = lptr->next;

}

}

## 1 Answer

0 votes
answered Nov 16, 2018 by (1,230 points)
There is a sort function in the algorithm header file, but I think it only works with STL containers. If you are not using an STL container, you can make a sorting function yourself by starting with one of the items in the list and comparing it to every other item. Through the comparison, you can have the elements reordered from smallest to largest based on their comparison to the first item. When this is done, have the first item automatically placed first in the list. Print the items by indexing or iterating through each item in the list.

To delete the even numbers from the list, you need a separate list, array, vector, or another container to hold some even numbers. You should choose how many even numbers you need based on the numbers in your list. For example, if the biggest number in your list is 10, your even number container should go to 10 (2, 4, 6, 8, 10). If the biggest number is 12, go to 12, and so on. Then, iterate through the container, using if-statements to check if the number you're currently indexing in the even number container matches the one you are currently indexing in your list. If the if-statement eventually does come true (The currently indexed item in the list matches the currently indexed item in the container), write code to delete that item from the list. This iteration process can be achieved through nested for loops or using iterators. Print the items in the list again in the same way stated above.

Hope this helps!