# Construction of single linked list with C + +

Time：2021-1-16

This example for you to share the C + + single linked list construction code, for your reference, the specific content is as follows

Construction of single linked list, including the most commonly used functions, setdata(), insert(), remove(), getdata(), search().

The code is as follows:

``````#include <iostream>
#include <stdlib.h>

using namespace std;

template<class T>
T data;
};

template<class T>
class List{
public:
List(const T& x){first=new LinkNode<T>(x);}
List(List<T> &L);
~List(){makeEmpty();}
void makeEmpty();
int Length()const;
bool getData(int i, T &x)const;
void setData(int i,T &x);
bool Insert(int i,T &x);
bool Remove(int i, T &x);
bool IsFull()const{ return false;}
void Sort();
void inputFront(T endTag);
void inputRear(T endTag);
void output();
List<T>& operator=(List<T> &L);
private:
};

template<class T>
void List<T>::makeEmpty(){
while(p!=NULL){
delete p;
}
}

template<class T>
while(p!=NULL){
if(p->data==x)break;
}
Return p; // returns P regardless of whether it is found or not. If it is found, it returns P. if it is not found, it returns a null pointer
}

template<class T>
//This location function is very important for later functions to locate the corresponding node according to I
if(i<0)return NULL;
int sum=0;
while(p!=NULL&&sum<i){
sum++;
}
Return p; // no matter whether it is a null pointer or not, the pointer that reaches the I position is returned. If it does not reach the I position, it is the end
}

template<class T>
bool List<T>::getData(int i, T& x)const{
if(i<0)return false;
if(p==NULL)return false;
else{
x=p->data;
return true;
}
}

template<class T>
void List<T>::setData(int i, T& x){
if(i<0)return;
if(p==NULL)return;
else{
p->data=x;
}
}

template<class T>
bool List<T>::Insert(int i, T &x){
//This is after the i-th element
if(cur==NULL)return false;
If (P = = null) {cerr < "memory allocation error!" < < endl; exit (1);}
//if(pre==NULL||cur==NULL||p==NULL)return false;
else{
return true;
}
}

template<class T>
bool List<T>::Remove(int i, T& x){
//Delete the element at position I
if(pre==NULL)return false;
if(current==NULL)return false;
x=current->data;
delete current;
return true;
}

template<class T>
void List<T>::output(){
while(current!=NULL){
cout<<current->data<<" ";
}
}

template<class T>
List<T>& List<T>::operator=(List<T>& L){
//This is the assignment method
T value;
while(p!=NULL){
value=p->data;
}
return *this;
//This method can be used to better realize the assignment
//  if(pre==NULL){
//   first=NULL;
//   return *this;
//  }
//  int sum=L.Length();
//  T &x;
//  int i=1;
//  while(i<=sum){
//   L.getData(i++,x);
//  }
//  return *this;

}

template<class T>
int List<T>::Length()const{
int sum=0;
while(p!=NULL){
sum++;
delete p;
}
return sum;
}

//Establishing single linked list by forward interpolation
template<class T>
void List<T>::inputFront(T endTag){
T value;
makeEmpty();
cin>>value;
while(value!=endTag){
If (newnode = = null) {cerr < "memory allocation error! "<<endl; exit(1);}
cin>>value;
}
}

//Establishing single linked list by post interpolation
template<class T>
void List<T>::inputRear(T endTag){
T value;
cin>>value;
while(value!=endTag){
if(newNode==NULL){cerr<<""<<endl;exit(1);}
last=newNode;
cin>>value;
}
}

//Copy constructor
template<class T>
List<T>::List(List<T> &L){
//Copy constructor
T value;
while(p!=NULL){
value=p->data;