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 8839645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:16:57+00:00 2026-06-14T10:16:57+00:00

So i have a singly linked list. New items are added to the front

  • 0

So i have a singly linked list. New items are added to the front of the chain, so if you added 8,4,10 the list would be 10,4,8. Anyway, now I am trying to sort the list after the insertion is completed except I just cannot figure out how to then loop through these numbers and re-arrange them in ascending order. I’ll probably take a break here and come back in a bit, hopefully that will help me figure this out.

*this is a project for school, so suggesting I use some other container is not helpful in my case except being informative as I cannot change what I am working with.

Layout of the list

struct Node
    {
      int Item;      // User data item
      Node * Succ;   // Link to the node's successor
    };

unsigned Num //number of items in the list
Node * Head //pointer to the first node

My insertion function looks like this

Node * newOne;
newOne = new (nothrow) Node;
newOne->Item = X;
newOne->Succ = NULL;

if(Head == NULL)
{
        Head = newOne;
}
else
{
        newOne->Succ = Head;
        Head = newOne;
}
Num++;
  • 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-14T10:16:58+00:00Added an answer on June 14, 2026 at 10:16 am

    This can be done two ways, i am not going to post the code, but hopefully this will help you.

    1. Arrange in ascending order during insertion

    This involves adding the elements always in ascending order. For example if the link list is

                | 1  |
    

    and you add 5 the new link list is

               |1|--->|5|
    

    and if you add 3 next it should be

               |1| ---> |3| ----> |5|
    

    This involves comparison of the new element till you find the correct position.

    2. Wait till all the elements are inserted, arrange in ascending order.

    This would involve using a sorting algorithm like merge sort, insertion sort, bubble sort etc on the elements of linklist.

    After the sorting of the numbers is done, rewrite the linklist in the correct order.

    Example:

    if link list contains

            3 2 5 1 6 
    

    After sorting through an algorithm

      1 2 3 5 6 (stored in an array)
    

    Now loop through the link list and replace the elements in correct order.

    Be careful if the nodes contain other elements, those other elements would need to be replaced too.

    Note: This requires extra memory, the other way would to swap the nodes which would take longer time. Unless the link-list contains a large number of nodes(thus making memory important) or has other elements, using array would be simpler.

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

Sidebar

Related Questions

I have a singly linked list and I need to sort it in constant
I am trying to sort a singly linked list which has been created and
I have a singly-linked list containing 1 value in each element. struct listElement {
I have a singly linked list with node and list structs defined as: typedef
What I want to do is have a singly linked-list class with a default
I'm trying to write a basic, singly-linked list class in C++. I did it
Possible Duplicate: Reverse a singly linked list reverse a linked list? I have been
Assume that I have a singly linked list with sentinel. To remove the last
Suppose, I have a singly linked list and its basic building block is, struct
I'm trying to make a circular singly linked list. I'd like to be able

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.