Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:55:11+00:00 2026-06-18T15:55:11+00:00

I’ve been banging my head against the wall for about 3 hours now trying

  • 0

I’ve been banging my head against the wall for about 3 hours now trying to come up with the fix for this but I can’t figure it out. My test program is written as such…

int main()
{
  SimpList<int> intList;   // (empty) list of integers

  cout << "Let's build a sorted list of integers." << endl;
  cout << endl << "Uninitialized List: ";
  intList.print();
  cout << endl << "Length: " << intList.size() << endl;

  int intData[] = { 5, 3, -2, 7, 9, -8, 1, -4 };

  for (int i=0; i<8; i++)
    intList.insert( intData[i] );

  cout << endl << "After inserting 8 integers: ";
  intList.print();
  cout << endl << "Length: " << intList.size() << endl;

  system("PAUSE");
  return 0;
}

So the Link List is getting initialized from an array and a for loop. My class code for the node and list is here…

    template < typename T >   // Forward declaration of the SimpList class
class SimpList;

template < typename T >
class Node                 // Node class for the SimpList class
{
  private:
    // Constructors
    Node () { next = 0; }  // default constructor

    // Complete the definition inline
    Node ( const T &initItem, Node<T> *ptr ) { }

    // Data members
    T           item;   // Node data item
    Node        *next;  // Pointer to the next node

  friend class SimpList<T>;
};

template < typename T >
class SimpList
{
  public:

    // Constructor (add your code inline)
    SimpList ()
    {
      head = &PHONY;
      length = 0;
    }


    // List manipulation operations
    void insert ( const T &newitem );   // insert a data item
    bool remove ( T &item );            // remove data item
    bool find ( T &item ) const;        // find data item
    void clear ();                      // empty the list

    bool isEmpty () const { 
     if (length == 0)
         return true;
     else
         return false;
    }

    // length accessor method
    int size () const { 
        return length;
    }

    // print the list items
    void print () const;

  private: // data members
    Node<T> PHONY;      // empty node that anchors the list
    Node<T> *head;      // pointer to the beginning of the list
    int length;         // length of list
};

And then the insert and print functions are as follows…

template < typename T >
void SimpList<T>::print() const
{
  if (length == 0)
  {
    cout << "List is empty." << endl;
    return;
  }

  Node<T> *ptr = head->next;
  while (ptr != NULL)
  {
    cout << ptr->item << "  ";
    ptr = ptr->next;
  }
  cout << endl;
}

template < typename T >
void SimpList<T>::insert(const T& newitem) {

        Node<T> *currN = head->next;
        Node<T> *prevN = 0;
        Node<T> *tmp = new Node<T>();
        tmp->item = newitem;

        if(head->next == NULL ) {
          head->next = tmp;
        }
        else {
            prevN = head;
            while(currN != NULL && currN->item < newitem) {
                prevN = currN;
                currN = currN->next;
            }
            prevN->next = tmp;
        }
        length++;
        print();
}

I inserted the last “print()” into the insert function as a way of debugging what was happening and the output is quite perplexing as it gives me

5
3
-2
-2 7
-2 7 9
-8
-8 1
-8 -4

But I want the output to be sorted smallest to largest (-8 -4 -2 1 3 5 7 9)

edit: solved…forget to update tmp->next to currN. DERP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:55:13+00:00Added an answer on June 18, 2026 at 3:55 pm

    Couple of things you need to fix:

    1. PHONY->next is not null , so your initialization will fail :

      if(head->next==NULL) {
        head->next=tmp;
        return;
      }
      
    2. Node constructor should initialize next to 0 instead of this , hence you will never hit NULL condition for end of your list.

    3. in insert in else statement you should start search from head->next, currently you start search from currn = head which is PHONY which is not correct.

    4. You also need to set tmp->next appropriately ,consider this case when you are inserting in middle of the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I upgraded downgraded to rails 2.3.17 due to the security bugs, but now I
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.