# Linear table (data structure)

## Linear table

1. Linearity table
Definition: a finite sequence of n (> = 0) data elements, marked as L = (a1, a2,..., an). (ai is the data element, n is the table length, a1 is the first element, and an is the tail element)
characteristic:
1. Except the first element, each other element has and only has a direct forward trend.
2. Except the last element, every other element has only one direct successor.
main points:
The elements in the table have logical order. In the sequence, each element is arranged in its order, with unique first and last elements;
The number of elements in the table is limited;
All the elements in the table are data elements, that is, each table element is atomic data. It is not allowed to set tables in the table.
The data types of the elements in the table are the same. This means that the elements in each table occupy the same storage space.
2. Sequence table
Definition: the elements in a linear table are successively stored in a continuous storage space to form a sequential table. It is the sequential storage representation of a linear table, and the storage structure can be described by a one bit array.
**Features: * * the logical order of elements is consistent with the physical order;
It can be stored sequentially and accessed directly by subscript.
Sequential table continuous storage mode:
LOC(i)=LOC(i-1)+l=a+i*l
LOC is the element storage location and l is the element size.
Static structure definition:

```#define maxSize 100 / / maximum allowable length
typedef int dataType; //Element data type
typedef struct{
dataType data[maxSize];     //Storage array
int n;          //Number of current table elements
}SeqList;
```

The sequence table is statically defined. It is assumed that L is a sequence table of type SeqList, and L.data[i] is generally used to access it.
Once the table is full, it cannot be expanded.
Dynamic structure definition

```#define initSize 100 / / maximum allowable length
typedef int dataType; //Element data type
typedef struct{
dataType *data;     //Storage array
int n;          //Number of current table elements
int maxSize;    //Maximum length of table
}SeqList;
```

The sequence table is dynamically defined. It can be expanded, and the new size is included in the data member maxSize.
Implementation of basic operation of sequence table
Construct an empty sequence table:

```void initList(SeqList&L)
{
L.data=(dataType*)malloc(initSize*sizeof(dataType));
if(L.data==NULL)
{
printf("Storage allocation failed!\n");
exit(1);
}
L.n=0;
L.maxSize=initSize;
}
```

Linear linked list is the link storage representation of linear list. The logical order in the middle of elements is represented by link pointers in each node.
The first element node in the linked list is called the first element node, and the last node is called the tail node. (the first element node is not the head node)
Features: each element (table item) is composed of node s.
Nodes can be stored continuously or discontinuously.
The logical order of nodes may not be consistent with the physical order.
Tables can be expanded.

```typedef char datatype;
typedef struct node     //Linked list node
{
datatype data;        //Node data field
struct node *link;     //Node chain domain
```

When defining the actual linked list, you only need to define the chain header pointer.

```linklist first; //Chain header pointer
```

In the linked list, if the overloaded "+ +" function is not defined, statements such as p + + cannot be used to proceed to the next logical node. Generally, p = p - > link is used to proceed to the next node.
Circular single linked list is a variant of single linked list. The link pointer of the end node of the linked list is not NULL, but points to the front end of the table. In order to simplify the operation, the head node is added to the circular single linked list. The empty judgment condition of circular single linked list is: first - > link = first.
Features: as long as you know the address of a node in the table, you can search the addresses of all other nodes. In the search process, the link field of no node is empty.
The implementation of all operations of the circular single linked list is similar to that of the single linked list. The difference is that the tail of the chain is detected, and the pointer is not NULL, but returns to the head of the chain.
Structure definition of circular single linked list:

```typedef int datatype;
typedef struct node   // Circular linked list definition
{
datatype data;     //Node data
struct node *link;   //Successor node pointer
}circnode,*circlist;
```

The operation of locating the pointer p at the ith node in the linked list is as follows:

```circnode *p=first; //firsr is the header node
int k=0;
{
p=p->link;    //Otherwise, p refers to the target
k++;
}
```

A two-way linked list refers to a linear linked list that can be traversed in both the forward and subsequent directions. The structure of each node of the two-way linked list is: The two-way linked list usually adopts the form of circular double linked list of leading nodes, and each node is in two chains: Node pointing:
P - > llink (left) indicates the leading node of node p;
P - > rlink (right) indicates the successor node of node p;
Definition of circular double linked list:

```typedef int datatype;
typedef struct node   // Node definition
{
datatype data;     //Node data
int freq;          //Access count
```

The time complexity of finding node successor in single linked list is O (1), and the time complexity of finding node forward trend is O (n); while the time complexity of finding successor and forward trend in two-way linked list is O (1).
Create an empty circular double linked list:

```void initdbllist(dbllist&first)
{
first=(dblnode*)malloc(sizeof(dblnode));
if(first=NULL)
{
printf("Storage allocation failed!\n");
exit(1);
}