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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:04:55+00:00 2026-06-17T09:04:55+00:00

the program should do the insertion ascending sort for the nodes,first it should check

  • 0

the program should do the insertion ascending sort for the nodes,first it should check the names and if the names are equal it should sort the ids,i do not know what is the issue that does not sort properly.

# include <stdio.h>
# include <string.h>
# include <stdlib.h>

typedef struct nd{
int id;
char name[20];
float gpa;
struct nd *next;
}node;


typedef node *list;

//---------------------------------------------------------------

int insertlist(list *head,char *buffer)
{
list p,q,n;
int m,num,k,sc;
p=(list)malloc(sizeof(node));
num=sscanf(buffer,"%d %s %f",&(p->id),(p->name),(&p->gpa));
if(num!=3)
 {
  printf("info not complete\n");
  free(p);
  return 1;
 }
else
{

   if(!*head)
   {
    *head=p;
    p->next = NULL;
   }
//******** sorting tthe names and ids for equal names
   else if(sc=strcmp((*head)->name,p->name)> 0 || ((sc == 0) && ((*head)->id > p->id)))
      {//head is modified
        p->next=*head;
        *head=p;
      }

   else{
        n=*head;
        q=n->next;
        while(q && ((sc=strcmp(q->name,p->name)<0) || ((sc == 0) && (q->id < p->id))))
          {
           n=q;
           q=q->next;
          }
       n->next=p;
       p->next=q;
       }
}

return 0;
}

//------------------------------------------------------

int main()
{
int id,r;
list head,p;
FILE *fp;
char c,buffer[100],filename[10];
if ((fp=fopen("student.txt","r"))==NULL)
{
  printf("error opening %s",filename);
  exit(1);
}
else
 {
head=NULL;

while(fgets(buffer,100,fp)!=NULL)
   {
    buffer[strlen(buffer)-1]=='\0';
    r=insertlist(&head,buffer);
   }
fclose(fp);
 }

for(p=head;p!=NULL;p=p->next)
  printf("%d  %s  %f\n\n",p->id,p->name,p->gpa);
}

An example of the contents of student.txt:

121513 ala 45.00
121510 wang 21.00 
145852 frank 26.00 
151515 ala 25.00 
  • 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-17T09:04:55+00:00Added an answer on June 17, 2026 at 9:04 am

    Your sorting issue is one of operator precedence

    < and > have a higher precedence than =, meaning it will be evaluated first, then an assignment will take place.

    So your string compares in these two places:

    else if(sc=strcmp((*head)->name,p->name)> 0 || ((sc == 0) && ((*head)->id > p->id)))
    ...
    while(q && ((sc=strcmp(q->name,p->name)<0) || ((sc == 0) && (q->id < p->id))))
    

    are wrong. sc is getting the value of strcmp((*head)->name,p->name)> 0 and strcmp(q->name,p->name)<0 respectively (note this is going to always be 1 or 0, never -1)

    If you simply adjust your code as such:

    else if((sc=strcmp((*head)->name,p->name))> 0 || ((sc == 0) && ((*head)->id > p->id)))
    ...
    while(q && (((sc=strcmp(q->name,p->name))<0) || ((sc == 0) && (q->id < p->id))))
    

    You’ll see it working. Moral of the story: don’t try to be stingy with your parens or brackets, it doesn’t cost you anything to put more in, it makes the code clearer, and it saves you debugging headaches like this one.

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

Sidebar

Related Questions

my program should print a message on the screen if the formula that the
The assignment at my first year uni computing course says my program should read
I have this program that should execute a piece of code base on the
Im writing a program that should read input via stdin, so I have the
I'm working on a Java program that should repeatedly kill two processes as soon
I wrote a program to test and verify the running time of insertion sort
I'm trying to implement my first android Program. It should write calendar entries (I
When we program with database (for example in Java and MySql), should we check
This program should calculate the value of 2^1+2^2 + ... + 2^10: #include <stdio.h>
What the program should do: After the user clicks the delete button with a

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.