Data structure array and linked list

Posted by revbackup on Tue, 07 Dec 2021 08:18:30 +0100

1, Linear table

1. Linearity table
The linear structure of an ordered sequence composed of data elements of the same type. The starting position of a table is called the header, and the ending position of a table is called the footer.
Except that the first element has no direct precursor and the last element has no direct successor, every other data element has a precursor and successor.
2. Generalized table
Generalized table is a generalization of linear table. For a linear table, n elements are basic single elements, but in a generalized table, these elements can be not only a single element, but also another linear table or even a generalized table.

2, Sequential storage structure (array)

1. Sequential storage structure: it refers to that the data elements of the linear table are sequentially stored in a storage unit with continuous addresses, and the intermediate data has front-end and back-end, that is, the logical structure is adjacent and the physical structure is adjacent.
2. Advantages:

  • The storage density is large (the storage occupied by the node itself / the storage occupied by the node structure), and there is no need to add additional storage space to represent the logical relationship between the elements in the table.
  • It can quickly access the elements at any position in the table. When saving and reading data, the time complexity is O(1), that is, random access.

3. Disadvantages:

  • Once the capacity of a linear table is defined, it is difficult to expand.
  • When inserting and deleting elements of a linear table, a large number of elements need to be moved, which wastes time.
  • When the length of linear table is uncertain, the maximum storage space must be allocated to make full use of the storage space.

3, Linked storage structure (single linked list)

1. Chain storage structure: the location of nodes in the memory is arbitrary, that is, logically adjacent data elements are not necessarily adjacent physically. The chain representation of linear list is also called non sequential image or chain image.
2. Basic information:

  • The basic unit of the linked list is the node. Nodes can be divided into data field and pointer field. The field storing the data element information is the data field, and the field storing the direct successor position is the pointer field.
  • The storage location of the first node in the linked list is called the head pointer, and each subsequent node is the location pointed to by the previous subsequent pointer.
  • In order to operate the linked list more conveniently, a node is attached in front of the first node of the single linked list, which is called the head node. The pointer field of the head node stores the pointer to the first node.

Note: generally, the head node should be specified.

3. Advantages: large continuous space is not required, and it is convenient to change the capacity. Delete element convenience

4. Disadvantages: it can not be accessed randomly. It takes a certain space to store the pointer.

Single linked list: (I'll change it here)

struct node {
	int data;
	node *next;
	node(int data = 0,node *next = NULL) {
		this->data = data;
		this->next = next;
	}
};

node *head,*q;     //Head: head pointer. The linked list counts from 1 
    	           //q: Execution pointer, p is temporary pointer 
void init() {
	*head = new node();
}
void print() {       //Output single linked list 
	node *q = head -> next;
	while(q != NULL) {
		printf("%d",q->data);
		q = q->next;
	}
    puts("");
} 

bool insert(int n,int k) {  //Insert in the nth position. If it fails, 0 is returned 
	node *q=head;
	int t = 0; //Position 0	
	while(q != NULL) {	
		if(t == n-1) {
			node *p = new node();
			p->data = k;			
			p->next = q->next;			
			q->next = p;
			return 1;
		} 
		q = q->next;
		t ++;
	}
	return 0;  
} 

void push_back(int k) {  //Insert tail       
	node *q = head->next; 
	while(q != NULL) {
		q = q->next;
	}
	node *p= new node();
	p->data = k;
	p->next = NULL;
	q->next = p;
}

bool erase(int n) {  //Delete the nth position and return 0   
	node *p= new node();
	node *q = head->next;
	int t = 0; //Position 0	
	while(q != NULL) {	
		if(t == n-1) {					
			p = q;
		} 
		if(t == n) {
			p->next = q->next;
		}
		q = q->next;
		t ++;
	}
	return 0;
}

4, STL variable length array vector

1.vector concept
vector is a sequential container of variable length array. Its internal implementation is based on the idea of multiplication and uses continuous storage locations to store elements. Compared with arrays, vectors consume more memory in exchange for the ability to manage storage and dynamic growth.
vector supports random access to O(1), but it does not support inserting O(n) anywhere. In order to ensure efficiency, the addition and deletion of elements should be carried out at the end.
Note: header file #include < vecto r >

2.vector operation
(1) Initialization

vector<Type> v              v Is an empty vector,Perform default initialization
vector<Type> v(n)           v Contains n An object that repeatedly performs default initialization of values
vector<Type> v[n]           Create an irregular two-dimensional array v,behavior n,Column uncertainty

(2) Basic operation

bool v.empty()               If v If it does not contain any elements, it returns true, otherwise it returns false
int  v.size()                return v Number of elements in
void v.clear()               Empty array
type v.front()               Return array v First element of
type v.back()                Return array v Last element of

(3) Add, delete, modify and query

void v.push_back(k)           towards v Add a value of k Element of
void v.pop_back()             Delete tail element
void v.insert(v.begin()+i,k)  In the first i Insert before elements k
void v.erase (v.begin()+l,v.begin()+r)  Delete interval[l,r)

(4) Operator

v1 = v2           use v2 Copy replacement of elements in v1 Elements in
v1 == v2          True if and only if they have the same number of elements and the element values at the corresponding positions are the same
< , <= , > , >=   Compare in dictionary order
v[i]              Return to page i Number of positions, starting from 0

5, STL linked list

1.list concept
List is a two-way linked list of data structures, and its memory space can be discontinuous. Through the pointer to access data, it can efficiently delete and insert people anywhere. The insertion and deletion operations are constant time. The list and vector operations are basically similar.
Note: header file #include

6, STL Dual Ended queue deque

Double ended queue is a random access data type, which provides the function of fast insertion and deletion at both ends of the sequence. deque is similar to vector.
It is a double ended queue class template. The double ended queue container is composed of several blocks. The element addresses in each block are continuous, and the addresses between blocks are discontinuous. There is a specific mechanism to form these blocks into a whole. You can quickly insert and delete elements from the front or back, and you can randomly access element O(1), but it is slow to delete elements. (vetor is multiplication thought)
Note: header file #include < deque >

type s.push_front(K)          Insert element in queue header K
type s.push_back(K)           Insert element at end of queue K
void s.pop_front()            Delete the first element of the queue
void s.pop_back()             Delete an element at the end of the queue

7, Compare

1. Comparison between deque and vector
There are more double ended queues than vector s. You can push and pop at both ends, but the disadvantage is that it takes up more memory.
2. Comparison between list and vector
list and vector have the opposite advantages and disadvantages, and their application scenarios are different.
vector: fewer insert and delete operations and frequent random access to elements.
list: frequent insertion and deletion, less random access.
3. Comparison between vector and list and deque
If efficient random access is required, regardless of the efficiency of insertion and deletion, use vector.
If you need a lot of inserts and deletions and don't care about random access, you should use list.
If random access is required and the insertion and deletion of data at both ends are concerned, deque should be used.

Topics: Algorithm data structure linked list