I can't get the code to display in an ordered list.
// Chapter 5 - Homework Assignment
#include <iostream>
#include <cstdlib>
using namespace std;
//
// DECLARE FUNCTION HEADER FOR
// CLASS ListNode
class ListNode
{
public:
ListNode(double v, ListNode *p)
{
value = v;
next = p;
}
private:
double value;
ListNode *next;
friend class LinkedList;
};
//
// DECLARE FUNCTION HEADER FOR
// CLASS LinkedList
class LinkedList
{
public:
void add(double x);
bool isMember(double x);
LinkedList( ) { head = NULL;}
void rPrint(){ rPrint(head);}
~LinkedList();
void insert(double x, int pos);
void sort();
private:
ListNode * head;
static void rPrint(ListNode *pList);
static ListNode * insert(ListNode *item, ListNode *sortedList);
};
//****************************************************************
// *
// The function sorts by successively removing the head of the *
// remaining list and inserting the item into a sorted list. *
// The sorted list starts out empty. *
//****************************************************************
//
// FOR THE LINKED LIST TO USE THE sort() FUNCTION
void LinkedList::sort()
{
ListNode *sortedList = NULL;
//
// USE A while LOOP TO CONTINUE LOOPING
// AS LONG AS HEAD IS NOT EQUAL TO NULL
while(head != NULL)
{
ListNode *item = head;
//
// TO MOVE POINTER TO NEXT
item -> next = head;
item-> next = sortedList;
sortedList = insert(item, sortedList);
}
head = sortedList;
}
//***********************************************
// insert(item, sortedList) *
// Inserts the node item into an a sorted *
// list and returns the resulting sorted list. *
//***********************************************
ListNode *LinkedList::insert(ListNode *item , ListNode *sortedList)
{
// If sortedList is empty or first member of the sorted list
// is greater or equal to item then item becomes the first
// member of the augmented list
if (sortedList == NULL || sortedList->value >= item->value)
{
item->next = sortedList;
return item;
}
// Use recursion to insert the item at the right place in the tail
sortedList->next = insert(item, sortedList->next);
return sortedList;
}
//**********************************************************
// LinkedList::insert *
// Insert a given value at a specified position. *
//**********************************************************
void LinkedList::insert(double x, int pos)
{
if (pos == 0 || head == NULL)
{
// ADD VALUE TO THE BEGINNING OF THE LIST
head = new ListNode(x, head);
return;
}
ListNode *p = head;
int numberToSkip = 1;
while (numberToSkip <= pos)
{
if (p->next == NULL || numberToSkip == pos)
{
p->next = new ListNode(x, p->next);
return;
}
//
// TO MOVE THE POINTER TO THE NEXT NODE
p = p->next;
numberToSkip++;
}
}
//
// TO CREATE THE LinkedList DESTRUCTOR
LinkedList::~LinkedList()
{
ListNode *p = head;
ListNode *q; // Next pointer
while (p != NULL)
{
q = p->next;
delete p;
p = q;
}
}
//******************************************************
// Recursive print function *
// Prints all elements on a list passed as parameter. *
//******************************************************
void LinkedList::rPrint(ListNode *pList)
{
if (pList == NULL) return;
else
{
cout << pList->value << " ";
rPrint(pList->next);
}
}
//**********************************
// LinkedList::add *
// Adds a given value to the list. *
//**********************************
void LinkedList::add(double x)
{
head = new ListNode(x, head);
}
int main( )
{
// Explain program to user
cout << "This program allows you to enter number in a linked list"
<< "\nby position. It also allow the list to be sorted.\n";
//
cout << "\nenter your first and last name here.\n\n";
//
//CREATE AN EMPTY LIST
LinkedList list1;
// Construct a list using insert by position
for (int k = 1; k <= 5; k++)
{
cout << "\nEnter a number followed by a position: ";
int x, pos;
cin >> x >> pos;
list1.insert(x, pos);
cout << "\nCurrent list membership is: ";
list1.rPrint();
}
// Demonstrate the list sort.
//
// TO SORT THE LIST
list1.sort();
cout << "\n\nFollowing is the sorted list: \n";
//
list1.rPrint();
list1.sort();
//
cout << "\n\n\n\n";
system("pause");
return 0;
}