#include
#include
#include
#include
Struct people
{
Char name [20].
Char phone [30].
Char email [30];
Struct people * next;
};
Int main ()
{
Struct people * head;
Struct people * creat ();
Void the output (struct people * head);
The head=creat ();
The output (the head);
return 0;
}
Struct people * creat ()
{
Struct people * head;
P1, struct people * * p2.
Void the save (struct people * (p1);
Int I, n;
P1=p2=(struct people *) malloc (sizeof (struct people));
I=1;
n=0;
The head=NULL;
Printf (" please enter: \ n ");
While (I==1)
{
Printf (" name: ");
The scanf (" % s ", & amp; P1 - & gt; Name);
Printf (" call: ");
The scanf (" % s ", & amp; P1 - & gt; Phone);
Printf (" Email: ");
The scanf (" % s ", & amp; P1 - & gt; Email);
Save (p1);
If (n==0) head=p1;
The else p2 - & gt; Next=p;
P1 p2=;
P1=(struct people *) malloc (sizeof (struct people));
N=n + 1;
Printf (" continue to enter please press 1, exit please press 0. \ n ");
The scanf (" % d ", & amp; i);
}
The p2 - & gt; Next=NULL;
Return (the head);
}
Void the output (struct people * head)
{
Struct people * p;
Void sort (struct people * head);
Printf (" * * * * * * * * * * * * * * * * * * * * * * * * * address book * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
P=the head;
Sort (head);
If (a head!=NULL)
{
Do
Telephone: {printf (" name: % s % s \ n ", p - & gt; The name, p - & gt; Phone);
P=p - & gt; Next;
} while (p!=NULL);
}
}
Void the save (struct people * p)
{
The FILE * fp.
Char name [20].
Printf (" file name: ");
The scanf (" % s ", name);
If ((fp=fopen (name, "wb"))==NULL)
{
Printf (" always open a file \ n ");
return;
}
If (fwrite (p, sizeof (struct people), 1, fp)!
=1)Printf (" the file write error \ n ");
The fclose (fp);
}
Void sort (struct people * head)
{
Struct people * (p1, p2, * tail;
Tail=NULL;
While ((head - & gt; Next - & gt; Next)!=NULL)
{
P1=head;
The p2=head - & gt; Next;
While ((p1 - & gt; Next - & gt; Next)!=NULL)
{
If (STRCMP ((p1 - & gt; Name), (p1 - & gt; Next - & gt; The name) & gt; 0)
{
P1 - & gt; Next=p2 - & gt; Next;
The p2 - & gt; Next=p2 - & gt; Next - & gt; Next;
P1 - & gt; Next - & gt; Next=p2;
The p2=p1 - & gt; Next;
}
P1=p1 - & gt; Next;
The p2=p2 - & gt; Next;
}
Tail=p2;
}
CodePudding user response:
Suggest sorting data section with the exchange, exchange pointer part is more complex and difficult to deal withCodePudding user response:
He this problem is not only a complicated problem, on the whole have a bunch of questions, didn't want to good help to change from beginning to end, or only advice to sortThis sort is a not complete code, trouble not only on the complex of exchange, but on the whole process of exchange control
For example, the head is not short, is an effective data, then head should also participate in the exchange, but it can not exchange, even if the exchange could not return to the
The tail pointer, it is used to control the similar bubble sorting process, where execution, no use
In front of the save function, but also save the current only one node, the output of the whole list just repeat a node
Input, the tail will be more than a a meaningless empty node
Various problems in short, the code of the overall flow is smooth, can be a lot of questions on the details, I don't know how to give advice
CodePudding user response:
That should be how to changeCodePudding user response:
According to the train of thought to change yourself,This is good for you,
Even if there is a problem, even if the method of trouble, go now, go into a big step
Sort to change good, head nodes are to be involved in the sorting, and changed after the first node is not need to return it, then it is function prototype is
Struct people * sort (struct people * head);
Call is: when the head=sort (head); With the return value update calls its head
To exchange a pointer, you need three things to actually
A node on the current node next node
Exchange is the current and the next node, but after the exchange, the result of the need to change the next pointer to a node on the
Proir - & gt; Cur - & gt; Next, & gt; More exchange before
Proir - & gt; Next, & gt; Cur - & gt; More exchange after
Ptail is within the range of control cycle, the first order from the beginning to the end of the
The second control, from the beginning to the penultimate
The third time control, from top to bottom third
.
Because behind is sorting good
By controlling the while (tail==head - & gt; Next) to judge all sorts end
This is just a way, the code will have trouble, but if the node data structure complexity, this method is of high efficiency, higher than exchange data,
CodePudding user response:
#include#include
#include
Typedef struct People {
Char name [20].
Char id [20];
Struct People * next;
} People;
People * create (int n) {
If (n<1) return NULL;
int i; Int v.
V=rand () % 1000;
People * pHead=(People *) malloc (sizeof (People));
Sprintf (pHead - & gt; The name, the name "- % 3 d", v);
Sprintf (pHead - & gt; Id, "id - % 3 d", v);
People * p=pHead;
For (I=1; i
V=rand () % 1000;
Sprintf (p - & gt; Next - & gt; The name, "the name - % d", v);
Sprintf (p - & gt; Next - & gt; Id, "id - % 3 d", v);
P=p - & gt; Next;
}
P - & gt; Next=NULL;
Return pHead;
};
Void printList (People * pHead) {
Printf (" \ n -- -- -- -- -- -- -- -- -- People List -- -- -- -- -- -- -- -- -- \ n ");
If (pHead==NULL) return;
People * p=pHead;
While (p) {
Printf (" % s: % s \ n ", p - & gt; The name, p - & gt; Id);
P=p - & gt; Next;
}
}
People * sortList (People * pHead) {
If (NULL==pHead | | pHead - & gt; Next==NULL) return pHead;
People * pTH=(People *) malloc (sizeof (People));
PTH - & gt; Next=pHead;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull