?
Solved

insertion into linked list

Posted on 2003-11-13
5
Medium Priority
?
245 Views
Last Modified: 2010-04-02
I wanted to insert a value to a linked list in ascending order. But when i insert 5,10,2 - it will print out 5,2,10. When i try 4,3,8 - its correct! Pls correct my mistakes below.

void insert(nodePtr &head, value val)
{
      nodePtr newNode = new node;
      newNode->num = val;
      newNode->next = NULL;

      if(head == NULL)
            head = newNode;

      else if(head->next == NULL)
      {
            if(head->num > val)
            {
                  newNode->next = head;
                  head = newNode;
            }
      }
      else
      {
            nodePtr search = head;
            nodePtr temp;

            while(search != NULL)
            {
                  if(search->next == NULL)
                  {
                  search->next = newNode;
                        break;
                  }

                  if(search->next->num > val)
                  {
            temp = search->next;
                  search->next = newNode;
                  newNode->next = temp;
                        break;
                  }

                  search = search->next;
            }

      }
}
0
Comment
Question by:Rebecca2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
sbooth17 earned 120 total points
ID: 9740825
Try this, untested code, I think it should work.

nodePtr newNode = new node;
newNode->num = val;
newNode->next = NULL;

if(head == NULL)
    head = newNode;
else
{
     nodePtr search = head;
     nodePtr temp;
 
  //check the first value  
  if(val < search->num)
  {
     //goes before head
     newNode->next = search;
     head = newNode;
     return;
  }
 
  while(true)
  {
    if(search->next == NULL)
    {
      search->next = newNode;
      break;
    }
    if(val < search->next->num)
    {
       temp = search;
       search->next = newNode;
       newNode->next = temp;
      break;
    }
    search = search->next;
  }
}
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9741164
What type is val and num? If that are strings then "10" is less "2" because comparision works lexicographically.
Use type int if i am right.

Regards, Alex
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9741303
Rebecca2003:

You should probably keep one question in one posting rather than here and in http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20796480.html

You should go to Community Support and have one of them removed.

-bcl
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249141
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: sbooth17 {http:#9740825}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question