Learning notes of data structure (C language version) - Chapter 2 linear table

Posted by unbreakable9 on Mon, 31 Jan 2022 00:31:02 +0100

2.1 definition and characteristics of linear table

definition:

A finite sequence consisting of n (n$\geqslant n (n $\ geqslant $0) ) elements with the same data characteristics is called a linear table.

The number of elements n (n$\geqslant n (n $\ geqslant $0) ) in a linear table is defined as the length of the linear table. When n =0, it is called an empty table.

For a non empty linear table or linear structure, it is characterized by:

(1) There is a unique data element called "first";
(2) There is a unique data element called "last";
(3) Except for the first, each data element in the structure has only one precursor;
(4) Except for the last one, each data element in the structure has only one successor.

2.2 case introduction

Case 2.1

A univariate polynomial \ (P_n(x) \) can be written as follows:

\[P_n(x)=p_0+p_1x+p_2x^2+···+p_nx^n \]

Requirements: realize the addition, subtraction and multiplication of two univariate polynomials.

Univariate polynomials can be uniquely determined by \ (n+1 \) coefficients. Therefore, univariate polynomials \ (P_n(x) \) can be abstracted as an ordered sequence composed of \ (n+1 \) elements, which can be expressed by a linear table \ (P \):

\[P=(p_0,p_1,p_2,···,p_n) \]

At this time, the index \ (* * * I * * * \) of each term is implied in the serial number of its coefficient \ (p_i \).

Suppose \ (Q_m(x) \) is a univariate $m \ (degree polynomial, which can also be expressed by linear table \) Q $:

\[Q=(q_0,q_1,q_2,···,q_m) \]

Without losing generality, let \ (m ≤ n \), then the result of the addition of two polynomials \ (R_n(x)=P_n(x)+Q_n(x) \) can be represented by linear table \ (R \):

\[R=(p_0+q_0,p_1+q_1,p_2+q_2,···,p_m+q_m,p_{m+1},···,p_n) \]

Case 2.2: operation of sparse polynomials

In processing

\[S(x)=1+3x^{1000}+2x^{2000} \]

A linear table with a length of 20001 is used to represent the polynomial, and there are only three elements in the table, which will cause a waste of storage space. Because the elements of a linear table can contain multiple data
The element setting can be changed, and each term of the polynomial can be uniquely determined by (coefficient, exponent).

In general, the univariate polynomial of degree n can be written as

\[P_n(x)=p_1x^{e_1}+p_2x^{e_2}+···+p_mx^{e_m} \]

Where \ (p_i \) is the non-zero coefficient of the term with index \ (e_i \), and

\(0≤e_1<e_2<···<e_m=n\)

If a linear table with a length of \ (m \) and two data items (coefficient term and index term) per element is used

\(((p_1,e_1),(p_2,e_2),···,(p_m,e_m))\)

The polynomial \ (P_n (x) \) can be uniquely determined. In the worst case, if \ (n+1(=m) \) coefficients are not zero, twice as much data is stored as the scheme that only stores each coefficient. However, for thousands of sparse polynomials similar to \ (S(x) \), this representation will greatly save space.

Case 2.3: library information management system

    The publishing house has some book data stored in a text file book.txt For simplicity, it is assumed that each book contains only three parts of information: ISBN (Book number), title and price, and some data in the document. Now it is required to realize a book information management system, including the following six specific functions.

    (1) Find: Based on the specified ISBN Or book title to find the relevant information of the corresponding book, and return the position serial number of the book in the table.
    (2) Insert: insert a new book information.
    (3) Delete: delete a kind of book information.
    (4) Modify: according to the specified ISBN, Modify the price of the book.
    (5) Sort: sort the books according to the price from low to high.
    (6) Count: count the number of books in the book list.
    To realize the above functions, like the polynomial in the above case, we first abstract it into a linear table according to the characteristics of the book table, and each book is used as an element in the linear table. Then we can use the appropriate storage structure to represent the linear table, and design and complete the relevant functional algorithms on this basis. The specific storage structure can be determined according to the advantages and disadvantages of the two different storage structures and the actual situation.

2.3 type definition of linear table

Linear table is a very flexible data structure. Its length can be increased or shortened according to needs, that is, the data elements of linear table can not only be accessed, but also be inserted and deleted. The abstract data type definition of linear table is given below:

ADT List{

Data object: \ (D=\{a_i|A_i ∈ ElemSet,i=1,2, ···, n,n ≥ 0 \} \)

Data relationship: \ (r = \ {< a {I-1}, a _i > | a {I-1}, a _i ∈ D,i=2, ····, n \} \)

Basic operation:

InitList (&L);//Operation result: construct an empty linear table L
DestroyList(&L);//Initial condition: linear table L already exists
                //Operation result: destroy linear table L
ClearList (&L);//Initial condition: linear table L already exists
               //Operation result: reset L to empty table
ListEmpty(L);//Initial condition: linear table L already exists
             //Operation result: if L is an empty table, return true; otherwise, return false
ListLength(L); //Initial condition: linear table L already exists
               //Operation result: returns the number of data elements in L
GetElem(L,i,&e); //Initial condition: linear table l already exists, and 1 < = I < = listlength (L).
                 //Operation result: use e to return the value of the first data element in L
LocateElem(L,e); //Initial condition: linear table L already exists
                 //Operation result: returns the position of the first element in L with the same value as e in L. If such a data element does not exist, the return value is 0.
PriorElem(r,,cur_e,&pre_e); //Initial condition: linear table L already exists
                            //Operation result: if cur_ If e is the data element of L and is not the first, use pre_e returns its precursor, otherwise the operation fails, pre_e no definition.
NextElem(L,cur_e,&next_e); //Initial condition: linear table L already exists
                           //Operation result: if cur_ If e is the data element of L and not the last one, use next_e returns its successor, otherwise the operation fails, next_e no definition.
Listinsert(&L,i,e); //Initial condition: linear table l already exists, and 1 < = I < = listlength (L) + L
                    //Operation result: insert a new data element E before the first position in L, and add 1 to the length of L
ListDelete(&L,i); //Initial condition: linear table l already exists and is not empty, and l < = I < = listlength (L)
                  //Operation result: delete the first data element of L, and the length of L is reduced by 1
TraverseList(L); //Initial condition: linear table L already exists.
                 //Operation result: traverse the linear table L, and visit each node of L once in the traversal process

}ADT List

(1) Abstract data type is only the definition of a model and does not involve the specific implementation of the model. Therefore, the parameters involved in the description here do not need to consider the specific data type. In practical application, there may be many types of data elements. At that time, different data types can be selected according to specific needs.
(2) The operations given in the above abstract data types are only basic operations, which can constitute other more complex operations. For example, the two application examples in Section 2.2, whether the operation of univariate polynomials or the management of books, first need to read in the data elements and generate a linear table including the required data, which belongs to the creation of linear table. This operation can first call lnitlist (& L) in the basic operation definition to construct an empty linear table L, and then repeatedly call listlnsert (& L, I, e) to insert element E in the table to create a required linear table. Again, yes
The operation of univariate polynomial can be regarded as the merging of linear tables, and the merging process needs to insert elements continuously. Other operations such as splitting and copying linear tables can also be realized by using the combination of the above basic operations.
(3) For different applications, the interfaces of basic operations may be different. For example, in the delete operation of case 2.2, if it is required to delete the book with ISBN X in the book table, first determine the position of the book in the linear table according to x, and then delete the book record from the table by using the basic operation ListDelete (& L, I).
(4) The linear table defined by the abstract data type can be expressed and implemented according to the actual storage structure.

2.4 sequential representation and implementation of linear table

2.4.1 sequential storage representation of linear table

2.4.2 realization of basic operations in sequence table

It can be seen that some operations are easy to implement when the linear table is represented by the sequence table defined above. Because the length of the table is an "attribute" of the sequential table, the operation of calculating the table length can be realized by returning the value of length. Whether the table is empty can be determined by judging whether the value of length is 0. The time complexity of these operation algorithms is O(1). The implementation of several other main operations of the sequence table is discussed below.

1. Initialization

The initialization operation of sequence table is to construct an empty sequence table.

Algorithm 2.1 initialization of sequence table

[algorithm steps]

① Dynamically allocate an array space of predefined size for the sequence table L, so that elem points to the base address of this space.

② Set the current length of the table to 0.

[algorithm description]

Status InitList(SqList &L)
{
  L.elem= new ElemType[MAXSIZE];  //Allocate an array space of MAXSIZE for the sequential table
  if (! L. elem) exit (OVERFLOW); //Storage allocation failed exit
  L.length=O;                     //Empty table length is 0
  return OK; 
}

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define MAXSIZE 20

/*Define sequence table*/
typedef struct {
  int date[MAXSIZE];
  int length;
}SqList;

/*Initialization sequence table*/

int InitList(SqList &L)
{
  
  if(!L.date) 
  { 
    printf("Initialization failed!\n");
    return 0;
  }
  L.length = 0;
  printf("Initialization succeeded!\n");
  return 1;
}

/*Establish sequence table*/
int CreatList(SqList L, int a[], int n)
{
  if (n > MAXSIZE) 
  {
    printf("Insufficient space, unable to create sequence table! \n");
    return 0;
  }
    
  for (int i = 0; i < n; i++)
    L.date[i] = a[i];
  L.length = n;
  return 1;
}
/*Traversal operation*/
void PrintList(SqList L)
{
  for (int i = 0; i < L.length; i++)
    printf("%d ", (L.date[i]));
}

/*Value (search by bit)*/
int Getdate(SqList L,int i,int *e)
{
  if (i<1 || i>L.length)
  {
    printf("Illegal search location, search error\n");
    return 0;
  }
  else
  {
    *e = L.date[i];
    printf("%d", e);
    return 1;
  }
}
/*Find (find by value)*/
int Locatedate(SqList L, int e)
{
  for(int i = 0; i < L.length; i++)
    if (L.date[i] == e) return i + 1;
    return 0;
}

/*insert*/
int ListInsert(SqList L, int i, int e)
{
  if ((i < 1) || (i > L.length + 1)) return 0;
  if (L.length == MAXSIZE) return 0;
  for (int j = L.length - 1; j >= i - 1; j--)
    L.date[j + 1] = L.date[j];
  L.date[i - 1] = e;
  ++L.length;
  return 1;
}
/*delete*/
int ListDelete(SqList L, int i)
{
  if ((i < 1) || (i > L.length)) return 0;
  for (int j = i; j <= L.length - 1; j++)
    L.date[j - 1] = L.date[j];
    --L.length;
    return 1;
}
int main()
{
  int a[5] = { 1,2,3,4,5 };
  int* p=new int;
  SqList List1;
  InitList(List1);//initialization
  printf("Assign values to the sequence table: 1 2 3 4 5\n Traverse and output the sequence table:\n");
  CreatList(List1, a, 5);//establish
  PrintList(List1);//Traverse the output sequence table
  printf("\n Take the value of the third digit:\n");
  Getdate(List1, 3, p);

}