Home > Software engineering >  List pointer unreadable
List pointer unreadable

Time:10-07

The following is the process of the error:
Void selectnum (stu * head)
{int num, j;
Stu * p=head;
Printf (" input to query student id \ n ");
The scanf (" % d ", & amp; Num);
While (p - & gt; Num! Num=)
{p=p - & gt; next;
If (p=NULL) {printf (" not query to the students \ n "); return; }
}
Printf (" information is as follows: \ n ");
Printf (" student id \ \ t t name telephone \ \ t t \ \ t t mailbox address birthday (year month day) \ n ");
Printf (" % d % s % s % d % d % d % s % s \ n ", p - & gt; Num, p - & gt; The name, p - & gt; Call, p - & gt; Email, p - & gt; Address, p - & gt; B.y ear, p - & gt; B.m onth, p - & gt; B.d ay);
Printf (" the end of input: 1 to continue. \ n2. \ n ");
The scanf (" % d ", & amp; J);
If (j==1)
return;
The else
Baymanu ();
}
After input student information to create a linked list, query the first students is no problem, but if the student would be an error, after the query hint memory is unreadable, novice and timber, always can't find the wrong, hope each great god grant instruction

CodePudding user response:

Fyi:
//without header node singly linked list 
#include
#include
#include
#include
#include
Struct NODE {
The int data;
Struct NODE * next;
Q} * head, * p, * and * s * (p1, p2, * q1, * * ta.
Int I, k, n, m, v, t, n=10;
Int main () {
The setlocale (LC_ALL, "CHS");
Srand (time (NULL));

The head=NULL;

Printf (", "create % d a singly linked list of nodes, N);//create a N a singly linked list of nodes
P=the head;
for (i=0; iQ=(struct NODE *) malloc (sizeof (struct NODE));
If (NULL==q) exit (1);
Q - & gt; data=https://bbs.csdn.net/topics/rand () % 100;//fill in 0.. 99 the random value of
Q - & gt; next=NULL;
{if (NULL==p)
The head=q;
P=the head;
} else {
P - & gt; Next=q;
P=q;
}
}

//output the singly linked list
S=the head;
While (1) {
If (NULL==s) {
printf("\n");
break;
}
Printf (" % 02 d - & gt;" That s - & gt; data);
S=s - & gt; next;
}

K=3;
V=5;
Printf (" the value for % d node is inserted into the first % d a singly linked list node before: ", v, k);//the value of v is inserted into a singly linked list of nodes before the first k node
N=0;
P=the head;
While (1) {
{if (NULL==p)
break;
}
n++;
If (k==1) {
Q=(struct NODE *) malloc (sizeof (struct NODE));
If (NULL==q) exit (1);
Q - & gt; data=https://bbs.csdn.net/topics/v;
Q - & gt; next=head;
The head=q;
break;
} else {
If (k - 1==n) {
Q=(struct NODE *) malloc (sizeof (struct NODE));
If (NULL==q) exit (1);
Q - & gt; data=https://bbs.csdn.net/topics/v;
Q - & gt; Next=p - & gt; next;
P - & gt; Next=q;
break;
}
}
P=p - & gt; next;
}

//output the singly linked list
S=the head;
While (1) {
If (NULL==s) {
printf("\n");
break;
}
Printf (" % 02 d - & gt;" That s - & gt; data);
S=s - & gt; next;
}

K=5;
Printf (" to delete the % d node: ", k);//remove the first k node
N=0;
P=the head;
While (1) {
{if (NULL==p)
break;
}
n++;
If (k==1) {
Q=head;
The head=head - & gt; next;
Free (q);
break;
} else {
If (k - 1==n) {
Q=p - & gt; next;
If (q) {
P - & gt; Next=q - & gt; next;
Free (q);
}
break;
}
}
P=p - & gt; next;
}

//output the singly linked list
S=the head;
While (1) {
If (NULL==s) {
printf("\n");
break;
}
Printf (" % 02 d - & gt;" That s - & gt; data);
S=s - & gt; next;
}

Printf (" smallest to: ");//smallest
For (p=head, p1=NULL; P!=NULL; P1=p, p=p - & gt; Next) {
For (q=p - & gt; Next, q1=p; q!=NULL; Q1=q, q=q - & gt; Next) {
If (p - & gt; The data & gt; Q - & gt; Data) {

//exchange data
//printf (" \ n 02 02 swap % d % d ", p - & gt; The data, the q - & gt; data);
//t=p - & gt; The data; P - & gt; data=https://bbs.csdn.net/topics/q-> data; Q -> data=t;

//or

//exchange next
//printf (" \ n 02 02 swap % d % d ", p - & gt; The data, the q - & gt; data);
If (p==head) {//p is head
If (p - & gt; Next==q) {//pq next to
The head=q;
P - & gt; Next=q - & gt; next;
Q - & gt; Next=p;
Q=p;
P=the head;
} else {//pq not next to
The head=q;
The p2=p - & gt; next;
P - & gt; Next=q - & gt; next;
Q - & gt; Next=p2;
Q1 - & gt; Next=p;
Q=p;
P=the head;
}
} else {//p not head
If (p - & gt; Next==q) {//pq next to
P1 - & gt; Next=q;
P - & gt; Next=q - & gt; next;
Q - & gt; Next=p;
Q=p;
P=p1 - & gt; next;
} else {//pq not next to
P1 - & gt; Next=q;
The p2=p - & gt; next;
P - & gt; Next=q - & gt; next;
Q - & gt; Next=p2;
Q1 - & gt; Next=p;
Q=p;
P=p1 - & gt; next;
}
}

//output the singly linked list
//s=head;
//the while (1) {
//if (NULL==s) {
//printf (" \ n ");
//break;
//}
02//printf (" % d - & gt;" That s - & gt; data);
//s=s - & gt; next;
//}
//getchar ();
}
}
}

//output the single chain table and calculate the length of the table n
N=0;
S=the head;
While (1) {
If (NULL==s) {
printf("\n");
break;
}
Printf (" % 02 d - & gt;" That s - & gt; data);
n++;
S=s - & gt; next;
}

Printf (" the entire list reverse: ");//the whole list reverse
If (n>=2) {
P=the head;
Q=p - & gt; next;
P - & gt; next=NULL;
While (1) {
Q1=q - & gt; next;
Q - & gt; Next=p;
P=q;
Q=q1;
If (NULL==q) break;
}
The head=p;
}

//output the singly linked list
S=the head;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related