Sequence Table (Array Implementation)

Source: Internet
Author: User

# Include <iostream. h>
# Include <stdlib. h>

Const int maxlistsize = 50;
Typedef int datatype;

Class seqlist
{
PRIVATE:
Datatype listitem [maxlistsize];
Int size;

Public:
Seqlist (void );

// The method of access elements
Int listsize (void) const;
Int listempty (void) const;
Int find (datatype & item) const;
Datatype getdata (int pos) const;

// The method of modify elements
Void insert (const datatype & item );
Void Delete (const datatype & item );
Datatype deletefront (void );
Void clearlist (void );
};

Void printlist (const seqlist & L)
{
Int length = L. listsize ();

For (INT I = 0; I <length; I ++)
{
Cout <L. getdata (I) <"";
}

Cout <Endl;
}

Void insertmax (seqlist & L, int ELT)
{
Int I = 0;

While (I <L. listsize () & L. getdata (I) <ELT)
{
I ++;
}

If (I! = L. listsize ())
{
Cout <"not insert" <Endl;
}
Else
{
L. insert (ELT );
}
}

// Constructor.
Seqlist: seqlist ()
{
Size = 0;
}

Void seqlist: insert (const datatype & item)
{
If (size + 1)> maxlistsize)
{
Cerr <"maxinum list size exceeded" <Endl;
Exit (1 );
}

Listitem [size ++] = item;
}

Void seqlist: delete (const datatype & item)
{
Int I = 0;

While (I <size &&! (Item = listitem [I])
{
I ++;
}

If (I <size)
{
While (I <size-1)
{
Listitem [I] = listitem [I + 1];
I ++;
}

Size --;
}
}

Datatype seqlist: getdata (int pos) const
{
If (Pos <0 | POS> = size)
{
Cerr <"POS is out of range! "<Endl;
Exit (1 );
}

Return listitem [POS];
}

Int seqlist: Find (datatype & item) const
{
Int I = 0;

If (listempty ())
{
Return 0;
}

While (I <size &&! (Item = listitem [I])
{
I ++;
}

If (I <size)
{
Item = listitem [I];

Return 1;
}
Else
{
Return 0;
}
}

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.