[The tenth day of the C language inspector training camp] Three articles understand the linear table in the data structure (1) ----- addition, deletion, modification, query and destruction of the sequence table (array)

foreword

It will be divided into three blogs, introducing the linear table in the data structure from the shallower to the deeper. The linear table is the most commonly used data structure in the data structure and the most basic data structure. The realization of the stack and queue and the binary tree that will be learned in the future , graphs, etc. will use the linear table structure. It will be divided into three blogs to introduce the linear table in detail. This blog mainly talks about the principle of the sequence table, the initialization, insertion, deletion, and search of the sequence table, and explains the implementation principle of the linked list.

Before officially introducing the linear table, you can take a look at the real postgraduate entrance examination questions about the linear table!
2010 (sequence list)
42. (13 points) Assume that n (n > 1) integers are stored in a one-dimensional array R. Try to design an algorithm that is as efficient as possible in terms of both time and space. Move the sequence saved in R to the left by p (0 < p < n) positions, that is, transform the data in R from (X0, X1,  , Xn1) to (Xp, Xp+1, , Xn 1, X0, X1, , Xp1). Require:

  • 1) Give the basic design idea of ​​the algorithm.
  • 2) According to the design idea, use C, C++ or Java language to describe the algorithm, and give comments on the key points.
  • 3) Explain the time complexity and space complexity of the algorithm you designed.

2012 (linked list)
42.Assuming that a single linked list with a head node is used to store words, when two words have the same suffix, they can share the same suffix storage space. For example, the storage images of "loading" and "being" are shown in the figure below.
insert image description here

Let str1 and str2 respectively point to the head node of the single linked list where the two words are located. The structure of the linked list node is Please insert image description here
design an algorithm as efficient as possible in time to find the starting position of the common suffix of the two linked lists pointed to by str1 and str2 (The position p of the node where the character i is located in the figure). Require:

  • 1) Give the basic design idea of ​​the algorithm.
  • 2) According to the design idea, use C or C++ or Java language to describe the algorithm, and give notes on the key points.
  • 3) Explain the time complexity of the algorithm you designed.

There are many big problems in the combination of sequence table and sorting, and many big problems in the linked list itself, all of which are very important!

1. The concept of linear table

Linear table defines
insert image description here
the characteristics of linear table.
insert image description here
The above two diagrams describe the logical structure of the linear table, which has nothing to do with the storage structure. The specific relationship is as follows.
linear list classification

  • Sequential representation of linear tables
    • aka sequence table
    • Sequential storage in logical structure
    • Sequential storage in storage structure
  • Linked Representation of Linear List
    • aka linked list
    • Sequential storage in logical structure
    • The storage structure is random storage

2. Sequence table (order representation of linear table)

1. Basic introduction

insert image description here
Definition of sequence table:

#define MaxSize 50 //定义线性表的长度
typedef struct {
    
    
	ElemType data[Maxsize] ;	//顺序表的元素
	int len;					//顺序表的当前长度
} sqList;	//顺序表的类型定义

ElemType指任意类型

2. The principle and practice of adding, deleting, modifying and checking

Let's take a look at the effect first.
insert image description here

Add an element to the sequence table
insert image description here
insert image description here
After unremitting efforts, the following code for inserting an element is finally completed:

bool insertData(myList* mylist,int i,ElemType e){
    
    
    //三个参数分别是结构体数组、元素要插入的位置、要插入的元素

    //判断数组长度
    if(mylist==NULL||i<=0||mylist->length+1<i||i>MaxSize){
    
    
        return false;
    }
    int j;
    for(j=mylist->length;j>=i;j--){
    
    
        mylist->data[j]=mylist->data[j-1];
    }
    mylist->data[i-1]=e;
    mylist->length+=1;
    return true;
}

Delete an element in the sequence table
insert image description here
insert image description here
There are two ways to delete an element, one is to delete according to the serial number (that is, the position), and the other is to delete according to the element. I have implemented both methods here; the formula in the above loop
: The l.data[j-1]=l.data[j] method moves data forward, which can effectively prevent problems such as array crossing. If it is not this formula then the loop condition needs to be changed.
Delete by serial number

//删除元素(删除指定位置)
bool deleteDataByI(myList* mylist,int i){
    
    
    if(mylist==NULL||i<=0||mylist->length<i||i>MaxSize){
    
    
        return false;
    }
    int j=i;
    for (;j<mylist->length;j++){
    
    
        mylist->data[j-1]=mylist->data[j];
    }
    // 删除完毕之后,将多出来的一个空位赋上\0
    mylist->data[mylist->length-1]=baseElem;
    mylist->length-=1;

}

delete by element

//删除元素(遍历删除指定元素)
bool deleteDataByE(myList* mylist,ElemType e){
    
    
    // 删除指定元素(搜索,删除)
    if(mylist==NULL){
    
    
        return false;
    }
    int j=0;
    // 临时记下原来长度
    int tempIndex=mylist->length;
    // 第一遍遍历(去除相关元素)
    for(;j<tempIndex;j++){
    
    
        if(mylist->data[j]==e){
    
    
            mylist->data[j]=baseElem;
            mylist->length-=1;
        }
    }
    // 将数组内的空缺由后面元素填补掉
    int i=0;
    for(;i<mylist->length;i++){
    
    
        if(mylist->data[i]==baseElem){
    
    
            for(j=i+1;j<tempIndex;j++){
    
    
                if(mylist->data[j]!=baseElem){
    
    
                    mylist->data[i]=mylist->data[j];
                    mylist->data[j]=baseElem;
                    break;
                }
            }
        }
    }

}

Query Elements vs. Print Elements

//查询元素
int searchElement(myList* mylist,ElemType e){
    
    
    int i=0;
    for(;i<mylist->length;i++){
    
    
        if(mylist->data[i]==e){
    
    
            return true;
        }
    }
    return false;
}
//打印元素
void printData(myList* mylist){
    
    
    if(mylist==NULL){
    
    
        printf("null");
    }
    int i=0;
    for(;i<mylist->length;i++){
    
    
        printf("%3c",mylist->data[i]);
    }
    printf("\n");
}

Cleanup Sequence Table

 memset(p->data,baseElem, MaxSize);
 p->length=0;

Create an array using dynamic memory
insert image description here

Summary and source code

The sequence table is the array we often say in C language, no matter in terms of logical structure or storage structure, it is sequential storage. There are advantages and disadvantages, summarized as follows:
insert image description here

Source
code idea: Since it is written in pure C language, the bool type is customized. When inserting data, you need to pay attention to critical conditions. It cannot exceed the array or the existing length + 2, that is, it cannot be randomly inserted in the Blank area, the data should be coherent after inserting, deleting the function by index is similar to inserting, deleting by element is to delete the relevant elements first, and then use two indexes to gather the elements together. The query and print functions are relatively simple. said!

//
// Created by Zhu Shichong on 2023/1/9.
//
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 50
#define baseElem '\0'
# define bool int
# define true 1
# define false 0
typedef char ElemType;
//数组类型
typedef struct{
    
    
    ElemType data[MaxSize];
    int length;
}myList;
//插入元素
bool insertData(myList* mylist,int i,ElemType e){
    
    
    //三个参数分别是结构体数组、元素要插入的位置、要插入的元素

    //判断数组长度
    if(mylist==NULL||i<=0||mylist->length+1<i||i>MaxSize){
    
    
        return false;
    }
    int j;
    for(j=mylist->length;j>=i;j--){
    
    
        mylist->data[j]=mylist->data[j-1];
    }
    mylist->data[i-1]=e;
    mylist->length+=1;
    return true;
}
//删除元素(删除指定位置)
bool deleteDataByI(myList* mylist,int i){
    
    
    if(mylist==NULL||i<=0||mylist->length<i||i>MaxSize){
    
    
        return false;
    }
    int j=i;
    for (;j<mylist->length;j++){
    
    
        mylist->data[j-1]=mylist->data[j];
    }
    // 删除完毕之后,将多出来的一个空位赋上\0
    mylist->data[mylist->length-1]=baseElem;
    mylist->length-=1;

}
//删除元素(遍历删除指定元素)
bool deleteDataByE(myList* mylist,ElemType e){
    
    
    // 删除指定元素(搜索,删除)
    if(mylist==NULL){
    
    
        return false;
    }
    int j=0;
    // 临时记下原来长度
    int tempIndex=mylist->length;
    // 第一遍遍历(去除相关元素)
    for(;j<tempIndex;j++){
    
    
        if(mylist->data[j]==e){
    
    
            mylist->data[j]=baseElem;
            mylist->length-=1;
        }
    }
    // 将数组内的空缺由后面元素填补掉
    int i=0;
    for(;i<mylist->length;i++){
    
    
        if(mylist->data[i]==baseElem){
    
    
            for(j=i+1;j<tempIndex;j++){
    
    
                if(mylist->data[j]!=baseElem){
    
    
                    mylist->data[i]=mylist->data[j];
                    mylist->data[j]=baseElem;
                    break;
                }
            }
        }
    }

}
//查询元素
int searchElement(myList* mylist,ElemType e){
    
    
    int i=0;
    for(;i<mylist->length;i++){
    
    
        if(mylist->data[i]==e){
    
    
            return true;
        }
    }
    return false;
}
//打印元素
void printData(myList* mylist){
    
    
    if(mylist==NULL){
    
    
        printf("null");
    }
    int i=0;
    for(;i<mylist->length;i++){
    
    
        printf("%3c",mylist->data[i]);
    }
    printf("\n");
}
int main() {
    
    
    myList *p=(myList*)malloc(sizeof (myList));
    // 初始化
    memset(p->data,baseElem, MaxSize);
    p->length=6;
    p->data[0]='h';
    p->data[1]='o';
    p->data[2]='w';
    p->data[3]='a';
    p->data[4]='r';
    p->data[5]='e';
    printf("before insert : ");
    printData(p);
    insertData(p,3,'v');
    insertData(p,6,'v');
    insertData(p,8,'v');
    printf("after  insert : ");
    printData(p);
    printf("delete by index : ");
    deleteDataByI(p,4);
    printData(p);
    printf("delete by element : ");
    deleteDataByE(p,'v');
    printData(p);
    printf("search: %d\n", searchElement(p,'a'));
    printf("search: %d", searchElement(p,'c'));
    return 0;
}

In the next section, we will introduce the addition, deletion, modification, query and destruction of C language linked lists. If you are interested, please pay more attention!

Guess you like

Origin blog.csdn.net/apple_51931783/article/details/129050443