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)

data structure

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.

2.2 single linked list

1. Definition

The linked storage of linear list is also called single linked list. The linked list connects a group of scattered memory blocks together through pointers. The memory blocks are called the "nodes" of the linked list. In addition to storing data, the node of each linked list also needs to record the address of the next node in the chain, which is called the successor pointer next. The linked list has two special nodes, the first node (head node) and the last node (tail node). The head node is used to record the base address of the linked list, which can be used to traverse the whole linked list. The tail node points to an empty address NULL, indicating that this is the last node in the linked list.

Linked list features:

  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.

2. Advantages and disadvantages

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

4. Basic operation

(1) Create a single linked list

Establishing single linked list by head interpolation

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)

An important application of header interpolation: reverse linked list

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

Traversal linked list calculation

Time complexity: O(n)

(6) Code

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

//Define a linked list
//Emphasize that this is a single linked list - use LinkList
//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
} LNode, *LinkList;      //LNode *p == LinkList p
//Pointer variable p: indicates the node address
//Node variable * p: represents a node

/**
 * initialization
 */ 
bool InitList(LinkList &L) {
    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
 */ 
bool isEmpty(LinkList L) {
    if (L->next == NULL)
        return true;
    else
        return false;
}

/**
 * Head insertion
 */ 
LinkList List_HeadInsert(LinkList &L) {
    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
 */
LinkList List_TailInsert(LinkList &L)
{
    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;
}


/**
 * Output linked list
 */ 
void printList(LinkList L) {
    LNode *temp = L->next;
    while (temp != NULL)
    {
        /* code */
        cout << temp->data << endl;
        temp = temp->next;
    }
    
}

/**
 * Insert in bit order
 */ 
bool LinkedListInsert(LinkList &L, int i, int e) {
    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
 */ 
int LinkedListSearchBySite(LinkList L, int i) {
    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
 */ 
int LinkedListSearchByValue(LinkList L, int e) {
    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
 */
void GetLength(LinkList L) {
    LNode *p = L;
    int len = 0;
    while (p->next != NULL)
    {
        /* code */
        p = p->next;
        len++;
    }
    cout << "Table length " << len << endl;
}


int main()
{
    LinkList L; //Declare a pointer to a single linked list
    //Initialize an empty table
    InitList(L);
    // L = List_HeadInsert(L);
    L = List_TailInsert(L);
    // L = List_TailInsert2(L);
    printList(L);
    GetLength(L);
    //Insert element
    LinkedListInsert(L, 4, 520);
    LinkedListInsert(L, 6, 1314);
    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. 💪

Tags: C C++ data structure linked list Singly Linked List

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