Data structure C language implements the initialization of sequence tables, adding elements, finding and deleting elements, and printing elements

Purpose

Be familiar with and master the sequential storage structure of linear tables, master and apply basic operation algorithms such as search, insertion, and deletion of sequential tables, and train and improve structured programming capabilities and program debugging capabilities.

There are the following definitions and declarations of sequence tables:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define	MAXSIZE  100  //最大长度 
typedef  int datatype;
typedef  struct
{
    
    
	 datatype  data[MAXSIZE]; 
	 int last;    
}SeqList;

Use the sequence table as the storage structure to implement the following basic operations:

1. Initialization of the sequence table
2. Insertion operation
3. Delete operation
4. Search by value
5. Output the contents of the linear table
6. Define the main function to verify the above operations.

The modules are divided as follows:

1. Initialization of sequence table: SeqList * init_SeqList()
2. Insertion operation: int Insert_SeqList(SeqList *L,int i,datatype x)
3. Delete operation: int Delete_SeqList(SeqList *L,int i)
4. Search by value :int Location_SeqList(SeqList *L, datatype x)
5. Output the contents of the linear table: void Output_SeqList(SeqList *L)
6. Main function: void main()

run demonstration

Insert image description here
Insert image description here

Code implementation part

There are clear comments


```c
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define MAXSIZE 100

typedef int datatype;

typedef struct {
    
    
    datatype data[MAXSIZE];
    int last;
} SeqList;

// 顺序表的初始化
SeqList* init_SeqList() {
    
    
    SeqList* L = (SeqList*)malloc(sizeof(SeqList));
    L->last = -1;
    return L;
}

// 插入操作
int Insert_SeqList(SeqList* L, int i, datatype x) {
    
    
    if (L->last == MAXSIZE - 1) {
    
    
        printf("顺序表已满,插入失败。\n");
        return 0;
    }
    if (i < 1 || i > L->last + 2) {
    
    
        printf("插入位置非法,插入失败。\n");
        return 0;
    }
    int j;
    for (j = L->last; j >= i - 1; j--) {
    
    
        L->data[j + 1] = L->data[j];
    }
    L->data[i - 1] = x;
    L->last++;
    return 1;
}

// 删除操作
int Delete_SeqList(SeqList* L, int i) {
    
    
    if (i < 1 || i > L->last + 1) {
    
    
        printf("删除位置非法,删除失败。\n");
        return 0;
    }
    int j;
    for (j = i; j <= L->last; j++) {
    
    
        L->data[j - 1] = L->data[j];
    }
    L->last--;
    return 1;
}

// 按值查找
int Location_SeqList(SeqList* L, datatype x) {
    
    
	int i;
    for (i = 0; i <= L->last; i++) {
    
    
        if (L->data[i] == x) {
    
    
            return i + 1;
        }
    }
    return -1;  // 未找到返回-1
}

// 输出线性表的内容
void Output_SeqList(SeqList* L) {
    
    
    if (L->last == -1) {
    
    
        printf("顺序表为空。\n");
        return;
    }
    printf("顺序表的内容为:");
    int i;
    for (i = 0; i <= L->last; i++) {
    
    
        printf("%d ", L->data[i]);
    }
    printf("\n");
}

// 主函数进行验证
int main() {
    
    
	//定义要输入的元素个数
	int N; 
	int valueTemp;
	// 设置计数器 
	int count=0;
	// 表初始化 
    SeqList* L = init_SeqList();
    //定义长度
	printf("请输入N的长度:");
	scanf("%d",&N) ;
	// 表插入调用函数 
	while(count<N){
    
    
		printf("请输入第%d个元素:",count+1);
		scanf("%d",&valueTemp);
		Insert_SeqList(L, count+1, valueTemp);
		count ++;
	}
    printf("元素插入完毕\n");
    Output_SeqList(L);
    
	printf("请你输入要查按照的值:\n");
	scanf("%d",&valueTemp);
    int index = Location_SeqList(L, valueTemp);
    if (index != -1) {
    
    
        printf("元素%d的位置为:%d\n", valueTemp,index);
    }
    else {
    
    
        printf("元素%d未找到。\n",valueTemp);
    }

    Delete_SeqList(L, 2);
    Output_SeqList(L);

    free(L);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_52312427/article/details/132957432