One-way cyclic chain linear table for C + + template class

Source: Internet
Author: User

Based on the one-way chain linear table http://blog.csdn.net/shiwazone/article/details/47000191, the circular linked list is implemented, and the circular linked list points the pointer field of the tail node to the head node. Join the loop, you can let us in the search for a node of the index, we can first determine the position and the link table length of the relationship, if index in the first half of the list, we can traverse the head node to find, if in the second half, we can look forward from the tail node, of course, we need to use a doubly linked list. The operation of the doubly linked list, next time give it! Circular list gives us a way of thinking, if we in a data block, add one or some of the flag node, we can determine the operation we need is in which interval, can achieve improve productivity!

The plus operator is overloaded in the code to add two linked lists. In fact, we can have more operations, but a lot of operations are based on our insert delete operation! So when we do the data structure analysis, we should pay attention to the universality of inserting and deleting!


#pragma once#include <iostream>using namespace Std;template<typename eletype>class circlelist{public: struct Node{eletype _data; node* _next; Node () {_next = nullptr;} Node (Eletype data) {_data = data; _next = nullptr;}}; Circlelist (); ~circlelist (); bool Getelem (eletype& e, int index = 1) const;//Gets the index element in the list, assigns the _data of the element to E;bool Insertelem (const eletype e, int index = 1);//Insert a node at the index of the list, the data for the node is Ebool Deleteelem (eletype& e, int index = 1); /delete a node at the index of the list, delete the data of the node assigned to Ebool inserthead (const eletype& e);//Insert data in the head bool Inserttail (const eletype& E );//Insert data bool Clear () at the tail,//Empty listvoid showlist () const;//displays data for all elements of list circlelist<eletype>* operator+ ( circlelist<eletype>& addlist);//overloaded operator Private:bool empty () const;//determine if list is empty circlelist<eletype>* Addlist (circlelist<eletype>& addlist);//Add addlist data, which is equivalent to inserting addlist data from the tail into the original list//find the node of index position in list , the address of the node is assigned to n, where it is necessary to pass the reference to the pointer, in order to ensure that n can be modified, or only to ensure that *n can be modified, that is, N point node can be modified bool Find (int index, node*&Amp N) const;bool checkindex (int index) const;//Check whether the list is empty, whether index is legal node* head;//head pointer node* tail;//tail pointer int Length;};

#include "CircleList.h" #include <iostream>using namespace Std;template<typename eletype>bool circlelist <eletype>::checkindex (int index) const{if (Empty ()) {cout << "find:the List is empty!\n"; return false;} if (index<1 | | index>length) {cout << "the index is invalid!\n"; return false;} return true;} Template<typename eletype>bool circlelist<eletype>::find (int index, node*& n) const//index [1,Length] ; {if (CheckIndex (index)) {int i = 1; Node * temp = head;while (i < index) {temp = temp->_next;++i;} n = Temp;return true;} return false;} Template<typename eletype>void circlelist<eletype>::showlist () const{node * temp = nullptr;if (Empty ()) { cout << "The list is empty!\n";} else{for (int i = 1; I <= Length; ++i) {Find (i, temp); cout << temp->_data << "";} cout << Endl;}} Template<typename Eletype>bool circlelist<eletype>::clear () {if (Empty ()) {return true;} Else{while (Length) {Eletype M;deleteelem (m);} return true;}} Template<typename Eletype>bool circlelist<eletype>::D Eleteelem (eletype& e, int index = 1) {if ( CheckIndex (Index)) {node* temp = nullptr; node* pre_temp = nullptr;if (Find (Index, temp)) {if (index = = 1) {Find (index + 1, Head);} Else{if (index = = Length) {Find (index-1, Tail);} Else{find (index-1, pre_temp);p re_temp->_next = Temp->_next;}} E = Temp->_data;delete Temp;--length;return true;} return false;} return false;} Template<typename eletype>bool Circlelist<eletype>::insertelem (const eletype E, int index) {Node * Insertnode = new Node (e), if (Empty ()) {if (Index < 1) return false; Head = Tail = Insertnode;insertnode->_next = Insertnode;++length;return true;} if (index = = 1) {insertnode->_next = Head; Head = Insertnode; Tail->_next = Head;++length;return true;} if (index = = Length + 1) {Tail->_next = Insertnode;insertnode->_next = Head; Tail = Insertnode;++length;return true;} Node *temp = nullptr;if (Find (index-1, temp)) {insertnode->_next = Temp->_next;temp->_next = Insertnode;++length;return true;} return false;} Template<typename Eletype>bool Circlelist<eletype>::getelem (eletype& e, int index) CONST{IF ( CheckIndex (Index)) {node* temp = nullptr;if (Find (index, temp)) E = Temp->_data;return true;} return false;} Template<typename eletype>circlelist<eletype>::~circlelist () {Clear ();} Template<typename eletype>circlelist<eletype>::circlelist (): Length (0), Head (nullptr), Tail (nullptr) {} Template<typename eletype>bool Circlelist<eletype>::empty () const{return (Length = = 0);} Template<typename eletype>bool circlelist<eletype>::inserttail (const eletype& e) {return InsertElem ( E, Length + 1);} Template<typename eletype>bool circlelist<eletype>::inserthead (const eletype& e) {return InsertElem ( e);} Template<typename eletype>circlelist<eletype>* circlelist<eletype>::addlist (CircleList< eletype>& addlist) {if (Empty ()) {if (Addlist.empty ()) {return this;} node* temp = addlist.head;for (int i = 1; I <= addlist.length;++i) {inserttail (temp->_data); temp = Temp->_next;} return this;} Else{if (Addlist.empty ()) {return this;} else{node* temp = addlist.head;for (int i = 1; I <= addlist.length; ++i) {inserttail (temp->_data); temp = Temp->_ne XT;} return this;}}} Template<typename eletype>circlelist<eletype>* circlelist<eletype>::operator+ (CircleList< eletype>& addlist) {return addlist (addlist);}

#include "CircleList.cpp" int main () {circlelist<int> intlist1;intlist1.showlist (); Intlist1.insertelem (155); I Ntlist1.insertelem (5); Intlist1.insertelem (Intlist1.insertelem); Intlist1.insertelem (in); Ntlist1.showlist (); Circlelist<int> Intlist2;intlist2.insertelem (one), Intlist2.insertelem (one), Intlist2.insertelem (one); Ntlist2.insertelem (one), Intlist2.insertelem (one), intlist2.showlist (); Circlelist<int> *intlist3;intlist3 = IntList2 + intlist1;intlist3->showlist (); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

One-way cyclic chain linear table for C + + template class

Related Article

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.