9 seconds College C + + template class implementation single-linked list

Source: Internet
Author: User
Tags class definition


first on the code

/******************************** definition of linear table abstract class ***************************/

Template <class datatype>

Class list{

Public

virtual void empty () = 0; Empty the linear table

virtual int getlength () = 0; to find the length of a table

virtual void Insert (int i,const datatype& x) = 0; Inserting an element with a value of x in the table I position

virtual void Remove (int i) = 0; Remove elements from the I position in a table

virtual int Search (const datatype& x) = 0; finds and returns the position of the element with the value x in the table

Virtual DataType Visit (int i) = 0; accessing The value of element I in a table

virtual void traverse () = 0; traversing a linear table

};

/******************************* definition of a single-linked list class ********************************/

Template <class datatype>

Class linklist:publiclist<datatype>{// public inherits from list class

Public

Linklist (); constructors, generating empty lists when creating objects

void empty (); Empty List

int GetLength (); to find the length of a table

void Insert (int i,const datatype& x); Inserting an element with a value of x in the table I position

void Remove (int i); Remove elements from the I position in a table

int search (const datatype& x); finds and returns the position of the element with the value x in the table

DataType visit (int i); accessing The value of element I in a table

void Traverse (); to output elements from a table individually

~linklist (); Destructors

Private

Defining Nodes

struct node{

DataType data;

Node* Next;

Node (): Next (NULL) {};

Node (dataType d):d Ata (d), next (NULL) {};

};

node* Head; Linked list Header

node* tail; Chain Footer

int currentlength; Linked list length

};

implementation of/********************************* single-link list class ******************************/

Template <class datatype>

Linklist<datatype>::linklist () {

Head=new node;

Tail=new node;

head->next=tail;

currentlength=0;

cout<< "\ncreate list success!\n";

}

Template <class datatype>

void Linklist<datatype>::empty () {

node* nowpos=head->next;

while (Nowpos!=tail) {

node* Tmp=nowpos;

nowpos=nowpos->next;

Delete tmp;

}

head->next=tail;

currentlength=0;

cout<< "\nempty list success!\n";

}

Template <class datatype>

int Linklist<datatype>::getlength () {

return currentlength;

}

Template <class datatype>

void Linklist<datatype>::insert (Inti,const datatype& x) {

if (i<0| | I>currentlength) {

cout<< "\nthe index is out of range!\n";

Return

}

node* add=new node (x);

if (i==0) {

add->next=head->next;

head->next=add;

}

else{

Find The first i-1 node

node* nowpos=head->next;

while (-i) {

nowpos=nowpos->next;

}

add->next=nowpos->next;

nowpos->next=add;

}

++currentlength;

cout<< "\ninsert data success!\n";

}

Template <class datatype>

void Linklist<datatype>::remove (Inti) {

if (i<0| | I>=currentlength) {

cout<< "\nthe index is out of range!\n";

Return

}

if (i==0) {

node* tmp=head->next->next;

Delete head->next;

head->next=tmp;

}

else{

Find The first i-1 node

node* nowpos=head->next;

while (-i) {

nowpos=nowpos->next;

}

node* tmp=nowpos->next->next;

Delete nowpos->next;

nowpos->next=tmp;

}

--currentlength;

cout<< "\ndelete data success!\n";

}

Template <class datatype>

int Linklist<datatype>::search (constdatatype& x) {

node* Nowpos=head;

int i;

for (I=0;i<currentlength;++i) {

nowpos=nowpos->next;

if (nowpos->data==x) break;

}

if (i==currentlength) return-1;

return i;

}

Template <class datatype>

datatypelinklist<datatype>::visit (int i) {

Subscript out- of-bounds throws an exception value 0

if (i<0| | I>=currentlength) {

Throw 0;

}

Find the i node

node* nowpos=head->next;

while (i--) {

nowpos=nowpos->next;

}

Return nowpos->data;

}

Template <class datatype>

void Linklist<datatype>::traverse () {

if (currentlength==0) {

cout<< "\nthe list is empty!\n";

Return

}

node* nowpos=head->next;

cout<<nowpos->data;

nowpos=nowpos->next;

while (Nowpos!=tail) {

cout<< "" <<nowPos->data;

nowpos=nowpos->next;

}

cout<<endl;

}

Template <class datatype>

Linklist<datatype>::~linklist () {

Empty ();

Delete head;

Delete tail;

}

Note

1, this code is drawn from the Linear table project package, the contents of the project package are as follows:
Definition of linear table abstract class List.h

SeqList.h , definition of sequential table class

implementation of Sequential table class SeqList.txt

LinkList.h the definition of a single linked list class

implementation of single-linked list class LinkList.txt

definition of double-linked list class dLinkList.h

implementation of double-linked list class DLinkList.txt

definition of test class Test.h

implementation of test class Test.txt

2, we can use these code as a reference, want to improve the ability or to write their own;

3, all the classes are template class, if you want to the template class definition and implementation of separation to note that the implementation of the template class can not be placed in the. cpp file, can only be placed in a . txt file;

6. The knowledge involved is divided into three major areas:

(1) The realization principle of single-linked list;

(2) design and encapsulation of class;

(3) Design abstract class list and let the single-linked list inherit from list, is for the order table, double linked list, with (pure) virtual function to achieve polymorphism. If you use only a single-linked list, you can design a class to solve it, or you can use the above code directly.

more questions can be reached in 9 seconds of college .


9 seconds College C + + template class implementation single-linked list

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.