# Data structure - linear form linked list

First blog post - linear table (I) basic concepts and basic operations
Basic concept and operation of linear table (I)

# Linear table (II) single chain table

## Mind map   ## 2.1 chain representation of linear table

When storing a linear list in a chain, there is no need to use a storage unit with continuous addresses. The location of nodes in the memory is arbitrary, that is, logically adjacent data elements are not necessarily adjacent physically.

### 1. Definition

1. Linked list is stored in the form of nodes, which is chain storage.
2. Each node contains the data field, and the next field: points to the next node.
3. The nodes of the linked list are not necessarily stored continuously.
4. The linked list is divided into the linked list with the leading node and the linked list without the head node, which is determined according to the actual needs.

Related terms:

1. The header pointer is a pointer to the first node in the linked list.

2. When the head node wants to represent a single linked list, it only needs to declare a head pointer L, pointing to the first node of the single linked list, which is a node attached before the first node of the linked list; In the data field, only information such as table flag and table length is available.

3. The first element node refers to the node where the first data element a1 is stored in the linked list.

Advantages: large continuous space is not required, and it is convenient to change the capacity
Disadvantages: it can not be accessed randomly. It takes a certain space to store the pointer

### 3. Structure diagram

(1) Memory structure diagram (2) Logic structure diagram ### (1) Create a single linked list

This method starts with an empty table, generates a new node, and inserts the new node into the header of the current linked list, that is, after the header node.

Average time complexity: O(n) Establishing single linked list by tail interpolation

This method inserts the new node into the tail of the current linked list. Therefore, a tail pointer must be added to make it always point to the tail node of the current linked list. ### (2) Insert single linked list

Insert in bit order

Listinsert (& L, i, e): insert operation. Insert the specified element E at position i in table L.

Average time complexity: O(n) Perform a forward interpolation operation on a node

Time complexity: O(1) Post insert a node

Time complexity: O(1) ### (3) Delete single linked list

Delete in bit order

ListDelete & L, i, & e: delete operation. Delete the element at position i in Table L and return the value of the deleted element with e.

Average time complexity: O(n) Deletion of specified node

Time complexity: O(1) ### (4) Find single linked list

Search by bit

GetElem(L,i): bitwise lookup operation. Gets the value of the element at position i in table L.

Average time complexity: O(n)

Find by value

Locaterelem (L, e): find operation by value. Finds an element with a given keyword value in table L.

Average time complexity: O(n)

### (5) Table length

Time complexity: O(n)

### (6) Code

```#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

//Emphasize that this is a node - use LNode*
typedef struct LNode //Define single linked list node type
{
/* data */
int data;  //Data domain
struct LNode *next;  //Pointer field
//Pointer variable p: indicates the node address
//Node variable * p: represents a node

/**
* initialization
*/
L = (LNode *)malloc(sizeof(LNode));
if (L == NULL)  //Insufficient memory, allocation failed
return false;
L->next = NULL;    //There is no node after the head node
//L = NULL; // Empty table
return true;
}

/**
* Determine whether the linked list is empty
*/
if (L->next == NULL)
return true;
else
return false;
}

/**
*/
LNode *s;  //New node
for (int i = 1; i <= 10; i++)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = i; //data
s->next = L->next;
L->next = s; //Insert the new node into the table, and L is the header pointer
/* code */
}
return L;
}

/**
* Tail interpolation
*/
{
LNode *s; //New node
LNode *r = L; //Tail pointer
for (int i = 1; i <= 10; i++)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = i; //data
r->next = s;
r = s; //r points to the new epitope
/* code */
}
r->next = NULL; //Tail node pointer null
return L;
}

/**
*/
LNode *temp = L->next;
while (temp != NULL)
{
/* code */
cout << temp->data << endl;
temp = temp->next;
}

}

/**
* Insert in bit order
*/
if(i < 1)
return false;
LNode *p = L;
int j = 0;
while(p != NULL && j < i - 1) {
p = p->next;
j++;
}
if(p == NULL)
return false;
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
cout << "In the first " << i << " position" << "The inserted value is:" << e << endl;
cout << "Insert succeeded!" << endl;
return true;
}

/**
* Post insert a node
*/
bool InsertNextNode(LNode *p, int e) {
if(p == NULL)
return false;
LNode *s = (LNode *)malloc(sizeof(LNode));
if (s == NULL)
return false;
s->data = e;
s->next = p->next;
p->next = s;
cout << "Insert succeeded!" << endl;
return true;
}

/**
* Perform a forward interpolation operation on a node
*/
bool InsertPreNode(LNode *p, int e)
{
if (p == NULL)
return false;
LNode *s = (LNode *)malloc(sizeof(LNode));
if(s == NULL)
return false;
s->next = p->next;
p->next = s;
s->data = p->data;
p->data = e;
cout << "Insert succeeded!" << endl;
return true;
}

/**
* Delete by bit
*/
bool ListDelete(LinkList &L, int i) {
if(i < 1)
return false;
LNode *p = L;
int j = 0;
while(p != NULL && j < i - 1) {
p = p->next;
j++;
}
if(p == NULL || p->next == NULL)
return false;
LNode *q = p->next;
int e = q->data;
p->next = q->next;
free(q);  //release
cout << "Delete paragraph " << i << " position" <<"The value is:" << e << endl;
cout << "Delete succeeded!" << endl;
return true;
}

/**
* Deletes the specified element
*/
bool DeleteByNode(LNode *p)
{
if (p == NULL)
return false;
LNode *q = p->next;
p->data = p->next->data;
p->next = q->next;
free(q); //release
cout << "Delete succeeded!" << endl;
return true;
}

/**
* Search by bit
*/
cout << "Find page " << i << " The value of bit is:";
int j = 1;
LNode *p = L->next; //Get header node
if(i < 0)
return 0;
while (p != NULL && j < i)
{
/* code */
p = p->next;
j++;
}
return p->data;

}

/**
* Find by value
*/
cout << "The lookup value is " << e << " The location of the is:";
LNode *p = L->next; //Get header node
int i = 1;
while (p != NULL && p ->data != e)
{
/* code */
p = p->next;
i++;
}
return i;

}

/**
* Find table length
*/
LNode *p = L;
int len = 0;
while (p->next != NULL)
{
/* code */
p = p->next;
len++;
}
cout << "Table length " << len << endl;
}

int main()
{
//Initialize an empty table
InitList(L);
L = List_TailInsert(L);
// L = List_TailInsert2(L);
printList(L);
GetLength(L);
//Insert element
printList(L);
//Delete element
ListDelete(L, 2);
printList(L);
//lookup
cout << LinkedListSearchBySite(L, 4) << endl;
cout << LinkedListSearchByValue(L, 8) << endl;
return 0;
}
```
```1
2
3
4
5
6
7
8
9
10
Table length is 10
Insert the value 520 in bit 4
Insert succeeded!
Insert the value in bit 6 as 1314
Insert succeeded!
1
2
3
520
4
1314
5
6
7
8
9
10
Delete the second digit as: 2
Delete succeeded!
1
3
520
4
1314
5
6
7
8
9
10
Find the value of bit 4: 4
The location where the lookup value is 8 is: 9
```

come on. come on. come on. 💪

Posted on Fri, 19 Nov 2021 01:49:54 -0500 by noclist