C语言-数据结构之线性表:顺序表和单链表

目录


 不废话,直接开始 !

一、顺序表(seqList)

顺序表使用数组实现,它具有以下特点:

  • 内存连续:顺序表中的元素在内存中是连续存储的,这使得顺序表的访问操作非常高效。
  • 随机访问:由于元素在内存中的位置与其在逻辑上的顺序相对应,可以通过下标快速访问任意位置的元素。
  • 大小可变:顺序表的大小可以根据需求进行动态调整,可以方便地插入或删除元素。
  • 遍历方便:由于内存连续,遍历顺序表的元素只需要按顺序依次访问即可。

首先定义一个结构体类型SeqList,并给其指针起一个别名为List:

typedef int E;   // 元素类型

struct SeqList{
    E * array;      //底层数组
    int capacity;   // 容量
    int size;       //表长
};

typedef struct SeqList* List;  // 顺序表指针别名

顺序表初始化:


bool initList(List list){
    //动态分配一个包含 10 个元素的数组空间
    list->array= malloc(sizeof(E)*10); 
    //动态分配内存失败,退出程序
    if(list==NULL) return 0;
    list->capacity=10;
    list->size=0;
    return 1;
}

顺序表插入元素:

/*
 * 索引index合法范围 [1,size]
 * 元素在数组插入下标 index-1
 */
bool insertList(List list,E element ,int index){
    //检查索引范围
    if(index<1 || index>list->size+1) return 0;
    //若数组空间已满,则将容量变为原来的1.5倍
    if(list->size==list->capacity){
        int newCapacity=list->capacity+(list->capacity>>1);
        //使用realloc函数为array重新分配内存
        E * newArray= realloc(list->array,sizeof(E) * newCapacity);
        //分配内存失败,退出程序
        if(newArray==NULL) return 0;
        list->array=newArray;
        list->capacity=newCapacity;
    }
    //插入位置后面的元素依次向右移动
    for (int i = list->size; i >=index ; --i)
        list->array[i]=list->array[i-1];
    //将元素放到指定索引处
    list->array[index-1]=element;
    list->size++;
    return 1;
}

顺序表删除元素:

/*
 * 索引index合法范围 [1,size]
 * 元素在数组中删除的下标 index-1
 */
bool deleteList(List list, int index){
    //检查索引范围
    if(index<1 || index>list->size) return 0;
    //插入位置后面的元素依次向前移动,覆盖下标为index-1处元素
    for (int i = index; i < list->size ; ++i) {
        list->array[i-1]=list->array[i];
    }
    list->size--;
    return 1;
}
顺序表查找元素:
/*
 * element为需要查找的元素
 * 返回值为元素对应的索引
 */
int findList(List list ,E element){
    for (int i = 0; i < list->size; ++i)
        //返回的是对应的索引位置,而不是对应的数组下标
        if(list->array[i]==element) return i+1;
    return -1;  //未找到元素,返回-1
}

 顺序表修改元素


bool setList(List list,int index,E element){
    //检查索引范围
    if(index<1 || index>list->size) return false;
    list->array[index-1]=element;
    return true;
}

主程序main:

int main(){
    struct SeqList list;
    if(initList(&list)){
        for (int i = 1; i < 10; ++i)
            insertList(&list, i, i);
        printArrayList(&list);
        printf("%d\n",*getList(&list,2));  //返回第二个元素
        printf("%d\n",findList(&list,5));  //返回元素 5 对应的索引
        printf("%d",sizeList(&list));      //返回表长
        clearList(&list);
    }
}

seqList实现代码:


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int E;   // 元素类型

struct SeqList{
    E * array;      //底层数组
    int capacity;   // 容量
    int size;       //表长
};

typedef struct SeqList* List;  // 指针别名

bool initList(List list){
    list->array= malloc(sizeof(E)*10);
    if(list==NULL) return 0;
    list->capacity=10;
    list->size=0;
    return 1;
}

void printArrayList(List list){
    for (int i = 0; i < list->size; ++i)
        printf("%d ",list->array[i]);
    printf("\n");
}

/*
 * 索引合法范围 [1,size]
 */
bool insertList(List list,E element ,int index){
    if(index<1 || index>list->size+1) return 0;
    if(list->size==list->capacity){
        int newCapacity=list->capacity+(list->capacity>>1);
        E * newArray= realloc(list->array,sizeof(E) * newCapacity);
        if(newArray==NULL) return 0;
        list->array=newArray;
        list->capacity=newCapacity;
    }
    for (int i = list->size; i >=index ; --i)
        list->array[i]=list->array[i-1];
    list->array[index-1]=element;
    list->size++;
    return 1;
}

/*
 * 索引合法范围 [1,size]
 */
bool deleteList(List list, int index){
    if(index<1 || index>list->size) return 0;
    for (int i = index; i < list->size ; ++i) {
        list->array[i-1]=list->array[i];
    }
    list->size--;
    return 1;
}

void destroyList(List list){
    if(list==NULL) return;
    free(list->array);
    free(list);
}

E * getList(List list,int index){
    return &list->array[index-1];
}

bool setList(List list,int index,E element){
    if(index<1 || index>list->size) return false;
    list->array[index-1]=element;
    return true;
}

int findList(List list ,E element){
    for (int i = 0; i < list->size; ++i)
        if(list->array[i]==element) return i+1;
    return -1;
}

bool isEmptyList(List list){
    return list->size == 0;
}

bool isFullList(List list){
    return list->size == list->capacity;
}

int getCapacityList(List list){
    return list->capacity;
}

int sizeList(List list){
    return list->size;
}

bool setCapacityList(List list,int newCapacity){
    if(list->size > newCapacity && newCapacity > list->capacity)
        return false;
    int* newArray= realloc(list->array,sizeof(E)*newCapacity);
    if(newArray == NULL) return false;
    list->array=newArray;
    list->capacity=newCapacity;
    return true;
}

void clearList(List list){
    list->size=0;
}

int main(){
    struct SeqList list;
    if(initList(&list)){
        for (int i = 1; i < 10; ++i)
            insertList(&list, i, i);
        printArrayList(&list);
        printf("%d\n",*getList(&list,2));  //返回第二个元素
        printf("%d\n",findList(&list,5)); //返回元素 5 对应的索引;
        printf("%d",sizeList(&list)); //返回表长
        clearList(&list);
    }
}

二、单链表(singlyList)

单链表使用指针实现,它具有以下特点:

  • 链式结构:单链表由一系列节点组成,每个节点包含数据域和指针域。通过指针将节点连接起来形成链式结构。

  • 动态分配内存:单链表的节点是通过动态分配内存来创建的,可以根据需要动态增加或删除节点,不需要预先分配固定大小的内存空间。

  • 插入和删除高效:相比于顺序表(数组)实现,单链表在插入和删除操作上更高效。插入或删除一个节点只需要修改相邻节点的指针,时间复杂度为 O(1)。

  • 无需移动元素:在单链表中插入和删除节点时,不需要移动其他节点,只需要修改对应节点的指针即可。

  • 随机访问较慢:由于单链表无法直接访问任意位置的节点,需要从头节点开始依次遍历到目标位置,因此索引定位节点的时间复杂度为 O(n),效率较低。


首先定义一个结构体类型SinglyList,并给其指针起一个别名为List
              一个结构体类型ListNode, 并给其指针起一个别名为Node

typedef int E;

//带头结点的单链表,即头结点不存储数据

struct ListNode{
    E element;               //数据域
    struct ListNode * next;  //指针域
};

struct SinglyList{
    struct ListNode* head;  //头指针
    struct ListNode* tail;  //尾指针
    int size;
};

typedef struct SinglyList * List;  //别名

typedef struct ListNode * Node;    //别名

单链表初始化:

/*
* malloc函数为头、尾指针分配内存空间,他们的大小为结构体
* struct ListNode的字节大小
*/
void initList(List list){
    list->head= malloc(sizeof (struct ListNode));
    list->tail= malloc(sizeof (struct ListNode));
    list->head->next=list->tail->next=NULL;
    list->size=0;
}

单链表插入元素:


/*
 * 索引的合法插入范围 [1,size+1],即包含了首插入、尾插入
 */
bool insertList(List list, E element, int index){
    Node head=list->head;
    //检查索引范围
    if(list==NULL || index<1 || index>list->size+1) return 0;
    //为插入节点动态分配内存
    Node newNode= malloc(sizeof (struct ListNode));
    newNode->element=element;
    //首先空表时插入,表长为0
    if(list->head->next==NULL){
        list->tail->element=element;
        head->next=list->tail;
        list->tail->next=NULL;
        list->size++;
        return 1;
    }
    //其次是首插入,但表长不为0
    if(index==1){
        newNode->next=head->next;
        head->next=newNode;
        list->size++;
        return 1;
    }
    //再其次是尾插入,但表长不为0
    if(index==list->size+1){
        list->tail->next=newNode;
        newNode->next=NULL;
        list->tail=newNode;
        list->size++;
        return 1;
    }
   //再再其次是中间插入 索引范围:[2,size]
    Node front=head;
    while (index>1){
        front=front->next;
        index--;
    }
    head=front->next;
    front->next=newNode;
    newNode->next=head;
    list->size++;
    return 1;
}

单链表删除元素:

/*
 * 索引的合法删除范围 [1,size]
 */
bool deleteList(List list, int index){
    Node head=list->head;
    //索引合法检查,表为空检查
    if(head==NULL || index<1 || index>list->size) return 0;
    Node front=head;
    while (index>1){
        front=front->next;
        index--;
    }
    head=front->next;
    if(head==NULL){
        front->next=NULL;
        return 1;
    }
    Node temp=head;
    front->next=front->next->next;
    free(temp);//释放被删除节点的内存
    list->size--;
    return 1;
}

单链表查找元素:


/*
 * 返回元素的索引 索引合法范围 [1,size]
 */
int findList(List list, E element){
    Node head=list->head;
    if(head==NULL) return 0;
    Node node=head->next;
    int i=1;
    while(node!=NULL){
        if(node->element==element)
            return i;
        node=node->next;
        i++;
    }
    return -1;
}

单链表修改元素:


/*
 *  索引合法范围 [1,size]
 */
void setList(List list,E element, int index) {
    Node head=list->head;
    if (head == NULL || index < 1)
        return;
    Node front = head;
    while (index > 1) {
        front = front->next;
        index--;
    }
    head = front->next;
    if (head != NULL)
        head->element=element;
}

主程序main:

int main() {
    struct SinglyList singlyList;
    initList(&singlyList);
    for (int i = 1; i <= 10; ++i) {
        insertList(&singlyList, i * 100, i);//尾插入
    }
    setList(&singlyList,0,1); //修改第一个元素
    insertList(&singlyList,0,5); //中间插入
    deleteList(&singlyList, 3);   //删除第三个元素
    printList(&singlyList);
    printf("\n");
    printf("%d\n", sizeList(&singlyList));  //表长
    printf("%d\n ", getList(&singlyList,2));  //返回第二个元素
    printf("%d ", findList(&singlyList,500));  // 返回500对应的索引
}

singlyList实现代码:


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int E;

//带头结点的单链表,头结点不存储数据

struct ListNode{
    E element;
    struct ListNode * next;
};

struct SinglyList{
    struct ListNode* head;
    struct ListNode* tail;
    int size;
};

typedef struct SinglyList * List;

typedef struct ListNode * Node;

void initList(List list){
    list->head= malloc(sizeof (struct ListNode));
    list->tail= malloc(sizeof (struct ListNode));
    list->head->next=list->tail->next=NULL;
    list->size=0;
}

/*
 * 索引的合法插入范围 [1,size+1],即包含了首插入、尾插入
 */
bool insertList(List list, E element, int index){
    Node head=list->head;
    if(list==NULL || index<1 || index>list->size+1) return 0;
    Node newNode= malloc(sizeof (struct ListNode));
    newNode->element=element;
    //首先空表时插入,表长为0
    if(list->head->next==NULL){
        list->tail->element=element;
        head->next=list->tail;
        list->tail->next=NULL;
        list->size++;
        return 1;
    }
    //其次是首插入,但表长不为0
    if(index==1){
        newNode->next=head->next;
        head->next=newNode;
        list->size++;
        return 1;
    }
    //再其次是尾插入,但表长不为0
    if(index==list->size+1){
        list->tail->next=newNode;
        newNode->next=NULL;
        list->tail=newNode;
        list->size++;
        return 1;
    }
   //再再其次是中间插入 索引范围:[2,size]
    Node front=head;
    while (index>1){
        front=front->next;
        index--;
    }
    head=front->next;
    front->next=newNode;
    newNode->next=head;
    list->size++;
    return 1;
}

/*
 * 索引的合法删除范围 [1,size]
 */
bool deleteList(List list, int index){
    Node head=list->head;
    if(head==NULL || index<1 || index>list->size) return 0;
    Node front=head;
    while (index>1){
        front=front->next;
        index--;
    }
    head=front->next;
    if(head==NULL){
        front->next=NULL;
        return 1;
    }
    Node temp=head;
    front->next=front->next->next;
    free(temp);//释放被删除节点的内存
    list->size--;
    return 1;
}

/*
 * 返回元素 索引的合法范围 [1,size]
 */
E getList(List list, int index) {
    Node head=list->head;
    if (head == NULL || index < 1)
        return 0;
    Node front = head;
    while (index > 1) {
        front = front->next;
        index--;
    }
    head = front->next;
    if (head != NULL) {
        int element = head->element;
        return element;
    }
    return 0;
}

/*
 *  索引合法范围 [1,size]
 *
 */
void setList(List list,E element, int index) {
    Node head=list->head;
    if (head == NULL || index < 1)
        return;
    Node front = head;
    while (index > 1) {
        front = front->next;
        index--;
    }
    head = front->next;
    if (head != NULL)
        head->element=element;
}
/*
 * 返回元素的索引 索引合法范围 [1,size]
 */
int findList(List list, E element){
    Node head=list->head;
    if(head==NULL) return 0;
    Node node=head->next;
    int i=1;
    while(node!=NULL){
        if(node->element==element)
            return i;
        node=node->next;
        i++;
    }
    return -1;
}

int sizeList(List list){
    Node head=list->head;
    //因为头结点没有存储数据
    int resSize=-1;
    Node node = head;
    while(node!=NULL){
        node=node->next;
        resSize++;
    }
    return resSize;
//    return list->size; 注意,此处也可直接返回维护的表长,无需遍历节点
}

void printList(List list){
    Node head=list->head;
    Node node = head->next;
    while(node!=NULL){
        //while循环条件中的node!=NULL等价于node
        printf("%d ",node->element);
        node=node->next;
    }
}

int main() {
    struct SinglyList singlyList;
    initList(&singlyList);
    for (int i = 1; i <= 10; ++i) {
        insertList(&singlyList, i * 100, i);//尾插入
    }
    setList(&singlyList,0,1); //修改第一个元素
    insertList(&singlyList,0,5); //中间插入
    deleteList(&singlyList, 3);   //删除一下第三个元素
    printList(&singlyList);
    printf("\n");
    printf("%d\n", sizeList(&singlyList));
    printf("%d\n ", getList(&singlyList,2));
    printf("%d ", findList(&singlyList,500));
}


总结

  顺序表适用于元素频繁访问和随机访问的场景,但当需要频繁插入或删除元素时,顺序表的效率较低,因为在插入或删除操作后需要移动其他元素。在这种情况下,可以考虑使用链表等其他数据结构来代替顺序表。

单链表通过使用指针实现了灵活的链式结构,允许动态添加和删除节点,但随机访问效率较低。它在插入和删除操作频繁的情况下表现出较高的效率。

以上就是今天要讲的内容,感谢各位看官老爷的捧场。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shenjian~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值