I. Logical Structure of a linear table
- Linear table definition
1) A linear table (List) is a finite sequence composed of n (n ≥ 0) data elements of the same type.
2) The formal definition of a linear table is as follows: The linear table (List) is simplified to L and is a binary group,
L = (D, R)
D is a finite set of data elements.
R is a finite set of relationships between data elements.
There are many examples of linear tables in real life. For example, an even number between 1 and 100 is a linear table: (2, 4, 6 ,..., 100)
3) in a complex linear table, a data element is a record consisting of several data items,
A linear table containing a large number of records is also called a File ). For example, the student info table in Example 1.1 is a linear table, and each row in the table is a record. A record consists of student ID, name, administrative class, gender, date of birth, and other data items.
4) before selecting the representation of a linear table, the programmer should consider that this representation must support
. According to the definition of a linear table, the length of a linear table can be increased and shortened,
That is to say, the most important operation of a linear table should be able to insert and delete elements anywhere in a linear table. In addition, you can obtain the element value, read the value, or modify the value. You can also generate and clear (or reinitialize) linear tables.
- Sequence table definition
1) sequential storage of linear tables refers to storing data elements of linear tables in Sequence using an address-contiguous space in the memory. A linear table stored in this way is called a Sequence List)
2)C #The storage space occupied by arrays in the language in the memory is a set of continuous storage areas. Therefore,
Arrays have the characteristics of random access. Therefore, arrays are inherently characteristic of data storage areas that represent sequential tables.
3) transpose the array:
Class Program
{
Static void Main (String [] args)
{
Int [] aa = new int [] {1, 2, 4, 5, 7, 8, 9 };
Revose (aa );
For (int I = 0; I <aa. Length; I ++)
{
Console. WriteLine (aa [I]);
}
}
Public staticint [] Revose (int [] bb)
{
Int temp = 0;
Int len = bb. Length;
For (inti = 0; I <(bb. Length)/2; I ++)
{
Temp = bb [I];
Bb [I] = bb [len-i-1];
Bb [len-i-1] = temp;
}
Return bb;
}
}
4) The Custom sequence indicates the sample code:
Using System;
Using System. Collections. Generic;
Using System. Text;
Class Program
{
Static void Main (string [] args)
{
SeqList <int> aa = new SeqList <int> (10 );
Aa. Append (11 );
Aa. Append (20 );
Aa. Append (220 );
Console. WriteLine ("unreversed results ");
For (int I = 0; I <aa. Last + 1; I ++)
{
Console. WriteLine (aa [I]);
}
Aa. Reverse ();
Console. WriteLine ("reverse result ");
For (int I = 0; I <aa. Last + 1; I ++)
{
Console. WriteLine (aa [I]);
}
Console. WriteLine ("locate 20 ");
Console. WriteLine (aa. Locate (20 ));
Console. WriteLine ("null ");
Console. WriteLine (aa. IsEmpty ());
Console. WriteLine ("retrieve the first data ");
Console. WriteLine (aa. GetElem (1 ));
Console. WriteLine ("deleting the first data ");
Aa. Delete (1 );
Console. WriteLine ("Deleted Data ");
For (int I = 0; I <aa. Last + 1; I ++)
{
Console. WriteLine (aa [I]);
}
}
}
Public interface IListDS <T>
{
Int GetLength (); // evaluate the length
Void Clear (); // Clear operation
Bool IsEmpty (); // determines whether the linear table is empty.
Void Append (T item); // additional operation
Void Insert (T item, int I); // Insert operation
T Delete (int I); // Delete operation
T GetElem (int I); // retrieves the table element.
Int Locate (T value); // search by value
}
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.
// Indexer
Public T this [int index]
{
Get
{
Return data [index];
}
Set
{
Data [index] = value;
}
}
// The location attribute of the last data element
Public int Last
{
Get
{
Return last;
}
}
// Capacity attribute
Public int Maxsize
{
Get
{
Return maxsize;
}
Set
{
Maxsize = value;
}
}
// Constructor
Public SeqList (int size)
{
Data = new T [size];
Maxsize = size;
Last =-1;
}
// Calculate the length of the sequence table
Public int GetLength ()
{
Return last + 1;
}
// Clear the sequence table
Public void Clear ()
{
Last =-1;
}
// Determine whether the sequence table is empty
Public bool IsEmpty ()
{
If (last =-1)
{
Return true;
}
Else
{
Return false;
}
}
// Determine whether the sequence table is full
Public bool IsFull ()
{
If (last = maxsize-1)
{
Return true;
}
Else
{
Return false;
}
}
// Add new elements at the end of the sequence table
Public void Append (T item)
{
If (IsFull ())
{
Console. WriteLine ("List is full ");
Return;
}
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 ())
{
Console. WriteLine ("List is full ");
Return;
}
If (I <1 | I> last + 2)
{
Console. WriteLine ("Position is error! ");
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 ())
{
Console. WriteLine ("List is empty ");
Return tmp;
}
If (I <1 | I> last + 1)
{
Console. WriteLine ("Position is error! ");
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 GetElem (int I)
{
If (IsEmpty () | (I <1) | (I> last + 1 ))
{
Console. WriteLine ("List is empty or Position is error! ");
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 ())
{
Console. WriteLine ("List is Empty! ");
Return-1;
}
Int I = 0;
For (I = 0; I <= last; ++ I)
{
If (value. Equals (data [I])
{
Break;
}
}
If (I> last)
{
Return-1;
}
Return I;
}
Public void Reverse ()
{
T tmp = default (T );
Int len = GetLength ();
For (int I = 0; I <= len/2; ++ I)
{
Tmp = data [I];
Data [I] = data [len-i-1];
Data [len-i-1] = tmp;
}
}
}
- Single-chain table
1) An ordered table stores data elements in a linear table in a sequential order using sequential address storage units.
Adjacent data elements are physically adjacent. Therefore, find
Data elements are very convenient, which is an advantage of sequential storage. However, when you insert or delete a sequence table,
It needs to be implemented by moving data elements, which affects the running efficiency. This section describes the storage of a linear table.
The Storage structure is Linked Storage. Such a linear table is called a Linked List ). Linked list not
Logically adjacent data elements are also adjacent to physical storage locations. Therefore, the linked list is inserted.
And does not need to move data elements when deleting the table, but also loses the advantage that sequential tables can be stored randomly.
2) A linked list uses any storage unit to store data elements in a linear table.
It can be continuous or discontinuous ). Then, how does one represent two data elements logically adjacent to each other?
What about the relationship? That is, how to represent the linear relationship between data elements? Therefore, when you store data elements
In addition to storing the information of the data element itself, it also stores the storage address information of the adjacent data element.
These two pieces of information constitute the storage Image of the data element, which is called a Node ). Store Data Elements
The Domain of the information itself is called the Data Domain of the node, which stores Data elements adjacent to it.
The Domain storing address information is called the Reference Domain of the node ). Therefore, a linear table uses each knot
The reference field of the vertex forms a chain, which is the origin of the chain table name.
- Linear table in C #
1) The IList interface indicates a set. Items in the set can be sorted and can be accessed by index.
2) The non-generic IList interface declaration is as follows:
Interface IList: ICollection, IEnumerable
{
// Public attributes
Bool IsFixedSize {get;} // read-only. If IList has a fixed size,
// The value is true. Otherwise, the value is false.
Bool IsReadOnly {get;} // read-only. If IList is read-only,
// The value is true. Otherwise, the value is false.
Object this [Tindex] {get; set;} // indexer to get or set an index
// Public Method
Int Add (object value); // Add an item to the table and return the new inserted item.
// Position in the table.
Void Clear (); // Clear the table.
Int IndexOf (object value); // If the table has items equal to the value,
// Return its index in the Table. Otherwise, return-1.
Bool Contains (object value); // If the table has items equal to the value,
// Return true; otherwise, return false.
Void Insert (int index, object value); // Insert the item with the value to the cable
// Index.
Void Remove (object value); // Delete the first item in the table with the value.
Void RemoveAt (int index); // Delete the items at the index in the table.
}
3)Some collection classes in the. NET Framework implement the IList interface, suchArrayList, ListDictionary,
StringCollection, StringDictionary.
4)ArrayListClass Using arrays to implement IListInterface, so ArrayListIt can be viewed as a sequence table..
The ArrayList capacity can be dynamically increased. Generally, when the elements in the ArrayList are full, the capacity is doubled and the original elements are copied to the new space. When an element is inserted in the ArrayList, the element is added to the end of the ArrayList. The number of elements is automatically increased by 1. In addition, it should be noted that the prerequisite for the operation on elements in ArrayList is that ArrayList is an ordered table, but ArrayList itself is not necessarily an ordered table. Therefore, before performing operations on the elements in the ArrayList, sort the elements in the ArrayList. For more information about sorting algorithms, see Chapter 7th.
5) Example of ArrayList:
Class Program
{
Static void Main (String [] args)
{
// Create and initialize a new ArrayList.
ArrayList myAL = new ArrayList ();
MyAL. Add ("Hello ");
MyAL. Add ("World ");
MyAL. Add ("! ");
// Display the attributes and values of ArrayList
Console. WriteLine ("myAL ");
Console. WriteLine ("Count: {0}", myAL. Count );
Console. WriteLine ("Capacity: {0}", myAL. Capacity );
Console. Write ("Values :");
PrintValues (myAL );
}
Public staticvoid PrintValues (IEnumerablemyList)
{
Foreach (objectobj in myList)
{
Console. Write ("{0}", obj );
Console. WriteLine ();
}
}
}
6) stacks and queues are commonly used data structures in computers and are linear tables with limited operations. Stack insertion
And delete operations are carried out at the top of the stack. It is a linear table first and later. The queue deletion operation is performed in the queue header, while the insert and search operations are performed at the end of the queue. It is a first-in-first-out linear table. Like linear tables, stacks and queues have two storage structures. The stack of sequential storage is called the sequential stack, and the stack of chained storage is called the chain stack. Ordered storage queues are called ordered peer columns, and chained storage queues are called chain queues.