Home > Back-end >  Single linked list to set up and print what's the problem
Single linked list to set up and print what's the problem

Time:12-04

I am now doing the basic operations of a single linked list, but call the Print function can only output the latest the value of the incoming Set function can't Print all, is what happened, please?
Such as input 1 Alice 10001 input 1 Sam 10009 then enter 1 2, William, 10010 in the input can only output the latest input content, but how also can not find a reason, to feel your own code do not have what problem
 # include 
#include
#include
Typedef struct student {
int data;
char name[20];
Struct student * next;
}node;
* Set the node (the node * head, node * p);//in the end of the insert node p
Void Print (node * head);//print out all the node
* Insert node (the node * head, node * p, int n);//behind the NTH insert
Head node * Del (node * and an int data);//delete student id data node
Int Length (node * head); Length//o
Node * Sort (node * head);//in accordance with the data ascending order
Int main ()
{
Int choice, num, l, n;
Char r [20];
The head node *=NULL;
Node * p=(*) malloc (sizeof (node));
Printf (" Select: \ n1. Set \ n2 Print, \ n3 Insert, \ n4 interchange, Delete \ n5 GetLength, \ n6 Sort. \ n ");
The scanf (" % d ", & amp; Choice);
While (choice>=1 & amp; & Choice
=6){
The switch (choice) {
Case 1: printf (" Enter the name and data: \ n ");
The scanf (" % s % d ", r, & amp; Num);
Strcpy (p - & gt; The name, r);
P - & gt; data=https://bbs.csdn.net/topics/num;
The head=Set (head, p);
break;
Case 2: Print (the head); break;
Case 3: printf (" Enter the name and data: \ n ");
The scanf (" % s % d ", r, & amp; Num);
Strcpy (p - & gt; The name, r);
P - & gt; data=https://bbs.csdn.net/topics/num;
Printf (" Enter the location: \ n ");
The scanf (" % d ", & amp; n);
The head=Insert (head, p, n); break;
Case 4: printf (" Enter data: \ n ");
The scanf (" % d ", & amp; Num);
The head=Del (head, num); break;
Case 5: l=Length (the head); Printf (" Length=% d \ n "); break;
Case 6: head=Sort (head); break;
Default:; break;

}
Printf (" Select: \ n1. Set \ n2 Print, \ n3 Insert, \ n4 interchange, Delete \ n5 GetLength, \ n6 Sort. \ n ");
The scanf (" % d ", & amp; Choice);
}
return 0;
}
Node * Set (node * head, node * p)//in the end insert node p
{
Node * PTR.
If (head==NULL) {
The head=p;
The head - & gt; Next=head;
}
The else {
PTR=head;
While (PTR - & gt; Next!=head)
{
PTR=PTR - & gt; Next;
}
/* find tail */
P - & gt; Next=head;
PTR - & gt; Next=p;
}
Return the head;
}
Void Print (node * head)
{
Node * PTR=head;
If (head==NULL) {
Printf (" No records \ n ");
}
The else {
Printf (" % d % s \ n ", the head - & gt; The data, the head - & gt; Name);
PTR=PTR - & gt; Next;
for(; PTR!=the head; PTR=PTR - & gt; Next)
{
Printf (" % d % s \ n ", PTR - & gt; The data, the PTR - & gt; Name);
}
}
}
* Insert node (the node * head, node * p, int n)//behind the NTH node Insert the value of the p n> 0
{
Node * q=head;
int i=1;
If (head==NULL) {
Always Insert a printf (" \ n ");
} the else
{
While (q - & gt; Next!=head& & i{
Q=q - & gt; Next;
i++;
}
If (I==n) {
The node * r=q - & gt; Next;
P - & gt; Next=r;
Q - & gt; Next=p;
} else {
Always Insert a printf (" \ n ");
}
}
Return the head;
}
Head node * Del (node * and an int data)//delete student id for the data node
{
If ((head==NULL) & amp; & (the head - & gt; Next==head& & The head - & gt; The data!=data))
{
Always delete a printf (" \ n ");
} else if (head - & gt; Next==head& & The head - & gt; data=https://bbs.csdn.net/topics/=data) {
Free (the head);
} else {
Node * (p1, p2,
Int flag=1;
P1=head;
The p2=p1 - & gt; Next;
If (p1 - & gt; data=https://bbs.csdn.net/topics/=data)
{
While (p2 - & gt; Next!=head)
{
The p2=p2 - & gt; Next;
}//p2 tail
The head=head - & gt; Next;
The p2 - & gt; Next=head;
Free (p1);
}
The else
{
for(; The p2!=the head; P1=p1 - & gt; Next) {
If (p2 - & gt; data=https://bbs.csdn.net/topics/=data) {
flag=0;
P1 - & gt; Next=p2 - & gt; Next;
Free (p2);
break;
}
The p2=p2 - & gt; Next;
}
If (flag==1) {
Always delete a printf (" \ n ");
}
}
}
Return the head;
}
Int Length (node * head) {
Int count;
Node * p;
If (head==NULL) {
Count=0;
} else if (head - & gt; Next==head)
{
Count=1;
} the else
{
Count=1;
For (p=head - & gt; Next; p!=the head; P=p - & gt; Next)
{
count++;
}
}
return count;
}
Node * Sort (node * head)//in accordance with the student id ascending order
{
Int t;
Char STR [20].
Node * I=head, * j=head;
If (head==NULL | | (head - & gt; Next==head)) {//a single node
Return the head;
}
The else {
For (I=head; I - & gt; Next!=the head; I=I - & gt; Next) {
For (j=head; J - & gt; Next!=the head; J=j - & gt; Next) {
If ((j - & gt; Data) & gt; (j - & gt; Next - & gt; Data))
{
T=j - & gt; data; J - & gt; data=https://bbs.csdn.net/topics/j-> next -> data; J -> next -> data=t;
Strcpy (STR, j - & gt; Name);
Strcpy (j - & gt; The name, j - & gt; Next - & gt; Name);
Strcpy (j - & gt; Next - & gt; The name, STR);
}
}
}
Return the head;
}
}

CodePudding user response:

Why so many code? To input and print

CodePudding user response:

Now don't have to malloc list insert when allocating memory delete released when free memory? -- -- -- -- --

CodePudding user response:

Code too much, I also didn't watch carefully to see where your insert didn't see you allocated memory you just at the beginning of the main function are assigned a node in the structure of the body you want in each time you insert the malloc allocation a memorynullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related