Linear table (data structure)

Keywords: data structure linked list

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;
}

3. Linked list
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.
Linear linked list classification:
Single linked list
Circular linked list
Bidirectional linked list
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)
Single linked list
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.
Single linked list structure definition:

typedef char datatype;
typedef struct node     //Linked list node 
{
	datatype data;        //Node data field 
	struct node *link;     //Node chain domain 
}linknode,*linklist;       //Chain head pointer 

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 linked list
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;
while(p->link!=first&&k<i)   //Failed to return to the header node 
{
	p=p->link;    //Otherwise, p refers to the target 
	k++;
}

Bidirectional linked list
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;
p - > llink - > rlink or p - > rlink - > llink indicates the p node itself.
Definition of circular double linked list:

typedef int datatype;
typedef struct node   // Node definition 
{
	datatype data;     //Node data 
	int freq;          //Access count 
	struct node *llink,*rlink;   //Pointer 
}dblcnode,*dbllist;     //Bidirectional linked list 

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);
	}
	first->llink=first->rlink=first;
	first->freq=0;
}

Posted by dfownz on Sat, 18 Sep 2021 14:37:48 -0700