Concise memorandum of Data Structure linear table

Source: Internet
Author: User

Linear table

A linear table is an abstraction of a linear structure. A linear structure is characterized by a one-to-one linear relationship between data elements in the structure.
The Positional relationships between data elements are arranged one by one:
. Except for the data element at the first position, there is only one data element at the front of the other data element positions.
. Except for the last position, all other data element positions are followed by only one element.
A linear table is usually expressed as L = (D, R)
D is a finite set of data elements.
R is a finite set of relationships between data elements.

Basic operations on Linear tables:
CopyCodeThe Code is as follows: public interface ilistds <t> {
Int getlength (); // evaluate the length
Void clear (); // clear
Bool isempty (); // empty
Void append (T item); // append
Void insert (T item, int I); // insert
T Delete (int I); // Delete
T getelement (int I); // retrieves Table Elements
Int locate (T value); // search by value
}

Sequence Table

A sequence table is a sequence storage of linear tables. It stores the data elements (sequence list) of a linear table in a sequential space with an address. It features random access.

W: each data element occupies W storage units
A1: base address of the sequence table)
LOC (AI) = LOC (A1) + (I-1) * W 1 <= I <= N

In order to understand the sequence table, lightning learns such an example. If you are interested, you can write it on your own machine.
The Order tables La and lb with integer data types are arranged in ascending order of data elements from small to large.AlgorithmMerge them into a table LC, which requires that the data elements in the LC are also arranged in ascending order.
Algorithm ideas:
Scan the data elements of La and lb in sequence, compare the values of the current data elements of La and lb, and assign the smaller data elements to LC until a sequence table is scanned, then, assign the remaining data elements in the sequence table to LC. LC capacity must be able to accommodate the length of the addition of the La and lb tables.
Concept illustration:
Copy codeThe Code is as follows: public class seqlist <t>: ilistds <t> {
Private int maxsize; // capacity of the sequence table
Private T [] data; // array, used to store data elements in a sequence table
Private int last; // indicates the position of the last element in the sequence table.

// constructor
Public seqlist (INT size)
{< br> DATA = new T [size];
maxsize = size;
last =-1; // If the sequence table is empty, last =-1
}< br> // indexer
Public t this [int Index]
{< br> get {return data [Index];}
set {data [Index] = value ;}
}< br> // location attribute of the last element
Public int last
{< br> get {return last ;}
}< br> // capacity attribute
Public int maxsize
{< br> get {return maxsize ;}< br> set {maxsize = value ;}
}< br> // determines whether the sequence table is empty.
Public bool isempty ()
{< br> If (last =-1)
return true;
else
return false;
}< br> // determines whether the sequence table is full.
Public bool isfull ()
{< br> If (last = maxsize-1)
return true;
else
return false;
}< br> // calculate the length of the sequence table
Public int getlength ()
{< br> return last + 1;
}< br> // clear the sequence table
Public void clear ()
{< br> last =-1;
}< br> // Add a new element to the end of the sequence table
Public void append (T item)
{< br> If (isfull ())
{< br> console. writeline ("list is full. ");
return;
}< br> data [++ last] = item;
}

// Insert a data element at the position of the I Data Element in the sequence table
Public void insert (T item, int I)
{
If (isfull ())
Return;
If (I <1 | I> last + 2)
Return;
If (I = last + 2)
Data [last + 1] = item;
Else
{
For (Int J = last; j> = I-1; -- J)
{
Data [J + 1] = data [J];
}
Data [I-1] = item;
}
++ Last;
}
// Delete the I-th data element of the sequence table
Public t Delete (int I)
{
T TMP = default (t );
If (isempty ())
Return TMP;
If (I <1 | I> last + 1)
Return TMP;
If (I = last + 1)
TMP = data [last --];
Else
{
TMP = data [I-1];
For (Int J = I; j <= last; ++ J)
Data [J] = data [J + 1];
}
-- Last;
Return TMP;
}
// Obtain the I-th data element of the sequence table
Public t getelement (int I)
{
If (isempty () | (I <1) | (I> last + 1 ))
Return default (t );
Return data [I-1];
}
// Find the data element with the value in the sequence table
Public int locate (T value)
{
If (isempty ())
Return-1;
Int I = 0;
For (I = 0; I <= last; ++ I)
{
If (value. Equals (data [I])
Break;
}
If (I> last)
Return-1;
Return I;
}
}

copy the Code the code is as follows: public class genericlist
{< br> Public genericlist ()
{}< br> Public seqlist Merge (seqlist La, seqlist lb)
{< br> seqlist lc = new seqlist (La. maxsize + lB. maxsize);
int I = 0;
Int J = 0;
int K = 0;
// The elements in both tables are not empty.
while (I <= (La. getlength ()-1) & (j <= (lb. getlength ()-1)
{< br> If (La [I] LC. append (La [I ++]);
else
LC. append (Lb [J ++]);
}< br> // data elements in Table A
while (I <= (La. getlength ()-1)
LC. append (La [I ++]);
// data elements in Table B
while (j <= (lb. getlength ()-1)
LC. append (Lb [J ++]);
return lC;
}< BR >}

Client code:Copy codeThe Code is as follows: static void main (string [] ARGs)
{
Seqlist <int> sl1 = new seqlist <int> (4 );
Sl1.append (1 );
Sl1.append (3 );
Sl1.append (4 );
Sl1.append (7 );
Seqlist <int> sl2 = new seqlist <int> (6 );
Sl2.append (2 );
Sl2.append (5 );
Sl2.append (6 );
Sl2.append (8 );
Sl2.append (11 );
Sl2.append (14 );
Genericlist GL = new genericlist ();
Seqlist <int> sl3 = gl. Merge (sl1, sl2 );
Console. writeline ("Length:" + sl3.getlength ());
For (INT I = 0; I <sl3.getlength (); I ++)
{
Console. writeline (I + ":" + sl3 [I]);
}
}

Okay. Next time you learn the linked list.
Author: levinlee

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.