链表插入排序在C [英] linked list insertion sort in c

查看:123
本文介绍了链表插入排序在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序应该做的插入升序排序的节点,首先它应该检查名称,如果名称是相等的,应该的ID进行排序,我不知道什么是不正确排序的问题。

 #包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;typedef结构{第二
INT ID;
焦炭名[20];
浮GPA;
第二结构*下一个;
}节点;
typedef的节点*名单;// ------------------------------------------------ ---------------INT insertlist(名单*头上,CHAR *缓冲区)
{
列表P,Q,N;
INT男,NUM,K,SC;
P =(列表)的malloc(sizeof运算(节点));
NUM = sscanf的(缓冲,%D%S%F,&安培;(对GT; ID),(对GT;名),(安培; P-> GPA));
如果(NUM!= 3)
 {
  的printf(信息不完整\\ n);
  自由(对);
  返回1;
 }
其他
{   如果(!*头)
   {
    *头= P;
    对 - >接着= NULL;
   }
// ********分拣等于名称里边反名称和ID
   否则,如果(SC = STRCMP((*头) - >的名字,P->名)大于0 ||((SC == 0)及及((*头) - GT; ID指p - 和SEQ ID)))
      {//头部被修改
        P->接下来= *头;
        *头= P;
      }   其他{
        N = *头;
        Q =正 - >接着,
        而(Q&安培;及((SC = STRCMP(Q->的名字,P->名)℃下)||((SC == 0)及及(Q-> ID<对 - 和SEQ ID))))
          {
           n为q;
           Q = Q->接着,
          }
       N->接下来= P;
       P->接下来= Q;
       }
}返回0;
}// ------------------------------------------------ ------诠释的main()
{
INT ID河;
表头,磷;
FILE * FP;
焦C,缓冲[100],文件名[10];
如果((FP = FOPEN(student.txt,R))== NULL)
{
  的printf(错误开幕%S,文件名);
  出口(1);
}
其他
 {
头= NULL;而(与fgets(缓冲器,100,FP)!= NULL)
   {
    缓冲[strlen的(缓冲)-1] =='\\ 0';
    R = insertlist(安培;头,缓冲区);
   }
FCLOSE(FP);
 }为(p值=头;!p值= NULL; P =对 - >接着)
  的printf(%d个%S%F \\ n \\ n,P-> ID,P->的名字,P-> GPA);
}

中的内容的一个示例 student.txt

  121513 ALA 45.00
121510旺21.00
145852坦率26.00
151515 ALA 25.00


解决方案

您排序问题的运算符precedence

< > 有较高的precedence比 = ,这意味着它会先评估,然后分配将发生。

所以你的字符串中的这两个地方比较:

 否则,如果(SC = STRCMP((*头) - >的名字,P->名)大于0 ||((SC == 0)及和放大器; ((*头) -  GT; ID> P-> ID)))
...
而(Q&安培;及((SC = STRCMP(Q->的名字,P->名)℃下)||((SC == 0)及及(Q-> ID<对 - 和SEQ ID))))

是错误的。 SC 越来越的值STRCMP((*头) - >的名字,P->名)GT; 0 STRCMP(Q->的名字,P->名)℃的分别为(注意这将永远是 1 0 ,从未 1

如果您简单地调整你的code这样:

 否则如果((SC = STRCMP((*头) - >的名字,P->名))大于0 ||((SC == 0)及及((*头) -  GT; ID> P-> ID)))
...
而(Q&安培;及(((SC = STRCMP(Q->的名字,P->名))小于0)||((SC == 0)及及(Q-> ID &所述;对 - 和SEQ ID))))

您会看到它的工作。这个故事的寓意:不要试图吝啬你的括号或支架,它不花费你任何东西把更多的,它使code清晰,这样可以节省你头痛调试像这样的<。 / p>

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 

解决方案

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.

这篇关于链表插入排序在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆