c/c++语言开发共享c中链表中的升序

我试图通过链接和地址而不是值来改变链表中的升序

struct node { char name[30]; int percent; struct node *link; }; int main { clrscr(); randomize(); struct node *st; st=NULL; for(int i=0;ipercent display(st); AscMarks(&st); //Changing the order of links and addresses to arrange them in ascending order printf("nAscending order list...n"); display(st); getch(); return 0; } /*Adds a node at the end of a linked list */ void append(struct node **q,int per) { struct node *temp,*r; temp=*q; /* If the list is empty , create first node */ if(temp==NULL) { temp=(node*)malloc(sizeof(struct node)); temp->percent=per; getName(temp->name); temp->link=NULL; *q=temp; } else { while(temp->link!=NULL) temp=temp->link; r=(struct node*)malloc(sizeof(struct node)); r->percent=per; getName(r->name); r->link=NULL; temp->link=r; } } /*Displays the contents of the linked list */ void display(struct node *q) { while(q!=NULL) { printf("%dt%sn",q->percent,q->name); q=q->link; } } void getName(char *c) { for(int i=0;i<30;i++) { if(i==10||i==20) *(c+i)=' '; else *(c+i)=(char)((random(26)+97)); } *(c+i+1)=''; } /*To change the links and addresses in order to arrange the percent in ascending order */ void AscMarks(struct node **q) { struct node *temp,*temp1,*r; temp=*q; // r=q; for(int i=0;ilink) { temp1=temp->link; for(int j=i+1;jlink) { if(temp->percent>temp1->percent) { r=*q; while(r->link!=temp1) { r=r->link; } r->link=temp1->link; temp1->link=temp; temp=temp1; } } if(i==0) *q=temp; } temp->link=NULL; /* while(r!=NULL) { printf("n%d",r->percent); r=r->link; } */ } 

升序( AscMarks )没有按预期给出结果,我无法看到代码中的错误请帮忙

    你没有在最小地址到第二个最小地址之间建立链接等等……我已经通过节点变量“* s”通过s-> link = temp并将最后一个排序值(temp)的地址赋予s ..通过s = temp ..并且在每次运行后的“j”循环中,temp-> percent> temp1-> percent ..你已经完成了temp1-link = temp,它使得temp1的地址,即起始温度的地址…简而言之……“j”的尝试次数太少,无法比较所有地址……因为大部分地址都会重复…所以对于它你应该做j = i;

      void AscMarks(struct node **q) { struct node *temp,*temp1,*r,*s; temp=*q; // r=q; for(int i=0;i<7;i++,temp=temp->link) { temp1=temp->link; for(int j=i+1;j<7;j++,temp1=temp1->link) { if(temp->percent>temp1->percent) { r=*q; while(r->link!=temp1) { r=r->link; } r->link=temp1->link; j=i;//resetting the value of j temp1->link=temp; temp=temp1; if(i!=0) s-link=temp; //establishing link between //this sorted address(in this loop) to the //last sorted address } } if(i==0) *q=temp; s=temp;//giving it the address of structure which have the last //sorted value } temp->link=NULL; //No need to do this /* while(r!=NULL) { printf("n%d",r->percent); r=r->link; } */ } 

      以上就是c/c++开发分享c中链表中的升序相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/519734.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐