0
我想通過多條鏈路和地址,而不是價值變化做鏈表升序升序在C
struct node
{
char name[30];
int percent;
struct node *link;
};
int main
{
clrscr();
randomize();
struct node *st;
st=NULL;
for(int i=0;i<7;i++)
append(&st,random(101)); //Assigning random values to structure node->percent
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("%d\t%s\n",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)='\0';
}
/*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;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;
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
)是不會放棄的結果如預期,我無法看到代碼中有什麼故障請幫助
'void AscMarks(struct node * q)':元素的開始不能被改變。 – BLUEPIXY
'AscMarks'必須取得列表指針的地址才能更改第一個節點(例如'void AscMarks(struct node ** q)')。用'AscMarks(&st);' –
')呼叫我編輯了那個錯誤..但現在打印列表時有些值沒有打印.. @ DavidC.Rankin – joker007