# Chain representation of linear list — single chain list

Time：2021-7-20

### 1. Definition:

Data elements in a linear table are stored in a set of arbitrary storage units. In order to establish a linear relationship between data elements, each linked list node needs to store not only the information of the element itself, but also a node pointing to the next node Structure:

``````typedef int ElemType;
Typedef struct lnode {// defines the node type of a single linked list
ElemType data;           // Each node stores a data element
struct LNode *next;      // The pointer points to the next node
} LNode, *LinkList;          // Emphasize that this is a single linked list using linklist, and that nodes use lnode*``````

### 2. Create a single linked list

Insert a new node into the header of the current list: Code implementation:

``````/**
* @param L
* @return
*/
LNode *s;
int x;
L->next = NULL;                        // The initial list is empty
scanf("%d", &x);                 // Enter the value of the node
while (x !=  9999) {// enter 9999 to end
s = (LNode *) malloc(sizeof(LNode)); // Create a new node
s->data = x;
s->next = L->next;
L->next = s;                        // Insert the new node into the table with L as the header pointer
scanf("%d", &x);
}
return L;
}``````

When a single linked list is established by using the head insertion method, the order of reading in the data is opposite to that of the elements in the generated linked list

#### 2.2 establishing single linked list with tail insertion method

The new node is inserted into the end of the current list. This method requires a tail pointer to point to the last node of the linked list Code implementation:

``````/**
*Establishing single linked list with tail insertion method
* @param L
* @return
*/
int x;                                  // Let ElemType be an integer
LNode *s, *r = L;                          // R is the tail pointer
scanf("%d", &x);                  // Enter the value of the node
while (x != 9999) {
s = (LNode *) malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;                               // R points to the new tail node
scanf("%d", &x);
}
r->next = NULL;                         // Tail node pointer null
return L;
}``````

### 3. Basic operation

#### 3.1 insert an element before the specified node

Implementation schematic diagram: Code implementation:

``````/**
*Insert element E before P node
* @param p
* @param e
* @return
*/
bool InsertPriorNode(LNode *p, ElemType e) {
if (p == NULL) {
return false;
}
LNode *s = (LNode *) malloc(sizeof(LNode));
if (s == NULL) {
return false;
}
s->next = p->next;
p->next = s;          // The new node s is connected after P
s->data = p->data;    // Copy elements from P to s
p->data = e;        // Change the element in P to E
return true;
}``````

#### 3.2 insert after the specified node Code implementation:

``````/**
*The element E is inserted after the P node
* @param p
* @param e
* @return
*/
bool InsertNextNode(LNode *p, ElemType e) {
if (p == NULL) {
return false;
}
LNode *s = (LNode *) malloc(sizeof(LNode));
If (s = = null) {// memory allocation failed
return false;
}
s->data = e;        // Saving data element E with node s
s->next = p->next;  //  Pay attention to the sequence
p->next = s;
return true;
}``````

#### 3.3 deletion in bit order Code implementation:

``````/**
*Delete in bit order, leading node
* @param L
* @param i
* @param e
* @return
*/
bool ListDelete(LinkList &L, int i, ElemType e) {
if (i < 1) {
return false;
}
LNode *p;       // Pointer P points to the node currently scanned
int j = 0;      // Which node does P point to
p = L;            // L points to the head node. The head node is the 0 th node and does not store data
while (p !=  Null & & J < I - 1) {// loop finds the i-1st node
p = p->next;
j++;
}
If (P = = null) {// I value is illegal
return false;
}
If (P - > next = = null) {// there is no other node after the I-1 node
return false;
}
LNode *q = p->next;     // Let Q point to the deleted node
e = q->data;            // Use e to return the value of the element
p->next = q->next;      // Disconnect the * q node from the chain
free(q);                // Release the storage space of the node
return true;
}``````

#### 3.4 bit by bit search

``````/**
*Search by bit, return the ith element, and take the lead node
* @param L
* @param i
* @return
*/
LNode *GetElem(LinkList L, int i) {
if (i < 0) {
return NULL;
}
LNode *p;       // Pointer P points to the node currently scanned
int j = 0;      // Which node does P point to
p = L;          // L points to the head node. The head node is the 0 th node and does not store data
while (p !=  Null & & J < I) {// loop finds the ith node
p = p->next;
j++;
}
return![image.png](https://segmentfault.com/img/bVcSqxv)``````

## As package jar and AAR

Jar packaging (only class files and manifest files are included, not resource files, such as pictures and all files in Res) 1. Create a new project in as and a new model module. Add the model dependency to build. Gradle in the project, and then compile the project. Then add the following configuration in build.gradle […]