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>
struct LinkNode{
  T data;
  LinkNode<T> *link;
  LinkNode(LinkNode<T> *ptr=NULL){link=ptr;}
  LinkNode(const T& item, LinkNode<T> *ptr=NULL){data=item; link=ptr;}
};
 
template<class T>
class List{
public:
  List(){first=new LinkNode<T>;}
  List(const T& x){first=new LinkNode<T>(x);}
  List(List<T> &L);
  ~List(){makeEmpty();}
  void makeEmpty();
  int Length()const;
  LinkNode<T> *getHead()const{return first;}
  LinkNode<T> *Search(T x);
  LinkNode<T> *Locate(int i);
  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 IsEmpty()const{return (first->link==NULL)?true:false;}
  bool IsFull()const{ return false;}
  void Sort();
  void inputFront(T endTag);
  void inputRear(T endTag);
  void output();
  List<T>& operator=(List<T> &L);
private:
  LinkNode<T> *first;
};
 
template<class T>
void List<T>::makeEmpty(){
  //if(first->link==NULL)return;
  LinkNode<T> *p=first->link;
  while(p!=NULL){
   first->link=p->link;
   delete p;
   p=first->link;
  }
}
 
template<class T>
LinkNode<T> *List<T>::Search(T x){
  LinkNode<T> *p=first->link;
  while(p!=NULL){
   if(p->data==x)break;
   p=p->link;
  }
  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>
LinkNode<T> *List<T>::Locate(int i){
  //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;
  LinkNode<T> *p=first;
  while(p!=NULL&&sum<i){
   sum++;
   p=p->link;
  }
  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;
  LinkNode<T> *p=Locate(i);
  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;
  LinkNode<T> *p=Locate(i);
  if(p==NULL)return;
  else{
   p->data=x;
  }
}
 
template<class T>
bool List<T>::Insert(int i, T &x){
   //LinkNode<T> *pre=Locate(i-1);
   //This is after the i-th element
   LinkNode<T> *cur=Locate(i);
   if(cur==NULL)return false;
   LinkNode<T> *p=new LinkNode<T>(x);
   If (P = = null) {cerr < "memory allocation error!" < < endl; exit (1);}
   //if(pre==NULL||cur==NULL||p==NULL)return false;
   else{
     p->link=cur->link;
     cur->link=p;
     return true;
   }
}
 
template<class T>
bool List<T>::Remove(int i, T& x){
  //Delete the element at position I
  LinkNode<T> *pre=Locate(i-1);
  if(pre==NULL)return false;
  LinkNode<T> *current=pre->link;
  if(current==NULL)return false;
  x=current->data;
  pre->link=current->link;
  delete current;
  return true;
}
 
template<class T>
void List<T>::output(){
  LinkNode<T> *current=first->link;
  while(current!=NULL){
   cout<<current->data<<" ";
   current=current->link;
  }
}
 
template<class T>
List<T>& List<T>::operator=(List<T>& L){
  //This is the assignment method
  LinkNode<T> *srcptr=L.getHead(), *p=srcptr->link;
  LinkNode<T> *desptr=first=new LinkNode<T>;
  T value;
  while(p!=NULL){
   value=p->data;
   desptr->link=new LinkNode<T>(value);
   desptr=desptr->link;
   p=p->link;
  }
  return *this;
  //This method can be used to better realize the assignment
//  LinkNode<T> *pre=L.getHead();
//  if(pre==NULL){
//   first=NULL;
//   return *this;
//  }
//  LinkNode<T> *p=first=new LinkNode<T>;
//  first->link=p;
//  int sum=L.Length();
//  T &x;
//  int i=1;
//  while(i<=sum){
//   L.getData(i++,x);
//   p=new LinkNode<T>(x);
//   p=p->link;
//  }
//  return *this;
 
}
 
template<class T>
int List<T>::Length()const{
  int sum=0;
  LinkNode<T> *p=first->link;
  while(p!=NULL){
   sum++;
   first->link=p->link;
   delete p;
   p=first->link;
  }
  return sum;
}
 
 
//Establishing single linked list by forward interpolation
template<class T>
void List<T>::inputFront(T endTag){
  LinkNode<T> *newNode;
  T value;
  makeEmpty();
  cin>>value;
  while(value!=endTag){
   newNode=new LinkNode<T>(value);
   If (newnode = = null) {cerr < "memory allocation error! "<<endl; exit(1);}
   newNode->link=first->link;
   first->link=newNode;
   cin>>value;
  }
}
 
//Establishing single linked list by post interpolation
template<class T>
void List<T>::inputRear(T endTag){
  LinkNode<T> *newNode=new LinkNode<T>, *last;
  T value;
  last=first=new LinkNode<T>;
  cin>>value;
  while(value!=endTag){
   newNode=new LinkNode<T>(value);
   if(newNode==NULL){cerr<<""<<endl;exit(1);}
   last->link=newNode;
   last=newNode;
   cin>>value;
  }
}
 
//Copy constructor
template<class T>
List<T>::List(List<T> &L){
  //Copy constructor
  T value;
  LinkNode<T> *srcptr=L.gethead(), p=srcptr->link;
  LinkNode<T> *desptr=first->link=new LinkNode<T>;
  while(p!=NULL){
   value=p->data;
   desptr=new LinkNode<T>(value);
   desptr=desptr->link;
   p=p->link;
  }
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.