Home > Back-end >  Parameter when the transfer application space?
Parameter when the transfer application space?

Time:10-20

 
#include
using namespace std;
Template
The class List {
Public:
The List ();
Void the Add (T & amp;);//in the Link header to add a new node
Void the Remove (T & amp;);//delete contains a specific value in the Link element
T * Find (T&);//find contains a specific value of node
Void PrintList ();//print out the entire list
To the List ();
Protected:
Struct Node {
Node * pNext;//points to the next node
T * pT;//points to the current node content
};
Node * pFirst;//chain first node pointer
};
Template
List : : List () {
PFirst=NULL;
}
Template
Void List : : Add (T & amp; Pt) {
Node * temp=new Node;
Temp - & gt; PT=& amp; pt;
Temp - & gt; PNext=pFirst;//the head pointer to the new node pNext
PFirst=temp;//head pointer to point to the new node
//delete temp.
}
Template
Void List : : Remove (T & amp; Pt) {
Node * p=0;
If (* (pFirst - & gt; PT)==pT) {//first to determine whether to delete the head node, if so, the first node to take off the chain
P=pFirst;
PFirst=pFirst - & gt; PNext;
}
The else {//search, take off the chain
For (Node * q=pFirst; Q - & gt; PNext; Q=q - & gt; PNext)
If (* (q - & gt; PNext - & gt; PT)==pT) {
P=q - & gt; PNext;
Q - & gt; PNext=p - & gt; PNext;
break;
}
}
{if (p) delete [] p - & gt; PT; The delete [] p; }
The else cout<& lt;" Not finding! \ n ";
}
Template
T * List : : Find (T & amp; Pt) {
For (Node * p=pFirst; p; P=p - & gt; PNext)
If (* (p - & gt; PT)==pT) return p - & gt; PT;
return 0;
}
Template
Void List : : PrintList () {
For (Node * p=pFirst; p; P=p - & gt; Cout< pNext); <* (p - & gt; PT) & lt; & lt;" \ t ";
Cout}
Template
List : : ~ a List () {
Node * p;
while(! PFirst) {
P=pFirst;
PFirst=pFirst - & gt; PNext;
The delete [] p - & gt; PT;
The delete [] p;
}
}
Int main () {
List list;
Cout<& lt;" Please enter the list contents, end of 0 \ n ";
for(int i=0; i<7. I++)
List. The Add (* new float (I + 0.4));
//so why not use the following code?
/* float I;
While (1) {
Cin> i;
if(! I) break;
List. Add (I);
}
*/

List. PrintList ();
return 0;
}



in addition, also want to ask, this program in the function of dynamic application space seems to be not released, will cause the memory leak?

CodePudding user response:

(above 75 lines of code, the forehead,,, should be only text, novice can't post, so,,, ignore ~ ~)
Program is written, so the main function might not use list some function
(the couple, hope everybody a great god glad ah ~ ~)
  • Related