C + + Template implementation cyclic chain list __c++

Source: Internet
Author: User

The cyclic list is another form of chain-type storage structure. Its characteristic is that the pointer field of the last node in the table points to the head node, and the whole chain forms a ring.

Circular chain List classification:
(1) Single cycle list--in a single linked list, the pointer field null of the terminal node is changed to point to the header node or start node.
(2) Multi-chain cyclic chain List--link the nodes of the table to multiple rings.
Lead node:
The condition of judging the empty list is head==head->next;
Tail pointer:
The A1 of the starting node and the terminal node are all O (1) for the rear of the single cyclic list represented by the tail pointer. But the table's operation is often in the table at the beginning and end position, therefore, in the practical uses the tail pointer to represent the single circulation chain list. A single circular chain with a trailing pointer shows the following figure.
Note: The criteria for judging empty lists are rear==rear->next;
Characteristics:
The characteristics of the circular chain table is that it is not necessary to increase the amount of storage, only a little change of the table link mode, can make table processing more convenient and flexible.

Using C + + templates to implement a simple circular list:

ListNode.h

Template<typename type> class circularlist;

Template<typename type> class listnode{
private:
	friend class circularlist<type>;
	ListNode (): M_pnext (NULL) {}
	ListNode (const Type item,listnode<type> *next=null): M_data (item), M_pnext ( Next) {}
	~listnode () {
		m_pnext=null;
	}
	
Private:
	Type m_data;
	ListNode *m_pnext;
};
CircularList.h

#include "ListNode.h" Template<typename type> class circularlist{public:circularlist (): Head (New listnode<	
	Type> ()) {head->m_pnext=head;
		} ~circularlist () {makeempty ();
	Delete head;	} public:void Makeempty ();		Clear the list int Length ();	Get the length listnode<type> *find (Type value,int N);			Find the nth data which was equal to value listnode<type> *find (int n);			Find the nth data bool Insert (Type item,int n=0);					Insert the data into the nth data of the list Type Remove (int n=0);				Delete the nth data bool RemoveAll (Type item);	Delete the datas which are equal to value Type get (int n);		Get the nth data void Print ();

Print the list private:listnode<type> *head;

};
	Template<typename type> void Circularlist<type>::makeempty () {listnode<type> *pdel,*pmove=head;
		while (Pmove->m_pnext!=head) {pdel=pmove->m_pnext;
		pmove->m_pnext=pdel->m_pnext;
	Delete Pdel; }} TemplAte<typename type> int circularlist<type>::length () {listnode<type> *pmove=head;
	int count=0;
		while (Pmove->m_pnext!=head) {pmove=pmove->m_pnext;
	count++;
return count; } template<typename type> listnode<type>* circularlist<type>::find (int n) {if (n<0) {cout<<
		"The n is out of boundary" <<endl;
	return NULL;
	} listnode<type> *pmove=head->m_pnext;
	for (int i=0;i<n&&pmove!=head;i++) {pmove=pmove->m_pnext;
		} if (Pmove==head) {cout<< "The n is out of boundary" <<endl;
	return NULL;
return pmove; } template<typename type> listnode<type>* circularlist<type>::find (Type value,int N) {if (n<1) {Co
		ut<< "The N is illegal" <<endl;
	return NULL;
	} listnode<type> *pmove=head;
	int count=0;
		while (count!=n) {pmove=pmove->m_pnext;
		if (pmove->m_data==value) {count++; } if (Pmove==head) {cout<< "can ' t find the element" <<Endl
		return NULL;
} return pmove; } template<typename type> bool Circularlist<type>::insert (Type item, int n) {if (n<0) {cout<< "
		N is out of boundary "<<endl;
	return 0;
	} listnode<type> *pmove=head;
	Listnode<type> *pnode=new listnode<type> (item); if (pnode==null) {cout<< "Application error!"
		<<endl;
	Exit (1);
		for (int i=0;i<n;i++) {pmove=pmove->m_pnext;
			if (pmove==head) {cout<< "The n is out of boundary" <<endl;
		return 0;
	}} pnode->m_pnext=pmove->m_pnext;
	pmove->m_pnext=pnode;
return 1; } template<typename type> bool Circularlist<type>::removeall (Type Item) {listnode<type> *pmove=head
	;
	Listnode<type> *pdel=head->m_pnext;
			while (Pdel!=head) {if (Pdel->m_data==item) {pmove->m_pnext=pdel->m_pnext;
			Delete Pdel;
			pdel=pmove->m_pnext;
		Continue
		} pmove=pmove->m_pnext;
	pdel=pdel->m_pnext;
return 1; } tEmplate<typename type> Type circularlist<type>::remove (int n) {if (n<0) {cout<< "can" t find the Elem
		Ent "<<endl;
	Exit (1);
	} listnode<type> *pmove=head,*pdel;
	for (int i=0;i<n&&pmove->m_pnext!=head;i++) {pmove=pmove->m_pnext;
		} if (Pmove->m_pnext==head) {cout<< "can ' t find the element" <<endl;
	Exit (1);
	} pdel=pmove->m_pnext;
	pmove->m_pnext=pdel->m_pnext;
	Type temp=pdel->m_data;
	Delete Pdel;
return temp; } template<typename type> Type circularlist<type>::get (int n) {if (n<0) {cout<< "The n is out of Bo
		Undary "<<endl;
	Exit (1);
	} listnode<type> *pmove=head->m_pnext;
		for (int i=0;i<n;i++) {pmove=pmove->m_pnext;
			if (pmove==head) {cout<< "The n is out of boundary" <<endl;
		Exit (1);
} return pmove->m_data; Template<typename type> void Circularlist<type>::P rint () {listnode<type> *pmove=head->m_pnext
	; cout<< "Head";
		while (pmove!=head) {cout<< "--->" <<pmove->m_data;
	pmove=pmove->m_pnext;
} cout<< "--->over" <<endl<<endl<<endl; }
Main.cpp

#include <iostream>
#include "CircularList.h"

using namespace std;

int main ()
{
	circularlist<int> list;
	for (int i=0;i<20;i++) {
		list. Insert (i*3,i);
	}
	cout<< "The Length of the list is" <<list. Length () <<endl;
	List. Print ();
	for (int i=0;i<5;i++) {
		list. Insert (3,i*3);
	}
	cout<< "The Length of the list is" <<list. Length () <<endl;
	List. Print ();

	List. Remove (5);
	cout<< "The Length of the list is" <<list. Length () <<endl;
	List. Print ();

	List. RemoveAll (3);
	cout<< "The Length of the list is" <<list. Length () <<endl;
	List. Print ();

	cout<< "The third element is" <<list. Get (3) <<endl;

	List. Makeempty ();
	cout<< "The Length of the list is" <<list. Length () <<endl;
	List. Print ();

	return 0;
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.