Linear table of data structure

Source: Internet
Author: User

/****************************
* date:2015-07-19
* Description:linerlist.h
*****************************/

#ifndef _liner_list_h
#define _liner_list_h
//
#include <iostream>

Class Linerlist
Template<class t>
Class Linerlist
{
Public
Create a linear table (default constructor)
linerlist (int maxlistsize = 10);
Delete Table (destructor)
~linerlist () {delete [] element;}
Returns true if the table is empty, otherwise false
BOOL IsEmpty () const {return length = = 0;}
Returns the size of the table (that is, the number of elements in the table)
int length () const {return length;}
Finds the K element in the table and saves it to X, or False if one does not exist
BOOL Find (int k, t& x) const;
Returns the position of element x in the table, or 0 if X is not in the table
int Search (const t& x) const;
Delete the k element and save it to X; the function returns the modified linear table
linerlist<t>& Delete (int k, t& x);
Insert x after the K element; The function returns the modified linear table
linerlist<t>& Insert (int k, const t& x);
Element output
void Output (std::ostream& os) const;


Private
int length; Number of elements in table
int MaxSize; Capacity of the table
T *element; One-dimensional dynamic array
};

<< Heavy Duty
Template<class t>
std::ostream& operator<< (Std::ostream &, const linerlist<t> &);

#include "LinerList.cpp"

#endif//_liner_list_h

/****************************
* date:2015-07-19
* Description:LinerList.cpp
*****************************/

#ifndef _liner_list_cpp
#define _liner_list_cpp

#include "LinerList.h"
#include <iostream>
Template<class t> class Linerlist;
//
Template<class t>
void Linerlist<t>::output (std::ostream& os) const
{
for (int i = 0; i < length; i++)
Os << element[i] << "";
}
//
Template<class t>
std::ostream& operator<< (std::ostream& os, const linerlist<t>& List)
{
List.output (OS);
return OS;
}
//
Template<class t>
linerlist<t>::linerlist (int maxlistsize = 10)
{
MaxSize = maxlistsize;
element = new T[maxsize];
length = 0;
}
//
Template<class t>
BOOL Linerlist<t>::find (int k, t& x) const
{
if (K < 1 | | k > Length)
return false;
x = Element[k-1];
return true;
}
//
Template<class t>
int Linerlist<t>::search (const t& x) const
{
for (int i = 0; i < length; i++)
if (element[i] = = x)
return ++i;
return 0;
}
//
Template<class t>
linerlist<t>& linerlist<t>::D elete (int k, T &x)
{
if (Find (k, X))
{
for (int i = k; i < length; i++)
ELEMENT[I-1] = Element[i];
length--;
return *this;
}
Else
{
Cerr << "Out of Bounds" << Endl;
Exit (Exit_failure);
}
}
//
Template<class t>
linerlist<t>& linerlist<t>::insert (int k, const T &x)
{
if (k < 0 | | k > Length)
{
Cerr << "Out of Bounds" << Endl;
Exit (Exit_failure);
}
if (length = = MaxSize)
{
Cerr << "No Enough Space" << Endl;
Exit (Exit_failure);
Storage doubling
MaxSize *= 2;
T *new_element = new T[maxsize];
for (int i = 0; i < length; i++)
New_element[i] = Element[i];
delete [] element;
element = New_element;
}
for (int i = length-1; I >= K; i--)
ELEMENT[I+1] = Element[i];
ELEMENT[K] = x;
length++;
return *this;
}

#endif//_liner_list_cpp

Linear table of data structure

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.