2.2 sequence table
2.2.1 sequence table definition www.2cto.com
In a computer, the simplest and most natural way to save a linear table is to put the elements in the table one by one into the sequential Storage unit, which is the sequential Storage of the linear table ). The sequential storage of a linear table is to store the data elements of a linear table in a sequential space with an address in the memory. A linear table stored in this way is called a Sequence List ). An ordered table is characterized by adjacent data elements in the table in the memory.
C # the storage space occupied by arrays in the language in the memory is a set of continuous storage space. Therefore, data has the characteristics of random access, arrays are inherently characteristic of data storage areas that represent sequential tables.
Random Access (sometimes called direct access) represents an arbitrary component in a set of sequences at the same time. Otherwise, sequential access requires more time to access a remote component.
C # sequence table:
[Csharp] class SeqList <T>: leleapplication3.iseqlist <T>
{
Public int Maxsize {get; set ;}
Private T [] data;
Public int Last {get; private set ;}
// Indexer
Public T this [int index]
{
Get
{
Return data [index];
}
Set
{
Data [index] = value;
}
}
Public SeqList (int size)
{
Data = new T [size];
Maxsize = size;
Last =-1;
}
Public int GetLength ()
{
Return Last + 1;
}
Public void Clear ()
{
Last =-1;
}
Public bool IsEmpty ()
{
If (Last =-1)
Return true;
Else
Return false;
}
Public bool IsFull ()
{
If (Last = Maxsize-1)
Return true;
Else
Return false;
}
Public void Append (T item)
{
If (IsFull ())
Console. WriteLine ("List is full ");
Else
Data [++ Last] = item;
}
Public void Insert (T item, int I)
{
If (IsFull ())
Console. WriteLine ("List is full ");
Else if (I <0 | I> Last + 1)
Console. WriteLine ("Position is error! ");
Else if (I = Last + 1)
Data [++ Last] = item;
Else
{
For (int j = ++ Last; j> I; -- j)
{
Data [j] = data [j-1];
}
Data [I] = item;
}
}
Public T Delete (int I)
{
T tmp = default (T );
If (IsEmpty ())
{
Console. WriteLine ("List is empty ");
Return tmp;
}
Else if (I> Last | I <0)
{
Console. WriteLine ("Position is error! ");
Return tmp;
}
Else
{
Tmp = data [I];
For (int j = I; j <Last; j ++)
{
Data [j] = data [j + 1];
}
-- Last;
Return tmp;
}
}
Public T GetItem (int I)
{
If (IsEmpty () | I <0 | I> Last + 1)
{
Console. WriteLine ("List is empty or Position is error! ");
Return default (T );
}
Else
Return data [I];
}
Public int Locate (T value)
{
If (IsEmpty ())
{
Console. WriteLine ("List is empty! ");
Return-1;
}
For (int I = 0; I <= Last; ++ I)
{
If (value. Equals (data [I])
Return I;
}
Return-1;
}
}
Class SeqList <T>: leleapplication3.iseqlist <T>
{
Public int Maxsize {get; set ;}
Private T [] data;
Public int Last {get; private set ;}
// Indexer
Public T this [int index]
{
Get
{
Return data [index];
}
Set
{
Data [index] = value;
}
}
Public SeqList (int size)
{
Data = new T [size];
Maxsize = size;
Last =-1;
}
Public int GetLength ()
{
Return Last + 1;
}
Public void Clear ()
{
Last =-1;
}
Public bool IsEmpty ()
{
If (Last =-1)
Return true;
Else
Return false;
}
Public bool IsFull ()
{
If (Last = Maxsize-1)
Return true;
Else
Return false;
}
Public void Append (T item)
{
If (IsFull ())
Console. WriteLine ("List is full ");
Else
Data [++ Last] = item;
}
Public void Insert (T item, int I)
{
If (IsFull ())
Console. WriteLine ("List is full ");
Else if (I <0 | I> Last + 1)
Console. WriteLine ("Position is error! ");
Else if (I = Last + 1)
Data [++ Last] = item;
Else
{
For (int j = ++ Last; j> I; -- j)
{
Data [j] = data [j-1];
}
Data [I] = item;
}
}
Public T Delete (int I)
{
T tmp = default (T );
If (IsEmpty ())
{
Console. WriteLine ("List is empty ");
Return tmp;
}
Else if (I> Last | I <0)
{
Console. WriteLine ("Position is error! ");
Return tmp;
}
Else
{
Tmp = data [I];
For (int j = I; j <Last; j ++)
{
Data [j] = data [j + 1];
}
-- Last;
Return tmp;
}
}
Public T GetItem (int I)
{
If (IsEmpty () | I <0 | I> Last + 1)
{
Console. WriteLine ("List is empty or Position is error! ");
Return default (T );
}
Else
Return data [I];
}
Public int Locate (T value)
{
If (IsEmpty ())
{
Console. WriteLine ("List is empty! ");
Return-1;
}
For (int I = 0; I <= Last; ++ I)
{
If (value. Equals (data [I])
Return I;
}
Return-1;
}
} [Csharp] view plaincopyprint? // ISeqList. cs
Using System;
Namespace ConsoleApplication3
{
Interface ISeqList <T>
{
Void Append (T item );
Void Clear ();
T Delete (int I );
T GetItem (int I );
Int GetLength ();
Void Insert (T item, int I );
Bool IsEmpty ();
Bool IsFull ();
Int Locate (T value );
}
}
From xufei96's column