hey everyone, I have a program that simply creates a single linked list and then prints them out, either forwards or backwards.
Now, I have to add a function (AddSortedNode(int _value)) to the linked list program such that new nodes are added in ascending order.
I'm close, but it doesn't work in all situations. Can anyone see why?
the input nodes function calls the addsortednode function. the print functions and the addnodes at beginning and end functions all work correcetly.
CODE
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
NodeType * head = NULL;
/*******************************************************/
class NodeType
{
public:
int info;
NodeType * link;
};
/*******************************************************/
void AddNodeAtEnd(int _inputVal);
void AddNodeAtBeginning(int _inputVal);
void AddSortedNode(int _inputVal);
void InputNodes();
void PrintNodes();
void PrintNodesBackwards(NodeType * current);
/*******************************************************/
int _tmain(int argc, _TCHAR* argv[])
{
int userInput;
InputNodes();
PrintNodes();
cout<< "Backwards" << endl;
PrintNodesBackwards(head);
system("pause");
return 0;
}
/*******************************************************/
void InputNodes()
{
int inputVal = 0;
while (1)
{
cout << "Please Enter a new Node value, (-1) to quit: " << endl;
cin >> inputVal;
if (inputVal == -1)
return;
// AddNodeAtBeginning(inputVal);
// AddNodeAtEnd(inputVal);
AddSortedNode(inputVal);
}
}
/*******************************************************/
void AddNodeAtBeginning(int _inputVal)
{
NodeType * newNode = new NodeType();
newNode->info = _inputVal;
newNode->link = head;
head = newNode;
}
/*******************************************************/
void AddSortedNode(int _inputVal)
{
if (head == NULL)
{
// first node
head = new NodeType();
head->link = NULL;
head->info = _inputVal;
}
else
{
// add a new node
NodeType * current = head;
NodeType * temp;
while (_inputVal < current->info && current->link!=NULL)
{
current = current->link;
}
temp=current->link;
current->link = new NodeType();
current = current->link;
current->link = temp;
current->info = _inputVal;
}
//Add Nodes According to the input val.
//Large values should be at the front of the list
}
/*******************************************************/
void AddNodeAtEnd(int _inputVal)
{
if (head == NULL)
{
// first node
head = new NodeType();
head->link = NULL;
head->info = _inputVal;
}
else
{
// add a new node
NodeType * current = head;
while (current->link )
{
current = current->link;
}
current->link = new NodeType();
current = current->link;
current->link = NULL;
current->info = _inputVal;
}
//Add new nodes to the end of the list
}
/*******************************************************/
void PrintNodes()
{
cout << "Print Nodes!" << endl;
NodeType * current = head;
if (current == NULL)
return;
while (current->link )
{
cout << current->info << endl;
current = current->link;
}
cout << current->info << endl;
cout << "Node List Printed!" << endl;
}
/*******************************************************/
void PrintNodesBackwards(NodeType * current)
{
if (current!=NULL)
{
PrintNodesBackwards(current->link);
cout << current->info << endl;
}
}
/*******************************************************/
This post has been edited by wisheshewasleet: 2 Apr, 2008 - 09:06 PM