Linear table linearlist

Source: Internet
Author: User

The underlying layer uses a one-dimensional array as the continuous storage space. This exercise automatically expands and shrinks the space, following the STL vector. If the number of elements in a table is equal to maxsize, the scale-in algorithm doubles. If the number of elements in a table is smaller than 1/4 of maxsize, then, the space is reduced to half of the maxsize...

 

In addition, the template requires the declaration and implementation in an H file. The following is the implementation and test code of a linear table:

 

Linearlist. h

# Pragma once <br/> # include <stdio. h> <br/> template <class T> <br/> class clinearlist <br/>{< br/> Public: <br/> clinearlist (void ); <br/> virtual ~ Clinearlist (void); <br/> bool isempty () const; <br/> int length () const; <br/> // add, delete, modify, and query <br/> void insert (int K, const T & X); <br/> bool Delete (int K, T & X ); <br/> int search (const T & X) const; <br/> bool find (int K, const T & X) const; <br/> void output (ostream & out) const; </P> <p> PRIVATE: <br/> int length; <br/> int maxsize; <br/> T * element; <br/>}; <br/> template <class T> <br/> clinearlist <t>: clinearlist (void) <Br/>{< br/> maxsize = 1; <br/> element = new T [maxsize]; <br/> length = 0; <br/>}< br/> template <class T> <br/> clinearlist <t> ::~ Clinearlist (void) <br/>{< br/> Delete [] element; <br/>}< br/> template <class T> <br/> bool clinearlist <t>: isempty () const <br/>{< br/> return (0 = length ); <br/>}< br/> template <class T> <br/> int clinearlist <t >:: length () const <br/>{< br/> return length; <br/>}< br/> // add, delete, modify, and query <br/> template <class T> <br/> void clinearlist <t>: insert (int K, const T & X) <br/>{< br/> // insufficient data space, extended space <br/> If (length> = maxsize) <br/> {<br/> maxsize * = 2; <br/> T * newele = new T [maxsize]; </P> <p> // memcpy (newele, element, length); <br/> for (INT I = 0; I <length; I ++) <br/>{< br/> newele [I] = element [I]; <br/>}< br/> Delete [] element; <br/> element = newele; <br/>}< br/> // move data <br/> for (INT I = length; I> K; I --) <br/> {<br/> element [I] = element [I-1]; <br/>}< br/> // insert data <br/> element [k] = x; <br/> length ++; <br/>}< br/> template <class T> <br/> bool clinearlist <t>: delete (int K, T & X) <br/>{< br/> If (k <0 | K> length) <br/> return false; <br/> X = element [k]; <br/> for (INT I = K; I <length; I ++) <br/>{< br/> element [I] = element [I + 1]; <br/>}< br/> length --; <br/> // shrink the space <br/> If (length <= (maxsize/4 )) <br/> {<br/> maxsize/= 2; <br/> T * newele = new T [maxsize]; <br/> for (INT I = 0; I <length; I ++) <br/>{< br/> newele [I] = element [I]; <br/>}< br/> Delete [] element; <br/> element = newele; <br/>}< br/> return true; <br/>}< br/> template <class T> <br/> int clinearlist <t>: Search (const T & X) const <br/> {<br/> for (INT I = 0; I <length; I ++) <br/>{< br/> If (element [I] = x) <br/> return I; <br/>}< br/> return-1; <br/>}< br/> template <class T> <br/> bool clinearlist <t>: Find (int K, const T & X) const <br/>{< br/> If (k <0 | K> length) <br/> return false; <br/> X = element [k]; <br/> return true; <br/>}< br/> template <class T> <br/> void clinearlist <t>: output (ostream & out) const <br/> {<br/> out <"Length:" <length <Endl; <br/> for (INT I = 0; I <length; I ++) <br/>{< br/> out <element [I] <Endl; <br/>}< br/> 

 

Testdatastruct. cpp

// Testdatastruct. cpp: defines the entry point of the console application. <Br/> // <br/> # include "stdafx. H "<br/> # include <iostream> <br/> using namespace STD; <br/> # include" linearlist. H "<br/> clinearlist <int> intll; <br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> cout <intll. length () <Endl; <br/> intll. insert (0, 1); <br/> intll. output (cout); <br/> intll. insert (1, 3); <br/> intll. output (cout); <br/> intll. insert (2, 4); <br/> intll. output (cout); <br/> int N; <br/> intll. delete (1, N); <br/> cout <n <"deleted" <Endl; <br/> intll. output (cout); </P> <p> int TMP = 0; <br/> CIN> TMP; <br/> return 0; <br/>}< br/> 

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.