Linear list chain structure

When I first started learning linear tables, I found a lot of templates with linear table chain structures, but they didn’t fit my eyes, so I took a lot of detours. At that time, I wanted to record it. When I got up, I wrote an article in c++, just record it.

No-argument constructor:

generate head node

template<typename DataType>
LinkList<DataType>::LinkList()
{
    
    
    head=new Node<DataType>;
    
}

destructor

Used to release the node space in the singly linked list

template<typename DataType>
LinkList<DataType>::~LinkList()
{
    
    
    while(head!=NULL)
	{
    
    
		Node<DataType> *p=head;
		head=head->next;
		delete p;
	 } 
}

Build singly linked list

There are "head plugging method" and "tail plugging method". Here, head plugging is used.

template<typename DataType>
void LinkList<DataType>::creatList(int n)
{
    
    
    Node<DataType> *pre=head;
   	int i=0;
	for(i=0;i<n;i++)
	{
    
    
		Node<DataType> *p=new Node<DataType>;
		cout<<"Please intput the NO."<<i+1<<' '<<"Student's nums,name and chore:" <<' ';
		cin>> p->nums >>p->name >>p->score ;
		pre->next =p;
		pre=p;
		p->next =NULL;		
	}
}

print singly linked list

template<typename DataType>
void LinkList<DataType>::printList()
{
    
    
    Node<DataType> *p=head->next;
     while(p!=NULL)
	{
    
    
		cout<<p->nums<<' ' <<p->name<<' '<<p->score<<endl ;
		p=p->next ;
	}
}

insert node

template<typename DataType>
void LinkList<DataType>::insert()
{
    
    
    Node<DataType> *p1,*p2;
	p1=head->next ;
	Node<DataType> *p0=new Node<DataType>;
    cout<<"Please input you will insert the data of Student:"<<' ';
    cin>>p0->nums>>p0->name>>p0->score;
	while((p0->nums >p1->nums) &&(p1->next !=NULL))
	{
    
    
		p2=p1;
		p1=p1->next ;
		
	}
	if(p0->nums <=p1->nums )
	{
    
    
		if(head->next==p1){
    
    
		head->next=p0;
		p0->next=p1;}
		else p2->next =p0;
		p0->next =p1;
		
	}
	else{
    
    
		p1->next =p0;p0->next =NULL;
	}
}

delete node

template<typename DataType>
void LinkList<DataType>::delet()
{
    
    
    cout<<"Please intput the Student's nums that you will delete:"<<' ';
    int m;
    cin>>m;
    Node<DataType>*p1,*p2;
	p1=head;
	while(m!=p1->nums&&p1->next !=NULL)
{
    
    
	p2=p1;
	p1=p1->next ;
	}
	if(m==p1->nums )
{
    
    
	if(p1==head)head=p1->next ;
	else p2->next =p1->next ;

}
	else cout<<"the data is erro"<<endl;
}

lookup by value

template<typename DataType>
void LinkList<DataType>::findvalue()
{
    
    
    int m;
    cout<<"Please intput the nums that you will find:"<<' ';
    cin>>m;
    Node<DataType>*p1=head;
    while(p1->nums!=m&&p1->next!=NULL)
    p1=p1->next;
    if(p1->next!=NULL)
    cout<<"This data's address is:"<<p1<<endl;
    else cout<<"This data is erro!"<<endl;
}

bitwise lookup

template<typename DataType>
void LinkList<DataType>::locate()
{
    
    
    int K;
    cout<<"Please intput the serial number of the data you want to query:"<<' ';
    cin>>K;
    Node<DataType>*p=head;
    int i=1;
    while(i!=K&&p!=NULL)
    {
    
    
          p=p->next;
          i++;
    }
    if(i==K)
    cout<<"The digit of the data is:"<<' '<<p->nums<<' '<<p->name<<' '<<p->score<<endl;
    else cout<<"This data is erro"<<endl;

}

Calculate the length of the singly linked list

template<typename DataType>
void LinkList<DataType>::lengths()
{
    
    
    	int i=0;
	Node<DataType> *p=head->next ;
	while(p!=NULL)
	{
    
    
		i++;
		p=p->next ;
	}
	cout<<"Tis List of length is :"<<i<<endl;
}  

full code

#include <iostream>
using namespace std;
template<typename DataType>
struct Node
{
    
    
    DataType nums;
    float score;
    char name[30];
    Node<DataType> *next;
};
template<typename DataType>
class LinkList
{
    
    
    public:
    LinkList();
    void creatList(int n);
    ~LinkList();
    void printList();
    void insert();
    void delet();
    void lengths();
    void findvalue();
    void locate();
    private:
    Node<DataType> *head;

};
template<typename DataType>
LinkList<DataType>::LinkList()
{
    
    
    head=new Node<DataType>;
    
}
template<typename DataType>
LinkList<DataType>::~LinkList()
{
    
    
    while(head!=NULL)
	{
    
    
		Node<DataType> *p=head;
		head=head->next;
		delete p;
	 } 
}
template<typename DataType>
void LinkList<DataType>::creatList(int n)
{
    
    
    Node<DataType> *pre=head;
   	int i=0;
	for(i=0;i<n;i++)
	{
    
    
		Node<DataType> *p=new Node<DataType>;
		cout<<"Please intput the NO."<<i+1<<' '<<"Student's nums,name and chore:" <<' ';
		cin>> p->nums >>p->name >>p->score ;
		pre->next =p;
		pre=p;
		p->next =NULL;		
	}
}
template<typename DataType>
void LinkList<DataType>::printList()
{
    
    
    Node<DataType> *p=head->next;
     while(p!=NULL)
	{
    
    
		cout<<p->nums<<' ' <<p->name<<' '<<p->score<<endl ;
		p=p->next ;
	}
}
template<typename DataType>
void LinkList<DataType>::insert()
{
    
    
    Node<DataType> *p1,*p2;
	p1=head->next ;
	Node<DataType> *p0=new Node<DataType>;
    cout<<"Please input you will insert the data of Student:"<<' ';
    cin>>p0->nums>>p0->name>>p0->score;
	while((p0->nums >p1->nums) &&(p1->next !=NULL))
	{
    
    
		p2=p1;
		p1=p1->next ;
		
	}
	if(p0->nums <=p1->nums )
	{
    
    
		if(head->next==p1){
    
    
		head->next=p0;
		p0->next=p1;}
		else p2->next =p0;
		p0->next =p1;
		
	}
	else{
    
    
		p1->next =p0;p0->next =NULL;
	}
}
template<typename DataType>
void LinkList<DataType>::delet()
{
    
    
    cout<<"Please intput the Student's nums that you will delete:"<<' ';
    int m;
    cin>>m;
    Node<DataType>*p1,*p2;
	p1=head;
	while(m!=p1->nums&&p1->next !=NULL)
{
    
    
	p2=p1;
	p1=p1->next ;
	}
	if(m==p1->nums )
{
    
    
	if(p1==head)head=p1->next ;
	else p2->next =p1->next ;

}
	else cout<<"the data is erro"<<endl;
}
template<typename DataType>
void LinkList<DataType>::findvalue()
{
    
    
    int m;
    cout<<"Please intput the nums that you will find:"<<' ';
    cin>>m;
    Node<DataType>*p1=head;
    while(p1->nums!=m&&p1->next!=NULL)
    p1=p1->next;
    if(p1->next!=NULL)
    cout<<"This data's address is:"<<p1<<endl;
    else cout<<"This data is erro!"<<endl;
}
template<typename DataType>
void LinkList<DataType>::locate()
{
    
    
    int K;
    cout<<"Please intput the serial number of the data you want to query:"<<' ';
    cin>>K;
    Node<DataType>*p=head;
    int i=1;
    while(i!=K&&p!=NULL)
    {
    
    
          p=p->next;
          i++;
    }
    if(i==K)
    cout<<"The digit of the data is:"<<' '<<p->nums<<' '<<p->name<<' '<<p->score<<endl;
    else cout<<"This data is erro"<<endl;

}
template<typename DataType>
void LinkList<DataType>::lengths()
{
    
    
    	int i=0;
	Node<DataType> *p=head->next ;
	while(p!=NULL)
	{
    
    
		i++;
		p=p->next ;
	}
	cout<<"Tis List of length is :"<<i<<endl;
}  
int main()
{
    
    
    LinkList<int>p;
    int n;
    cout<<"Please intput the number of Students:";
    cin>>n;
    p.creatList(n);
    p.printList();
    p.insert();
    cout<<"The new List is:"<<endl;
    p.printList();
    p.delet();
    cout<<"The new List is:"<<endl;
    p.printList();  
    p.findvalue();
    p.locate();
    p.lengths();
    return 0;
    
}

Guess you like

Origin blog.csdn.net/Terminal_ve/article/details/128226218