Home > Back-end >  Bosses give directions, data structures and algorithms
Bosses give directions, data structures and algorithms

Time:10-21

CodePudding user response:

Void insertList (DLNode * p, ElementType x) {
If (NULL==p) return;
DLNode * pT=(DLNode *) malloc (sizeof (DLNode));
PT - & gt; Data=https://bbs.csdn.net/topics/x;
PT - & gt; The prior=p - & gt; The prior;
PT - & gt; Next=p;
P - & gt; The prior=pT;
}

CodePudding user response:




 void insertList (DLNode * p, ElementType x) {
If (NULL==p) return;
DLNode * pT=(DLNode *) malloc (sizeof (DLNode));
if (! PT)
return NULL;
PT - & gt; Data=https://bbs.csdn.net/topics/x;
PT - & gt; The prior=p - & gt; The prior;
PT - & gt; Next=p;
P - & gt; The prior=pT;
}

Add 1 to pT application space after judgment,
In addition, there is a problem in this function, if p is the first node, then the function can also can? P needs to be considered is the location of the head or middle, or end,

Fun
reference 1 floor response:
void insertList (DLNode * p, ElementType x) {
If (NULL==p) return;
DLNode * pT=(DLNode *) malloc (sizeof (DLNode));
PT - & gt; Data=https://bbs.csdn.net/topics/x;
PT - & gt; The prior=p - & gt; The prior;
PT - & gt; Next=p;
P - & gt; The prior=pT;
}

CodePudding user response:

No end node, the prior of p or next node set up properly, can be dealt with in the caller
If you want to be a more general, to transform, had returned to

DLNode * insertList (DLNode * p, ElementType x) {
If the return NULL (NULL==p);
DLNode * pT=(DLNode *) malloc (sizeof (DLNode));
If (NULL==pT) return NULL;
PT - & gt; Data=https://bbs.csdn.net/topics/x;
PT - & gt; The prior=p - & gt; The prior;
PT - & gt; Next=p;
P - & gt; The prior=pT;
Return pT;
}

Put insert position the effectiveness of the detection function, still in the calling before testing each have each benefits
Outside requires users to more clearly what you want to do, can reduce internal judgment function, improve the efficiency of execution of the function itself
Inside the function, the function can be more robust
  • Related