数据结构(Data Structure)(第二集)(顺序列表(Sequential List))(C语言)

在后面四集所讨论的是关于data structure中的几种基础而且常听到的结构体,列表(Lists),链表(Link List),堆栈(stack),列队(queue), 今天先从顺序列表(Sequential List)开始。

先说说编程中经常运用的数据类型吧!

抽象数据类型是什么?

一般来说,我们大多数运用的数据类型包括:
1. int
2. double/float
3. bool
4. char
以上其实都是由编程语言中的内置类型(build-in data types)。

数据类型的准确定义: 用来形容一系列值的集合(sets of value),以及定义值的一系列操作(sets of operation)
值(value)以变量的形式储存。

今天的重点——抽象数据类型 (Abstract Data Type)
可以简称为 (ADT)

ADT其实是一个数据对象(data object)的模型, 由一个结构体产生。

1. 列表(List)

什么是列表?

顾名思义,就是一系列以A1, A2, A3, … , An的形式存放的列表。

那要怎么定义一个列表的结构体?

我们直接来看一个以数组(array)形式而写成的一个顺序列表(Sequential List)的代码

a. 定义一个顺序列表的数据类型

/*
Description: Define a data type of sequential list
*/
#define MAXLEN 100

typedef struct SeqList{
    int data[MAXLEN];  //a array that stores some numbers
    int length;  //size of list
}

b. 建立一个空顺序表(Create an empty sequential list)

//Create an empty sequential list
SeqList *Create_ll(){
    SeqList *L;
    L = (SeqList *) malloc (sizeof(SeqList));
    L->length = 0;
    return L;
}

c. 对新的顺序列表进行初始化 Time of Complexity: O(n)

//Initialize a sequential list
void initialize(SeqList *L){
    int i;
    printf("Please input the length of the list:\n");
    scanf("%d", &L->Length);
    printf("Please input the values of the elements:\n");
    for(i=0;i<L->Length;i++){
       scanf("%d",&L->data[i]);
    }       
}

d. 在列表中进行插入操作 Time of Complexity: O(n)

/*
a: 要插入的数字(也可以是char,取决于列表要插入的数据)  
i: 要插入的位置
*/ 
int Insert(SeqList *L, int a, int i){
    int j;
    //check if list is full(列表是否满) 
    if(isFull(L)){    
        printf("Sorry,the list is full.");
    }

    //check if index is correct(输入的位置是否属于列表)
    if(i <= 0 || i > L->length){  
        printf("Sorry, your index has mistakes");
    }

   //Check if index is the last one of list(是否最后一位置)
    if(i == L->length){
         L->data[i] = a;
         L->length++;   
    }

   /*
   the index is in the middle of list, so go through the list from bigger length to smaller length
   */
    for(j = L->length-1; j >= i; j--){
        L->data[j+1] = L->data[j];
        L->data[i] = a;
        L->length++;    
    }
}

e. 列表中进行删除操作 Time of Complexity: O(n)

/*
i: 想删除的位置
Description: 这函数可以把某个位置的数字用后面位置的数字代替
*/

//delete a character from array
void Delete_sl(SeqList *L, int i) {
    int j;
    for (j = i; j < L->length; j++) {  //将后面的数字往前移
        L->data[j] = L->data[j+1];
    }
    L->length--;
}

f. 查找操作 Time of Complexity: O(n)

//to find the location of character
int Find_sl(SeqList *L, int a){
    int i;
    for(i = 0; i < L->length; i++){
        if(L->data[i] == a){
                return i;   
        }    
    }
}

g. 检查列表是否为空 Time of Complexity: O(1)

//Check if the list is empty
int isEmpty(SeqList *L){
    if(L->length == 0){
        return TRUE;    
    }else{
        return FALSE;    
    }

}

h. 检查列表是否已满 Time of Complexity: O(1)

//check if the list is full
int isFull(SeqList *L){
    if(L->length == MAXLEN){
        return TRUE;    
    }else{
        return FALSE;    
    }

}

以上就是关于顺序列表结构体的定义, 以下附上如何删除字符串中重复字符的代码

Time of Complexity: O(n^3)

 //check if the array has repeated characters
    for (i = 0; i < L->length; i++) {
        for (j = i+1; j < L->length; j++) {
                if (L->data[j] == L->data[i]) {
                    Delete_sl(L, i); break;
                }
        }
    }

Hints:
这最后一小段代码是根据字符串数组来写的,上面structure是根据integer整型来写的,所以大家如果在试代码的时候记得需要修改structure里面的数据类型。
思路: 是用循环的形式将每个字符与后面的字符逐一比较,如果相等,就将其删除(删除方法如上面的删除操作相同)

这一集是我对Sequential List的一些了解,如大家有什么异议,欢迎一起讨论!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值