C++ USES templates to implement an instance of List
- 2020-05-19 05:24:57
- OfStack
C ++ 1 List written using templates
template<class T>
class List
{
private:
struct Node
{
T data;
Node *next;
};
//head
Node *head;
//size
int length;
//process
Node *p;
//temp
Node *q;
public:
List()
{
head = NULL;
length = 0;
p = NULL;
}
void add(T t)
{
if(head == NULL)
{
q = new Node();
q->data = t;
q->next = NULL;
length ++ ;
head = q ;
p = head;
}
else
{
q = new Node();
q->data = t;
q->next = NULL;
length ++;
p -> next = q;
p = q;
}
}
void remove(int n)
{
if(n >= length )
{
return;
}
length -- ;
// Delete header node
if(n == 0)
{
q = head ;
head = head -> next;
delete(q);
}
else
{
q = head;
for(int i = 0 ; i < n-1 ; i++)
{
q = q -> next;
}
Node *t = q ->next;
q->next = q->next ->next;
delete(t);
}
//
p = head;
if (p != NULL)
{
while(p->next != NULL)
{
p = p->next;
}
}
}
int getSize()
{
return length;
}
int getLength()
{
return getSize();
}
T get(int n)
{
q = head;
for (int i = 0 ;i < n ; i++)
{
q = q->next;
}
return q->data;
}
};
This is called as follows
List<Stu>list;
Stu stu1;
Stu stu2;
Stu stu3;
stu1.username = "1";
stu2.username = "2";
stu3.username = "3";
list.add(stu1);
list.remove(0);
list.add(stu2);
list.add(stu3);
for (int i = 0 ;i < list.getSize() ; i ++)
{
cout << list.get(i).username;
}
Thank you for reading, I hope to help you, thank you for your support of this site!