Data Structure 2 - Linear Table 1: Basic Concepts

1. The concept of linear table

A linear list is a finite sequence of data elements with the same properties. It's like stringing beads on a thin thread, one after the other.
Please add a picture description

2. Logical characteristics of linear tables

① In the non-empty linear table, there is one and only one start node a1, which has no direct predecessor, but only one direct successor a2, which is equivalent to a1 being the beginning, followed by the connection a2

② There is and only one terminal node a(n), which has no direct successor, but only one direct predecessor a(n-1), which is equivalent to a(n) being the end, followed by a(n-1)

③ The remaining nodes have and only have one direct predecessor and one direct successor

Practical application of linear tables

1) For example, the following polynomial, to achieve addition and subtraction
f ( x ) = 10 x 5 + 4 x 3 + 7 x 2 + 2 x + 6 f(x)=10x^5+4x^3+7x^2+ 2x+6f(x)=10x5+4x _3+7x _2+2x _+6
It can be converted into
R = ( p 0 , p 1 , p 2 , ⋅ ⋅ ⋅ , pn ) R=(p_0,p_1,p_2,···,p_n)R=(p0,p1,p2,⋅⋅⋅,pn)
Each p corresponds to a coefficient, and the x index can be displayed by the subscript of p

So the addition of polynomials can be converted into
R = ( p 0 + q 0 , p 1 + q 1 , p 2 + q 2 , ⋅ ⋅ ⋅ , pn + qn ) R=(p_0+q_0,p_1+q_1,p_2+ q_2,···,p_n+q_n)R=(p0+q0,p1+q1,p2+q2,⋅⋅⋅,pn+qn)
2) If we further want to implement the addition and subtraction of sparse polynomials (that is, the exponents are relatively scattered, if it is still the same as before, it will take up a lot of memory)

So we construct the following linear table:
R = ( ( p 0 , e 0 ) , ( p 1 , e 1 ) , ( p 2 , e 2 ) , ⋅ ⋅ ⋅ , ( pn , en ) ) R=((p_0 ,e_0),(p_1,e_1),(p_2,e_2),···,(p_n,e_n))R=((p0,e0),(p1,e1),(p2,e2),⋅⋅⋅,(pn,en))
In this way, both the index and the coefficient of the linear table are stored

step:

  • create new array C

  • The exponents are the same, and the corresponding coefficients are added. If the sum is not 0, a new item is added to the C linear table

  • different exponents, copy the item with the smaller exponent into C

  • When a polynomial has been traversed, copy the other remaining items to C in turn

Arrays can also be used for this question. The following figure is a schematic diagram of a linked list implementation

Please add a picture description

Problems with sequential storage structures:

  1. Inflexible storage space allocation

  2. The space complexity of the operation is high

Type definition for linear table

ADT List{
Data object: D={a(i)|a(i) belongs to Elemset, (i=1,2,...,n,n≥0)
Data relationship: R={<a(i-1),a(i)>|a(i-1),a(i) belongs to D,(i=2,3,…,n)} // binary relation
Basic operation:
InitList(&L);
DestoryList(&L);
ListInsert(&L,i,e);
ListDelete(&L,i,&e);
······
}ADT List

Basic operation:

  • Linear list initialization: InitList(&L)

Operation result: Construct an empty linear list L

  • Linear list destruction: DestoryList(&L)

Initial condition: the linear table L already exists

Operation result: destroy the linear table L

  • Linear list clearing: ClearList(&L)

Initial condition: the linear table L already exists

Operation result: reset the linear table L to an empty table

  • Clear the linear table: ListEmpty(L)

Initial condition: the linear table L already exists

Operation result: If the linear table L is an empty table, return TRUE; otherwise return FALSE

  • Linear table length: ListLength(L)

Initial condition: the linear table L already exists

Operation result: return the number of data elements in the linear table L

  • Linear table lookup: GetElem(L,i,&e)

Initial conditions: the linear list L already exists, 1≤i≤ListLength(L)

Operation result: use e to return the value of the i-th data element in the linear table L

  • Linear table comparison: LocateElem(L,e,compare())

Initial conditions: the linear table L already exists, and compare() is the decision function of the data elements

Operation result: returns the bit sequence of the first data element in L that satisfies compare() with e. If such an element does not exist, the return value is 0

  • Linear table predecessor method: PriorElem(L,cur_e,&pre_e)

Initial condition: the linear table L already exists

Operation result: if cur_e is a data element of L and not the first one, use pre_e to return its predecessor, otherwise the operation fails; pre_e is meaningless.

  • Linear table successor method: NextElem(L,cur_e,&next_e)

Initial condition: the linear table L already exists

Operation result: if cur_e is a data element of L and not the last one, use next_e to return its successor, otherwise the operation fails; next_e is meaningless.

  • Linear table element insertion: ListInsert(&L,i,e)

Initial conditions: the linear list L already exists, 1≤i≤ListLength(L)+1

Operation result: insert a new data element e before the i-th position of L, and increase the length of L by one

  • Linear table element deletion: ListDelete(&L,i,&e)

Initial conditions: the linear list L already exists, 1≤i≤ListLength(L)

Operation result: delete the i-th data element of L, and return its value with e, and decrease the length of L by one

  • Linear table traversal: ListTraverse(&L,visited())

Initial condition: the linear table L already exists

Operation result: call visited() on each element in the linear list in turn

Guess you like

Origin blog.csdn.net/m0_61787307/article/details/129403908
Recommended