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

  • Home
  • SEARCH
  • 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 8742903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:34:57+00:00 2026-06-13T11:34:57+00:00

This is what I have so far: void sort(const E &t) { DNode<E> *tmp

  • 0

This is what I have so far:

void sort(const E &t)
{
  DNode<E> *tmp = new DNode<E>(t,NULL,NULL);


    if(size==0)
    {
        cout << "List is empty" << endl;
    }

            else if(t<=head->element)
            {
                tmp->element=t;
                head->prev = tmp;
                tmp->next=head;
                head = tmp;
            }
                    else if(t>=tail->element)
                    {
                        tmp->element=t;
                        tail->next = tmp;
                        tmp->prev=tail;
                        tail = tmp;
                    }

                         curr=tmp;
                         insert(t);
                         size++;
} 

insert() is just another function in my program:

 void insert(const E &t)
{
  DNode<E> *tmp = new DNode<E>(t,NULL,NULL);
  if (size == 0)
  { curr=head=tail=tmp; }
  else 
  {
    tmp->next=curr;
    tmp->prev=curr->prev;
    if (curr->prev) curr->prev->next=tmp;
    else { head=tmp; }
    curr->prev=tmp;
    curr=tmp;
  }
  size++;
}

It does compile, but it doesn’t give the correct results. I’m not sure what my errors are, and I would really need help.
Any help would be appreciated.

This is in my main program:

  one.sort(10);
  one.sort(20);
  one.sort(30);
  one.sort(40);
  one.sort(50);
  one.sort(60);
  one.print();
  one.moveToEnd();
  one.prev(); 
  one.prev();
  one.remove();
  one.remove();
  one.print();

  cout<<endl;

I should be getting this:

HEAD==> 10 -> 20 -> 30 -> 40 -> 50 -> 60 <==TAIL
HEAD==> 10 -> 20 -> 50 -> 60 <==TAIL

But I get this instead:
HEAD==> 10 -> 20 -> 20 -> 30 -> 30 -> 40 -> 40 -> 50 -> 50 -> 60 -> 60< ==TAIL
HEAD==> 10 -> 20 -> 20 -> 30 -> 30 -> 40 -> 40 -> 60 -> 60 <==TAIL

  • 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-13T11:34:58+00:00Added an answer on June 13, 2026 at 11:34 am

    The cause of the behavior you’re seeing is that your missing an else

    this:

    curr=tmp;
    insert(t);
    size++;
    

    is executed whether you have added something to the head or tail, or not. Since each entry you give will be added to the tail, it gets inserted twice each time. You should only call insert if you have not yet added the value to the head or tail.

    If I understand correctly, curr=tmp; and size++; should be run regardless, so only the call to insert should be inside an else block, I think.

    EDIT:

    Should look like this:

    if(size==0)
    {
        cout << "List is empty" << endl;
        //Need to insert here as well, to add the first value to the list.
        insert(t);
    }
    
            else if(t<=head->element)
            {
                tmp->element=t;
                head->prev = tmp;
                tmp->next=head;
                head = tmp;
            }
                    else if(t>=tail->element)
                    {
                        tmp->element=t;
                        tail->next = tmp;
                        tmp->prev=tail;
                        tail = tmp;
                    }
    
                    else 
                    {
                         insert(t);
                    }
                    curr=tmp;
                    size++;
    

    I’ve (kinda) maintained your whitespace usage, though I find it a bit unusual, by the way. I would normally place related ‘if’ ‘else if’ and ‘else’ statements on the same indention level, and indent further for nested blocks. I think that’s more standard, but neither here nor there.

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

Sidebar

Related Questions

I have a function of the form: void DoSomething(const boost::function<bool ()>& condition, other stuff);
I have this so far but the Column 'name' is ambiguous, so I want
I have this so far but I don't know how to write over the
I have this so far: http://jsfiddle.net/u5vhS/54/ . What I want to be able to
So far i have this: mov ah,02h mov cl,11001100001111011101000b ;6,692,584 in dec mov dl,0
I'm using the javascript module pattern, and have this so far: var APP; if(APP
Thanks to everyone out there helping newbies like me. So far I have this:
This is what I have so far: [my1@graf home]$ curl -# -o f1.flv 'http://osr.com/f1.flv'
This is what I have so far - my dropbox public URL creation script
This is what I have as far as code, it is in the first

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.