Home > Back-end >  With yi help [C] a difficult questions in the list
With yi help [C] a difficult questions in the list

Time:05-03

Questions in the comments, this might be a little complicated, please you bother, fees, fees brain
I am currently stuck in step (b), the following is to perform (a) (b) code, problem is in creat_sort_lianbiao (struct node * head) where the loop inside will be unable to perform
 # include & lt; Stdio. H> 
#include
#include
/*
1. List and an array of integrated operation (subject full score 100 points, each asked 20)
A) generate a length of 100, the values range from 0-300 integer, linked lists, and print the
B) will be inserted by size of the order in another list, and traverse the print
C) the results of the sorted list written to the file 1000 _numbers. TXT
D) to read data from the file and stored in an array, and print the
E) statistical frequency up to digital, and print the
*/
Struct node
{
Int num.
Struct node * next;
};
Struct node * creat_lianbiao ()
{
//generates a length of 100, random integer values range from 0-300 list... The Done
//int num.
Srand (time (0));

int i=0;
Struct node * head=NULL, * p=head, * newnode=NULL;
While (1)
{

Newnode=(struct node *) malloc (sizeof (struct node));
Newnode - & gt; Num=rand () % 301;
//printf (" % d ", newnode - & gt; Num);

If (head==NULL)
{
The head=newnode;
P=the head;
}

Else if (the head!=NULL)
{

P - & gt; Next=newnode;
P=p - & gt; Next;
}

i++;

If (I==100)
{
P - & gt; Next=NULL;
break;
}

}
//p=head;
Return the head;
}
//traversing print list function
Void print_lianbiao (struct node * head)
{
Struct node * p=head;
int i=0;
While (p!=NULL)
{
i++;
Printf (" % the DTH number: ", I);
Printf (" % d \ n ", p - & gt; Num);
P=p - & gt; Next;

}
}
/* core parts:
Description:
1. The subject can't in the original order list and then inserted into another list
2. You can't use the list to insert
3. My idea is: the cycle, find out the minimum value of each cycle,
The minimum value of node is inserted into the new list, at the same time in the original list to delete the node
So at the time of the next cycle will not find the last of the minimum
4. The problem is cycle cannot execute down

*/
Struct node * creat_sort_lianbiao (struct node * head)
{
Struct node * p=head, * head_2=NULL, * p_min=head, * p_2=head_2;
Int min=p_min - & gt; Num, I=0;

While (1)//
{
P=the head;
Min=head - & gt; Num.
While (1)//to find the minimum value in the list each cycle
{

If (min & gt; P - & gt; Num)//if min records is greater than the current value of the
{
//printf (" Done... \n");
Min=p - & gt; Num.
P_min=p;//the current value of addresses to p_min
}
If (p - & gt; Next==NULL) break;//if to the end, break
P=p - & gt; Next;
}
P=the head;

i++;
While (1)//min value of each cycle the original list address is assigned to p_2, and the node to delete the original list
{

If (p - & gt; Next==p_min)/search/cycle, if the match to p_min
{

P_2 - & gt; Next=(struct node *) malloc (sizeof (struct node));//open the node
P_2 - & gt; Next - & gt; Next=(struct node *) malloc (sizeof (struct node));//to create node

If (head_2==NULL)//if head_2 is empty list
{
//printf (" Done... \n");
Head_2=p_min;
P - & gt; Next=p - & gt; Next - & gt; Next;//remove the original min value node in the linked list
Printf (" p_min - & gt; Num=% d \ n ", p_min - & gt; Num);
break;

}
Else if (head_2!=NULL)
{

P_2 - & gt; Next=p_min;
P_2=p_2 - & gt; Next;
P_2 - & gt; Next=NULL;
P - & gt; Next=p - & gt; Next - & gt; Next;
break;

}
}
P=p - & gt; Next;

}
If (I==100)//if performed over 100 times
{
//printf (" Done... \n");
break;
}

}
//printf (" Done... \n");
Return head_2;



}


Void main ()
{

Printf (" the first question: \ n ");
Struct node * head=NULL, * p=head, * head_2=NULL;

The head=creat_lianbiao ();//generates a length of 100, random integer values range from 0-300 list... The Done
Print_lianbiao (head);//print the list... The Done
Putchar (" \ n ");
Putchar (" \ n ");
Head_2=creat_sort_lianbiao (head);//generate new list
Printf (" Done... ");
Print_lianbiao (head_2);//print new list


}

Bemused results:

CodePudding user response:

Second long comments there "cannot use list sorting" wrong number, should be "can't use array auxiliary sort"
  • Related