C + + data structure learning one (sequential table)

Source: Internet
Author: User

Sequentiallist.h Sequential Table template class

#ifndef sequential_list_hxx
#define Sequential_list_hxx

Using Std::cout;

Using Std::endl;

const int maxsize=100; Sequential table Array Maximum value

Template<class t>
Class Seqlist//define template class seqlist (sequential table)
{
Public

Seqlist () {length=0;} Parameterless constructor, creating an empty sequential table
Seqlist (T a[],int N); With a parametric constructor, a sequential table N of length n is no more than maxsize
~seqlist () {}//destructor

int Length () {return Length;} To find the linear table length

T Get (int i); Bitwise lookup, finding the I element in a linear table

int Locate (T x); Find by value, number of elements in a linear table with a value of X

void Insert (int i,t x); Insert operation, inserting an element with a value of x in the first position in a linear table

T Delete (int i); Delete operation, delete the first element of the linear table

void Printlist (); Traversal operation, sequentially outputting each element in sequence


Private
T Data[maxsize]; Array that holds data elements
int length; Length of linear table
};


Template<class t>
Seqlist<t>::seqlist (T a[],int N)
{
if (n>maxsize) throw "argument is illegal";
for (int i=0;i<n;i++)
Data[i]=a[i];
Length=n;
}

Template<class t>
T seqlist<t>::get (int i)
{
if (i>maxsize) throw "argument is illegal";
return data[i-1];
}

Template<class t>
int seqlist<t>::locate (T x)
{
for (int i=0;i<length;i++)
if (data[i]==x) return i+1; The element labeled I is equal to X, returning its ordinal i+1
return 0; Exit loop, description failed to find
}

Template<class t>
void Seqlist<t>::insert (int i,t x)
{
if (length>=maxsize) throw "overflow";
if (i<1| | (i>length+1)) Throw "position overflow";
for (int j=length;j>=i;j--)
DATA[J]=DATA[J-1]; Note that the first J element exists in the array small mark is j-1
Data[i-1]=x;
length++;
}

Template<class t>
T seqlist<t>::D elete (int i)
{
if (length==0) throw "underflow";
if (i<1| | i>length) throw "position";
T X=data[i]; Remove the element of position I
for (int j=i;j<length;j++)
DATA[J-1]=DATA[J]; Here J is already an array of elements in the following table
length--;
return x;
}

Template<class t>
void Seqlist<t>::P rintlist ()
{
for (int i=0;i<length;i++)
cout<<data[i]; Output the element values of a linear table sequentially
}

#endif

Seqlisttest.cpp
#include <iostream>
#include "Sequentiallist.h"
Using Std::cout;
Using Std::endl;

int main ()
{
int m[10]={1,2,3,4,28,50,7,8,9,10};
Seqlist<int> seqlist (m,10);

cout<< "Order table raw State" <<endl;
Seqlist.printlist ();
cout<< "5th element value of Order table" <<seqlist.get (5) <<endl;
cout<< "Sequential table Length" <<seqlist.length () <<endl;

cout<< "Order table Delete state after an element" <<endl;
Seqlist.delete (5);
Seqlist.printlist ();
cout<< "5th element value of Order table" <<seqlist.get (5) <<endl;
cout<< "Sequential table Length" <<seqlist.length () <<endl;

cout<< "status after adding an element to the order table" <<endl;
Seqlist.insert (5,8);
Seqlist.printlist ();
cout<< "5th element value of Order table" <<seqlist.get (5) <<endl;
cout<< "Sequential table Length" <<seqlist.length () <<endl;

cout<< "=108 in Table" <<seqlist.locate (108) << "bit" <<endl;

return 0;
}

C + + data structure learning one (sequential 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.