# include
#include
#include
Typedef int ElemType;
Typedef struct Node
{
ElemType data;
Struct Node * next;
} Node, * LinkList;
InitList (LinkList * L)
{
* L=(*) malloc (sizeof (Node));
If (L==NULL)
{
Printf (" \ n application memory space failure ");
}
(* L) - & gt; Next=NULL;
}
Void CreateFromHead (LinkList L, int n)//head interpolation insert
{
The Node * p * q * s;
Int I, j=1;
p=L;
S=(*) malloc (sizeof (Node));
S - & gt; data=https://bbs.csdn.net/topics/n;
for(i=1; n>=p - & gt; Next - & gt; data; I++)//cycle, to find the first nodes with more than new
P=p - & gt; Next;//cycle pointer back at the end of the cycle, I recorded the first is greater than the nodes of the new serial number
p=L;//p to head node
While (j<=I)//cycle again, find the biggest node is smaller than the value of the new node address
{
Q=p; P=p - & gt; Next;//q record precursor, p record subsequent
j++;//change the loop variable
}
S - & gt; Next=q - & gt; Next;//the first target is larger than the new node node even after the new node
q-> Next=s;//connect the new node after the last node in its smaller
}
Locate the Node * (LinkList L, ElemType key)//single table lookup values for the key
{
Node * p;
P=L - & gt; Next;
while(p!=NULL)
If (p - & gt; The data!=key)
P=p - & gt; Next;
else break;
return p;
}
Int DelList (int I LinkList L, ElemType * e)
{
Pre Node * and * r;
Int k;
The pre=L; K=0;
While (pre - & gt; Next!=NULL& & K{
The pre=pre - & gt; Next;
k=k+1;
}
If (pre - & gt; Next==NULL)
{
Printf (" delete node unreasonable ");
return 0;
}
R=pre - & gt; Next;
The pre - & gt; Next=r - & gt; Next;
* e=r - & gt; data;
Free (r);//release the memory space
return 1;
}
Void OutputList (LinkList L)
{
LinkList p=L;
while(p!=NULL)
{
Printf (" % d ", p - & gt; data);
P=p - & gt; Next;
}
Printf (" \ n \ n ");
}
Int main ()
{
LinkList L, start;//head=NULL;
Int num, I, da, k;
//insert them one by one to maintain orderly
Printf (" please enter the number of data ");
The scanf (" % d ", & amp; Num);
for(i=0; i{
The scanf (" % d ", & amp; Da);
CreateFromHead (L, da);
}
//to find, if find delete
//output
OutputList (L);
}
CodePudding user response:
learn to debugCodePudding user response:
L, q pointer not initializedCodePudding user response:
# include
#include
#include
Typedef int ElemType;
Typedef struct Node
{
ElemType data;
Struct Node * next;
} Node, * LinkList;
//InitList (LinkList * L)
Void InitList (LinkList * L)
{
* L=(*) malloc (sizeof (Node));
If (L==NULL)
{
Printf (" \ n application memory space failure ");
}
(* L) - & gt; Next=NULL;
}
Void CreateFromHead (LinkList L, int n)//head interpolation insert
{
The Node * p * q * s;
Int I, j=1;
p=L;
S=(*) malloc (sizeof (Node));
if (! S)//safety
exit(0);
S - & gt; Data=https://bbs.csdn.net/topics/n;
//for (I=1; n>=p - & gt; Next - & gt; data; I++)//cycle, to find the first nodes with more than new
for(i=1; P - & gt; Next & amp; & n>=p - & gt; Next - & gt; data; I++)//cycle, to find the first nodes with more than new
P=p - & gt; Next;//cycle pointer back at the end of the cycle, I recorded the first is greater than the nodes of the new serial number
P=L;//p to head node
While (j & lt;=I)//cycle again, find the biggest node is smaller than the value of the new node address
{
Q=p;
P=p - & gt; Next;//q record precursor, p record subsequent
j++;//change the loop variable
}
S - & gt; Next=q - & gt; Next;//the first target is larger than the new node node even after the new node
q-> Next=s;//connect the new node after the last node in its smaller
}
//the Node * Locate (LinkList, L ElemType key)//single table lookup value is the key value
Locate the Node * (LinkList L, ElemType key)//single table lookup value is the key Node
{
Node * p;
P=L - & gt; Next;
while(p!=NULL)
If (p - & gt; The data!=key)
P=p - & gt; Next;
The else
break;
return p;
}
Int DelList (int I LinkList L, ElemType * e)
{
Pre Node * and * r;
Int k;
The pre=L; K=0;
While (pre - & gt; Next!=NULL& & K{
The pre=pre - & gt; Next;
k=k+1;
}
If (pre - & gt; Next==NULL)
{
Printf (" delete node unreasonable ");
return 0;
}
R=pre - & gt; Next;
The pre - & gt; Next=r - & gt; Next;
* e=r - & gt; data;
Free (r);//release the memory space
return 1;
}
Void OutputList (LinkList L)
{
LinkList p=L - & gt; Next;
while(p!=NULL)
{
Printf (" % d ", p - & gt; data);
P=p - & gt; Next;
}
Printf (" \ n \ n ");
}
Int main ()
{
LinkList L, start;//head=NULL;
Int num, I, da, k;
//insert them one by one to maintain orderly
Printf (" please enter the number of data ");
The scanf (" % d ", & amp; Num);
InitList (& amp; L);//this step, fewer defines why don't you call?
for(i=0; i{
The scanf (" % d ", & amp; Da);
CreateFromHead (L, da);
}
//to find, if find delete
//output
OutputList (L);
}
For your reference ~
Compare the code to find your own problem ~
CodePudding user response: