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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:33:44+00:00 2026-06-18T22:33:44+00:00

Below is my insertion sort using linked list code. I have debugged this like

  • 0

Below is my insertion sort using linked list code. I have debugged this like no other, but can’t figure out how to make it sort. As it sits right now, it goes in an infinite loop going into the if statement in insert(). What do I need to change?

//Sort function to call insertion sort function
void sortEmps()
{
temp = NULL;
struct EMP* next = top;

while(top != NULL)
{
    next = top -> next;
    insert(temp);
    temp = next;
}

top = temp;
}

//Insertion sort function
void insert(struct EMP *emp)
{
prev = NULL;
current = temp;

while (current != NULL && current->id < emp->id)
{
    prev = current;
    current = current->next;
}

if (prev == NULL)
{
    temp = emp;
}
else
{
    emp -> next = prev -> next;
    prev -> next = emp;
}
}

Here is my struct and add function. Pretty much the only things that are used before the sort. I am able to initialize a bunch of employees, so they are stored.

 typedef struct EMP
 {
int id;
char name [MAX];
double salary;
struct EMP* next;

} EMPLOYEE;                                                        
int addEmployee(char* name, double salary)
{
struct EMP* emp = createEmployee(name, salary);
emp -> next = top;
top = emp;

numEmps++;
//employees[numEmps++] = emp;
return TRUE;
}
  • 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-18T22:33:45+00:00Added an answer on June 18, 2026 at 10:33 pm

    Think what will happen if you trying to insert to existing list and current->id > emp->id (you never go into while loop and your prev == null, so by changing the pointer to the head of list to point to something else and as a result loosing your list somewhere in memory which is not dealocated, you are playing with fire.

    btw: it has no relation to your insertion sort

    I will not write you here the correct way to write this type of code, you can find enough information on the net.

    few suggestion , loose your global pointers , and read how to design a linked list that supports an easy insert and delete from the head ( it probably will say something on one more structure that you should use)

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

Sidebar

Related Questions

I have implemented an insertion sort in a double link list (highest to lowest)
Insertion-sort works also on unordered array, like the example here shows. This statement in
I'm trying to understand the follow code below: /** * Simple insertion sort. *
Possible Duplicate: php/Mysql query with inserting date fails I have a datepicker code below:
Below is the code from a plugin I use for sitemaps. I would like
I have a MySQL singleton class written in PHP. Its code is listed below:
I have the code below that I have found. I am trying to learn
I am currently using the below code to run a process and print its
i know insertion sort isn't that great...but even still...what are two more simple improvements
I have written the code below for adding products to a basket, then outputting

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.