Data structure (C language) - basic operation and implementation of sequential table in linear table

1. What is a sequence table, and what is the sequence table reflected in?

 

        A sequence table is two logically adjacent elements, and their positions in the sequence table are also the same. The letters AZ are a sequence table. The sequence table is composed of elements one by one, and these elements are of the same type.

        We can represent this sequence table with an array, that is, before creating the sequence table, we can give it a fixed size space.

Second, we can first think about what operations related to the sequence table can be, or what operations can we implement?

        1. Initialization sequence table

        2. Print out the sequence table

        3. Insert elements into the sequence table

        4. Delete elements in the sequence table

        5. Find the subscript whose value is X in the sequence table

        6. Find the value of the i-th element in the sequence table

        7. Obtain the length of the current sequence table, that is, the length of the array storing the elements.

Note: Don't memorize these things by rote, just understand how to implement these operations.

3. According to the operations listed above, we define a function for each of these operations to implement.

(1) Define a structure:

       In my opinion, data structure means that when we get a problem, we can define a structure about this problem. For example, a sequence table, then we need an array to store the elements, and we also need to record how many elements are currently, that is, the length of the array. So we can define the following structure:

#define MaxSize 10  //
typedef int ElemType//将int类型重新命名为ElemType,即ElemType可以代替int
typedef struct {
	ElemType data[MaxSize];//定义一个ElemType类型的数组,数组名为data,数组大小为MaxSzie
	int length;//顺序表的当前长度
}SqList;

 (2) Initialize the sequence table . To initialize the sequence table, we only need to set the length of the current sequence table equal to 0, which means that there is nothing in the current sequence table, instead of marking the pointer as empty as in the linked list. code show as below:

//初始化顺序表
void InitSq(SqList *L)
{
    L->length=0;
    printf("初始化之后,数组的长度为:%d\n",L->length);//输出顺序表的初始化后的长度。
    printf("-------------------------------------------------\n");
}

(3) Print the current sequence table, that is, loop through the array data[], you can output all the elements of the current array, the code is as follows, since the L we declared is a structure type variable, you can pass L.data[] And L.length to get the current array and length, if the declaration is a pointer of the structure SqList type, then get the current array and length through L->data[] and L->length:

//打印输出当前顺序表中的元素
void Print(SqList L)
{
    printf("当前表中的元素为:");
    for(int i=0;i<L.length;i++)
    {
        printf("%d ",L.data[i]);
    }
    printf("\n");
    printf("-------------------------------------------------\n");
}

(4) Insert into the current sequence table, and insert the value e into the i-th position of the sequence table, where the i-th position refers to starting from 1. We first need to judge whether i is within the valid insertion range. It can be inserted at the first position, or at the L->length position after the L->length position. Anything beyond this range is invalid. If the insertion is successful, move all the elements in the i-th position and after it by one bit. We know that the subscript of the i-th position in the array is i-1, so from i-1 to L->length- 1 These elements must be moved one bit backward;

//向元素中插入元素
void InsertSq(SqList *L,int i,ElemType e)
{
    //首先要判断这个顺序表是否满了
    if(i>L->length+1||i<=0)
    {
        printf("i=%d插入位置不存在\n当前表的长度为:%d\n",i,L->length);
        printf("-------------------------------------------------\n");
    }
    else
    {
        for(int k=L->length-1;k>=i-1;k--)
        {
            L->data[k+1]=L->data[k];        //把它后面的都往后移动一位
        }
        L->data[i-1]=e;
        L->length++;//因为此时我们插入了一个元素,数组的长度要加1.
    }
}

(5) Delete the element whose value is e in the current sequence table. We must also loop through this array, and start from the starting address of the array to find the element whose value is e. At this time, there are two cases, either found, put All elements after it are moved forward by one, and the length of the array is reduced by 1. If it is not found, print out that the value we want to delete is not present in our current array. After successful deletion, we need to move the element. At this time, we need to set the current position of the deleted element as i. At this time, the position of i is vacant, and its subscript in the array is i-1. The elements of , all move forward one place, we only need to loop to L->length-2, look at the code:

//在当前列表中删除值为x的元素
void DeleteSq(SqList *L,ElemType x)
{
    int i=0;
    while(i<L->length&&L->data[i]!=x){  //从头开始遍历,如果没找到,在符合条件下一直循环
        i++;
    }
    if(i>=L->length){//遍历完数组没有我们要删除的那个值
        printf("数组中没有你要删除的值为:%d的元素\n",x);
        printf("-------------------------------------------------\n");
    }
    else{
        printf("删除成功");
        for(int j=i;j<L->length-1;j++){    //把后一个值赋值到它的前一个元素所在的位置,所以到L->length-2即可。
            L->data[j]=L->data[j+1];
        }
        L->length--;
    }
}

(6) Get the subscript of the element whose value is X in the array , loop through the array, and return the subscript value of the array if it is found, or -1 if it is not found.

//获取值为X的元素在数组中的下标
int FindValue(SqList L,ElemType x)
{
    int i=0;
    while(i<L.length&&L.data[i]!=x)//循环遍历数组,如果不相等就循环找下一个
    {
        i++;
    }
    if(i>=L.length)      //遍历完数组还是没有找到
    {
        return -1;
    }
    return i;           //返回元素X所在的数组下标值
}

(7) To obtain the value of the i-th element, first determine whether the i is within the valid range. If it is, use *e to store the value. If we are different from *e, this value can only be valid in this function. , which cannot be returned to the main function. The specific code is as follows:

//获取第i个元素的值
void GetValue(SqList L,int i,ElemType *e)
{

    if(i<=0||i>L.length)
    {
        printf("您要查找的位置的有效范围为:1——%d\n",L.length);
        printf("您的输入为:%d,它不在数组的有效数字内\n",i);
    }
    else
    {
        (*e)=L.data[i-1];
        printf("您要查找的元素在第%d个位置,值为:e=%d\n",i,*e);
    }
    printf("-------------------------------------------------\n");
}

(8) To obtain the length of the sequence table, the code is as follows:

//获取顺序表当前的长度
int GetLength(SqList L)
{
    return L.length;
}

(9) Main function code:

int main()
{
    SqList s;//声明一个SqList类型的变量s,来标记这个顺序表
    ElemType e=0;
    InitSq(&s);            //初始化这个顺序表
    InsertSq(&s,1,23);     //插入五个元素
    InsertSq(&s,1,12);
    InsertSq(&s,1,5);
    InsertSq(&s,1,46);
    InsertSq(&s,3,89);
    Print(s);              //打印输出这五个元素
    int n,m;                 //定义变量,用来获取用户输入的要查找的元素的位置n,和值m
    printf("请输入你要查找的元素的位置:");
    scanf("%d",&n);
    GetValue(s,n,&e);     //获取第n个位置元素值
    printf("请输入你要查找的元素的值:");
    scanf("%d",&m);
    e=FindValue(s,m);     //获取值m在数组下标中的位置
    if(e>=0&&e<GetLength(s)){   //判断是否找到了那个元素。
        printf("您查找的元素%d,在数组中的下标为:%d\n",m,e);
        printf("-------------------------------------------------\n");
    }
    else{
        printf("您查找的元素%d不在数组中\n",m);
        printf("-------------------------------------------------\n");
    }
    int f;//f用于存储当前我们要删除的那个值
    printf("请输入你要删除的那个值:");
    scanf("%d",&f);
    DeleteSq(&s,f);
    Print(s);
    printf("当前数组的长度为:%d\n",s.length);
    printf("-------------------------------------------------\n");
    int r=0;
    printf("请输入你要插入的元素的值:");
    scanf("%d",&r);
    printf("向表中插入元素%d,插入之前顺序表的长度为:%d\n",r,s.length);
    InsertSq(&s,3,r);//在元素的第三个位置上插入888;
    Print(s);
    printf("插入之后顺序表的长度为:%d\n",s.length);
    return 0;
}

(10) The test results are as follows:

Summary: There are many ways to implement the sequence table. This is just for your reference. You can use it according to your own habits.

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/121123351