Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,331 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,127 people online right now. Registration is fast and FREE... Join Now!




Single Linked List(SOLVED!)

 
Reply to this topicStart new topic

Single Linked List(SOLVED!), Adding Nodes

wisheshewasleet
post 2 Apr, 2008 - 04:38 PM
Post #1


New D.I.C Head

*
Joined: 17 Sep, 2007
Posts: 15


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

wisheshewasleet
post 2 Apr, 2008 - 08:48 PM
Post #2


New D.I.C Head

*
Joined: 17 Sep, 2007
Posts: 15


My Contributions


I just need help with this part.

CODE

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
}


User is offlineProfile CardPM

Go to the top of the page

wisheshewasleet
post 2 Apr, 2008 - 09:21 PM
Post #3


New D.I.C Head

*
Joined: 17 Sep, 2007
Posts: 15


My Contributions


The fixed function is here.
The problem is that in my while loop, I was checking current->info instead of current->link->info agsint _input value.
I also wasn't checking to see if the first node was smaller than the input value.

so those things are added in. Hope it helps someone down the way!

hewre's the add sorted nodes function!!!
CODE

/*******************************************************/
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;
    if(head->info > _inputVal)
    {
        temp=head;
        head= new NodeType();
        head->link=temp;
        head->info=_inputVal;
    }
    else
    {
    while (current->link!=NULL &&_inputVal > current->link->info)
    {
      current = current->link;
    }
    temp=current->link;
    current->link = new NodeType();
    current = current->link;
    current->link = temp;
    current->info = _inputVal;
    }
}
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 03:04AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month