Two-way chain linear table (template implementation)

Source: Internet
Author: User

  Linear table (list): A finite sequence of 0 or more data elements.

The storage structure of linear table is divided into three kinds: 1, sequential storage structure 2, chained storage structure 3, static linked list.

A linear table of sequential storage structures is implemented by an array, because C + + does not support variable-length arrays, so a linear table of sequential storage structures specifies the length at the time of definition, which is a big problem. For example, the linear table of a sequential storage structure has a length of 10000, and we write a program that uses this linear table, but only adds more than 10 data to the table, which obviously wastes a lot of space. So, the linear table of this structure is suitable for practical applications.

The linear table of chain storage structure realizes the dynamic allocation of space, this concept solves the problem of sequential storage structure, and in the explanation of most data structure, the chain linear table is explained as the focus. However, almost all of the data structure books are in the chain linear table This structure concept, the code in the book almost exclusively pseudo-code, using the abstract data type, this writing can not be run directly on the computer, since the data types are abstract, why not use a template class to implement a linear table.

Linear table is one of the most basic data structure, so the application is also extensive, when writing linear table, should try to consider its flexibility, this article uses the doubly linked list structure to form a linear table, that is, a node of the linear table has three items: data, next pointer, pervious pointer. By constructing such nodes, linear tables can be flexibly inserted into nodes from the beginning and the end.

Node:

Bidirectional chain Linear table (the linear table defined in this document, the head and tail nodes do not hold data):

An empty linear table:

Push Data to list:

Pop Data from list:

Code

#ifndef basedef_h__#defineBasedef_h__Template<classAdt>structnode{Node (ADT data) { This->data =data; Previous=nullptr; Next=nullptr; } Node ()//used to create a no-merit head node.{Previous=nullptr; Next=nullptr;    } ADT data; Node<ADT> *previous; Node<ADT> *next;};//two-way chain linear tabletemplate<classAdt>classzllinklist{ Public: Zllinklist (); ~zllinklist (); BOOLClear (); Public:    BOOLpushhead (ADT data); BOOLPophead (ADT &data); BOOLpushback (ADT data); BOOLPopback (ADT &data); ADT at (intindex); intsize ();Private: Node<ADT> *head;//Logic HeaderNode<adt> *tail;//Logical Tail    intlistsize;};//Template Implementationtemplate<classAdt> zllinklist<adt>:: Zllinklist () {head=Newnode<adt>;//no value storedTail =Newnode<adt>;//no value storedHead->next =tail; Tail->previous =Head; Listsize=0;} Template<classAdt> zllinklist<adt>::~zllinklist () { This-Clear (); DeleteHead; Deletetail;} Template<classAdt>BOOLZllinklist<adt>:: Clear () {if(Head->next = =tail) {        return false; } Node<ADT> *p =nullptr;  while(Head->next! =tail) {P= head->Next; Head->next = p->Next; (P->next)->previous =Head; Deletep; }    return true;} Template<classAdt>intZllinklist<adt>:: Size () {returnListsize;} Template<classAdt>BOOLZllinklist<adt>::P ushhead (ADT data) {Node<ADT> *n =NewNode<adt>(data); Node<ADT> *p;//Auxiliary Pointersp = head->Next; Head->next =N; N->next =p; P->previous =N; N->previous =Head; Listsize++; return true;} Template<classAdt>BOOLZllinklist<adt>::P ushback (ADT data) {Node<ADT> *n =NewNode<adt>(data); Node<ADT> *p;//Auxiliary Pointersp = tail->previous; P->next =N; N->next =tail; Tail->previous =N; N->previous =p; Listsize++; return true;} Template<classAdt>BOOLZllinklist<adt>::P ophead (ADT &data) {    if(Head->next = =tail) {        return false; } Node<ADT> *p =nullptr; P= head->Next; Head->next = p->Next; (P->next)->previous =Head; Data= p->data; Deletep; Listsize--; return true;} Template<classAdt>BOOLZllinklist<adt>::P opback (ADT &data) {    if(Tail->previous = =head) {        return false; } Node<ADT> *p =nullptr; P= tail->previous; Tail->previous = p->previous; (P->previous)->next =tail; Data= p->data; Deletep; Listsize--; return true;} Template<classAdt> ADT Zllinklist<adt>::at (intindex) {Node<ADT> *p = head->next;//Auxiliary Pointers     for(inti =0; I < index; i++) {p= p->Next; }    returnP->data;}#endif

Demo

#include"iostream"#include"BaseStruct.h"using namespacestd;intMainintArgvChar*argc[]) {Zllinklist<Char>m; M.pushhead ('a'); M.pushhead ('b'); M.pushhead ('C'); M.pushhead ('D'); Chardata;    M.popback (data); Std::cout<< Data <<m.size () <<Endl;    M.popback (data); Std::cout<< data << m.size () <<Endl;    M.popback (data); Std::cout<< data << m.size () <<Endl;    M.popback (data); Std::cout<< data << m.size () <<Endl; return 0;}

Output:

Two-way chain linear table (template implementation)

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.