sorting list

1)add function to sort the list in ascending order of values.
2)Use the same print function to now print the values in ascending order.

this is where im stuck can someone help me out

problem:
when i input the list of numbers then i input 0 after that i have to add a number to the list and sort it

right now i can input number then i can input 0 but the program wont sort them in ascending order

#include <iostream>
#include <stdlib.h>

using namespace std;

struct nodeType
{
int num;
int info;
nodeType *first, *last, *newNode, *link, *next, *temp, *temp2, *current;
};
void addTailInt(nodeType *first, int num);
void printListOne (nodeType *first,int num);
void sort(nodeType *first, int num);

int main(int argc)
{
int num;
nodeType *first, *last, *newNode;


cout<<"Enter a list of integers, with the last integer entered being zero."<<endl;
cin>>num;
first = NULL;

while (num != 0)
{
newNode = new nodeType;
newNode->info = num;
newNode->link = NULL;

if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link=newNode;
last=newNode;
}
cin>>num;
}

cout<<endl;
addTailInt (first,num);
printListOne (first, num);
sort (first, num);



}

void addTailInt (nodeType *first, int num)
{
nodeType *temp, *temp2;
temp = new nodeType;
cout<<"Please enter integer to be added onto the end of the list: "<<endl;
cin>>temp->num;
temp->next=NULL;
if (first==NULL)
{
first = temp;
}
else
{
temp2 = temp;
while (temp2->next != NULL)
temp2 = temp2->next;
temp2->next = temp;
}
cout<<endl;
}
void printListOne (nodeType *first,int num)
{
nodeType *current;
current = first; //points to first node
cout<<"List of numbers: ";
while (current!=NULL) //loop to print numbers after the first node
{

cout<<current->info<<", ";
current=current->link;
}
}
void sort(nodeType *first, int num)
{
cout<<endl;
nodeType *temp1; // creates a temporary node
temp1 = (nodeType*)malloc(sizeof(nodeType)); // allocates space for node

nodeType *temp2; // creates a temporary node
temp2 = (nodeType*)malloc(sizeof(nodeType)); // allocates space for node

int temp = 0; // stores temporary data value

for( temp1 = first ; temp1!=NULL ; temp1 = temp1->next ) // sort function
{
for( temp2 = temp1->next ; temp2!=NULL ; temp2 = temp2->next )
{
if( temp1->num > temp2->num)
{
temp = temp1->num;
temp1->num = temp2->num;
temp2->num = temp;
}
}
}

}
Last edited on
closed account (D80DSL3A)
this is where im stuck

Where are you stuck? What is not working right?
Please describe the problem(s) in more detail.
Few will examine the code to figure it out with nothing else to go on.
Topic archived. No new replies allowed.