Template class
LinkList.h single Linked list
#ifndef link_list_hxx
#define Link_list_hxx
#include <iostream>
using namespace Std;
Template<class t>
struct Node
{
T data;
Node * NEXT;
};
Template<class t>
Class Linklist
{
Public
Linklist ();//non-parametric constructor to create an empty list with only the head node
Linklist (T a[],int n);//With a parametric constructor, a single-linked list of n elements is established
~linklist ();//destructor
int length ();//Find single linked list lengths
T Get (int i);//Bitwise lookup to find the element value of the I node in a single-linked list
int Locate (T x); Find by value, find the ordinal of an element with a value of x in a single linked list
void Insert (int i,t x);//insert operation, insert the node with the element value x in the first place
T Delete (int i);//delete operation, delete the I node in a single linked list
void Printlist ();//traversal operation, sequentially outputting each element in sequence
Private
Node<t> * first;//single-linked table head pointer
};
Template<class t>
Linklist<t>::linklist ()
{
First=new node<t>;//Generating Head nodes
first->next=null;//head node pointer empty
}
/*
Template<class t>
Linklist<t>::linklist (T a[],int N)//head interpolation to establish a single-linked list linklist
{
First=new node<t>;
first->next=null;//initialization of an empty list
for (int i=0;i<n;i++)
{
Node<t> *s=new node<t>;
s->data=a[i];//Create a node for each element.
s->next=first->next;
first->next=s;//node S is inserted into the head node.
}
}
*/
Template<class t>
Linklist<t>::linklist (T a[],int N)//tail interpolation method to establish a single-linked list linklist
{
First=new node<t>;
Node<t> *r=new node<t>;
R=first;
for (int i=0;i<n;i++)
{
Node<t> *s=new node<t>;
s->data=a[i];//Create a node for each element.
r->next=s;//node S is inserted after the endpoint
R=s;
}
r->next=null;//the single-linked list is complete and the pointer to the endpoint is empty
}
Template<class t>
Linklist<t>::~linklist ()
{
while (First!=null)//release storage space for each node of the single-linked list
{
Node<t> *q=new node<t>;
q=first;//staging the Released node
First=first->next;//first point to the next node of the released node
Delete q;
}
}
Template<class t>
int Linklist<t>::length ()
{
Node<t> *p=first->next;
int count=0;//working pointer p and accumulator count initialization
while (P!=null)
{
p=p->next;
count++;
}
Return count;//Note the relationship between the initialization of count and the return value
}
Template<class t>
T linklist<t>::get (int i)
{
Node<t> *p=first->next;
int count=1;
while (P!=null&&count<i)
{
p=p->next;
count++;
}
if (p==null) throw "position error";
else return p->data;
}
Template<class t>
int linklist<t>::locate (T x)
{
Node<t> *p=first->next;
int count=1;
while (P!=null)
{
if (p->data==x) return count;//find successful, end return bit number
p=p->next;
count++;
}
Return 0;//returns 0 indicates a lookup failure
}
Template<class t>
void Linklist<t>::insert (int i,t x)
{
Node<t> *p=first->next;//work pointer points to head node
int count=1;
while (p!=null&&count<i-1)//Find the first i-1 node
{
p=p->next;//work pointer moves back
count++;
}
if (p==null) throw "position";//No i-1 node found
else{
Node<t> *s=new node<t>;
s->data=x;
s->next=p->next;//node S is inserted after node p.
p->next=s;
}
}
Template<class t>
T linklist<t>::D elete (int i)
{
Node<t> *p=new node<t>;
p=first->next;
int count=1;
while (p!=null&&count<i-1)
{
p=p->next;
count++;
}
if (p==null| | P->next==null) throw "position";//node p does not exist or P points to the next node does not exist
else{
Node<t> *q=new node<t>;
q=p->next;
T x=p->data;
p->next=q->next;//Picking Chain
Delete q;
return x;
}
}
Template<class t>
void Linklist<t>::P rintlist ()
{
Node<t> *p=first->next;
while (P!=null)
{
cout<<p->data<<endl;
p=p->next;
}
}
#endif
Test
LinkListDemo.cpp
#include <iostream>
#include "LinkList.h"
using namespace Std;
int main ()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
linklist<int> linklist (a,10);
cout<< "Raw single-linked list data" <<endl;
Linklist.printlist ();
cout<< "Total Data" <<linklist.length () <<endl;
Linklist.delete (5);
cout<< "post-deletion data" <<endl;
Linklist.printlist ();
cout<< "Total Data" <<linklist.length () <<endl;
Linklist.insert (5,108);
cout<< "post-insert data" <<endl;
Linklist.printlist ();
cout<< "Total Data" <<linklist.length () <<endl;
cout<< "Query data" <<linklist.get <<endl;
cout<< "Query Location" <<linklist.locate (108);
return 0;
}
C + + data structure Learning II (single linked list)