Data structure 1---linear table

Source: Internet
Author: User

C++primer behind the advanced features can not look down, re-find a data structure of the book to see, Refueling!! The book is called "Data structure algorithm and C + + language description"!

Basic complete Insert, find, delete, print, oh, yes, that operator<< overload feeling indefinitely!!!

The reference code is as follows:

#include <iostream>//using namespace Std;template<class t>class linearlist{public:linearlist (int maxlistsize = 0); ~linearlist () {}bool IsEmpty () {return bool (length);}; int Length () {return Length;}; BOOL Find (int k,t& x); int Search (const t& a) const; linearlist<t>& Delete (int k,t &x); linearlist<t>& Insert (int k,t& x); void OutPut (Std::ostream &out) Const;private:int Length;int MaxSize; T *element;}; Template<class t>linearlist<t>::linearlist (int maxls) {MaxSize = Maxls;length = 0;element = new T[MaxLS];} Template<class t>bool linearlist<t>::find (int k, T &x) {if (k>maxsize| | k<0) Return false;x = Element[k-1];return true;} Template<class t>int linearlist<t>::search (const T &a) const{for (int i = 0;i < MaxSize; ++i) {if ( Element[i] = = a) return i++;} return 0;} Template<class t>linearlist<t>& linearlist<t>::D elete (int k,t &x) {if (This->find (k,x)) {for (int i = k-1;i<maxsize;i++){Element[i-1] = element[i];} length = Length-1;std::cout<<length<<std::endl;return *this;}} Template<class t>linearlist<t>& linearlist<t>::insert (int k,t &x) {if (length = = MaxSize) { std::cout<< "full! "<<std::endl;} for (int i = maxsize;i > k-1;i--) {element[i] = element[i-1];} Element[k] = X;length++;return *this;} Template<class t>void linearlist<t>::output (std::ostream &out) const{for (int i = 0;i < length;i++) out<<element[i]<< "";} Template<class t>std::ostream& operator<< (std::ostream& out,const LinearList<T>& a) {a . OutPut (out); return out;} int main () {int ftest = 0; Linearlist<int> ll (5); for (int i = 0; i< 5; ++i) {int temp = i * i;ll. Insert (i,temp);} Std::cout<<ll. Length () <<std::endl;std::cout<<ll<<std::endl;ll. Find (2,ftest); Std::cout<<ftest<<std::endl;ll. Delete (2,ftest); Std::cout<<ll. Length () <<std::endl;std::cout<<ll< <std::endl;return 0;} 

  

Data structure 1---linear table

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.