Home > Back-end >  Write templates list class but head node cannot be accessed in the subclass
Write templates list class but head node cannot be accessed in the subclass

Time:09-22

Why my head node in a singly linked list c + + class in (? ^? )
When I wrote the public inheritance, head node is protection type definition, but can't access in subclasses
May be a mind is a little bit chaotic didn't found the problem, can please kind people help me have a look?



#include

using namespace std;

//node definition phi (& gt; Omega & lt; *)
The template & lt; The class type>
The class ListNode//node
{

Public:
ListNode ()//create empty nodes or the default constructor
{next=NULL; }
ListNode (const type & amp; Item, ListNode & lt; Type> * next1=NULL)
{
Data=https://bbs.csdn.net/topics/item;//item for reference can be a return value??
Next=next1;//next initial blank
}
Type data.//the list after class has the direct manipulation of Pointers and data, so set to public
ListNode & lt; Type> * next;
};


The template & lt; The class type>
The class ablinklist//abstract class list
{
Protected:
int length;//the list length
ListNode * the head; The definition of//head node
Public:
//int length;//the list length
//ListNode * the head;



ListNode & lt; Type> * GetHead ()//get head node pointer
{//understand, function type is node pointer type, return pointer
return head;
}
ListNode & lt; Type> * GetNext (ListNode & N)
{
Return n.n ext==head? N.n ext - & gt; Next: n.n ext.//if n nodes is returned head to head the next
/* for circular linked list? *///if n is not pointing to the first node is returned his next node address
}
Type the Get (int I);//remove the ith node of the element
ListNode * Find (int I);//find the ith node
ListNode * Find1 (type value);//value value to search
Bool Set (type x, int I);//to the ith element set values in the table
Void MakeEmpty ();//set to an empty table
Virtual bool Insert (type value, int I)=0.//insert element to a location
Virtual type Remove (int I)=0;//delete node
IVirtual type Remove1 (type value)=0;//delete the value of the node (have more than one? Improve the?)
/* dangdang dangdang ~ ~ ~ ~ ~ ~ all the template definition finish see his left (* ^ ^ *) */
};
The template & lt; The class type>
Void ablinklist : : MakeEmpty ()//clear function
{
int i=1;
ListNode * q=head - & gt; next;
While (i++ & lt;=length)
{
The head - & gt; Next=q - & gt; next;
The delete q;
Q=head - & gt; next;
}
Length=0;
}
The template & lt; The class type>
Bool ablinklist : : Set (type x, int I)//Set value function
{
ListNode * p=Find (I);
If (p==NULL | | p==head)//I in accordance the Find returns NULL or head
return false;
The else
p-> Data=https://bbs.csdn.net/topics/x;
return true;

}
The template & lt; The class type>
ListNode * ablinklist : : Find1 (value) type//find value function
{
Int j=1;
ListNode * p=head - & gt; next;
For (j=1; J & lt;=length& & p-> The data!=value; J++)
{
P=p - & gt; next;
}
Return the p;
}
The template & lt; The class type>
ListNode * ablinklist : : Find (int I)//lookup function
{
Int j=1;
If (I & lt; 0 | | I & gt; Length)
return NULL;
Else if (I==0)
return head;
ListNode * p=head - & gt; next;
For (j=1; J & lt; I; J++)
{
P=p - & gt; next;
}
Return the p;
}
The template & lt; The class type>
Type ablinklist : : Get (int I)//function values
{
ListNode * p=Find (I);
Assert (p & amp; & P!=head);//assume that prevent run compulsory, automatically generate debugging information
If (p==NULL | | p==head)
return false;//confusion, to custom?
The else
The return p - & gt; The data;
}




/* write the specific definition of singly linked lists below car do cry */
The template & lt; The class type>
Class List: public ablinklist//z the inside cannot access to the head and length
//the problem * * * * * * * * * * *
{
Public:
The List ()//constructs an empty List
{
The head=new ListNode ;
Length=0;
}
The List (List & lt; Type> & L)//copy of an existing list
{the Copy (l); }
To the List ()
{MakeEmpty (); }//have borrow have also
List & Copy (the List & lt; Type> & L);//copy function reference type??????
Type the Remove (int I);//s I delete nodes
Bool Insert (type value, int I);//the new element is inserted into the node I * * * *
the establishment of the most important functionType the Remove (value) type;//delete the value of the node (have more than one? Improve the?)
};
The template & lt; The class type>
Type a List & lt; Type> : : Remove (type value)
{
ListNode * q, * p=head;
While (p - & gt; Next!=NULL& & p-> Next - & gt; The data!=value)
{
P=p - & gt; next;//to find the value of the previous node
}
If (p - & gt; Next==NULL)
return false;//what ErrData
Q=p - & gt; next;
p-> Next=q - & gt; next;//to have
The delete q;
Length -;
return value;
}
The template & lt; The class type>
Bool List & lt; Type> : : Insert (type value, int I)//Insert node
{
ListNode & lt; Type> * p=Find (I - 1);//locate
If (p==NULL)//judge
return false;
//set up the new node
ListNode & lt; Type> * newnode=new ListNode & lt; Type> (value, p - & gt; Next);
Assert (newnode);
If (newnode==NULL)
return false;//memory allocation is successful?
p-> Next=newnode;
Length + +;
return true;

}
The template & lt; The class type>
Type a List & lt; Type> : : Remove (int I)//delete node
Inullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related