顺序表的基本操作代码

seqlist.h:

#pragma once
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
typedef int type;
typedef struct Seqlist {
    type* data;
    int size;
    int capacity;
}sl;
//初始化顺序表
void initialize(sl* ps);
//销毁线性表
void destory(sl* ps);
//判断是否满员;
void isfull(sl* ps);
//头插数据
void headinsert(sl* ps);
//尾插数据
void tailinsert(sl* ps);
//头删数据
void headdelete(sl* ps);
//尾删数据
void taildelete(sl* ps);
//打印结果
void print(sl ps);

seqlist.c:

#include"seqlist.h"
void initialize(sl* ps){//初始化顺序表
    ps->data = NULL;
    ps->size = ps->capacity = 0;
}
void destory(sl* ps) {销毁顺序表
    if (ps->data) {
        free(ps->data);
        ps->data = NULL;
    }
    ps->size = ps->capacity = 0;
}
void isfull(sl* ps) {
    if (ps->size == ps->capacity) {//如果当前数量达到容量
        int newcapacity =ps->capacity== 0 ? 4 : 2 * ps->capacity;//未创建初始容量则是4,创建了则是扩容到当前容量的两倍
        type* p = (type*)realloc(ps->data, newcapacity * sizeof(type));//开辟空间
        if (p == NULL) {
            perror("申请失败");
                return;
        }
        ps->data = p;//指针重新赋值保证有效;
        ps->capacity = newcapacity;
    }
}
void headinsert(sl* ps,type c) {//头插法
    assert(ps);//避免指针为空
    isfull(ps);//判断是否容量已满,满了则扩容
    for (int i = ps->size; i>=1; i--) {
        ps->data[i] = ps->data[i-1];
    }
    ps->data[0] =  c;
    ps->size++;

}
void tailinsert(sl* ps,type c) {//尾插法
    assert(ps);
    
    ps->data[ps->size++] = c;
}
void headdelete(sl* ps){//头删法
    if (ps->data) {
        for (int i = 0; i<=ps->size-2; i++) {
            ps->data[i] = ps->data[i + 1];
        }
        ps->size--;
    }
}
void taildelete(sl* ps){//尾删法
    assert(ps);
    ps->size--;

}
void print(sl ps) {//打印
    for (int i = 0; i < ps.size; i++) {
        printf("%d", ps.data[i]);
    }
    printf("\n");
}

 测试样例

顺序表是一种线性表的存储结构,可以数组来实现。以下是顺序表基本操作代码示例(使用C语言): 1. 初始化顺序表: ```c #define MAX_SIZE 100 // 定义顺序表的最大长度 typedef struct { int data[MAX_SIZE]; // 用数组存储数据元素 int length; // 当前顺序表的长度 } SeqList; void InitList(SeqList *L) { L->length = 0; // 初始化长度为0 } ``` 2. 插入元素到顺序表中: ```c int Insert(SeqList *L, int pos, int elem) { if (pos < 1 || pos > L->length + 1) { return 0; // 插入位置不合法 } if (L->length >= MAX_SIZE) { return 0; // 顺序表已满,无法插入 } for (int i = L->length; i >= pos; i--) { L->data[i] = L->data[i - 1]; // 将插入位置及之后的元素后移一位 } L->data[pos - 1] = elem; // 在插入位置处放入新元素 L->length++; // 长度加1 return 1; // 插入成功 } ``` 3. 删除顺序表中指定位置的元素: ```c int Delete(SeqList *L, int pos) { if (pos < 1 || pos > L->length) { return 0; // 删除位置不合法 } for (int i = pos; i < L->length; i++) { L->data[i - 1] = L->data[i]; // 将删除位置之后的元素前移一位 } L->length--; // 长度减1 return 1; // 删除成功 } ``` 4. 查找顺序表中指定元素的位置: ```c int Find(SeqList L, int elem) { for (int i = 0; i < L.length; i++) { if (L.data[i] == elem) { return i + 1; // 返回元素在顺序表中的位置 } } return -1; // 未找到元素 } ``` 5. 获取顺序表中指定位置的元素: ```c int GetElem(SeqList L, int pos) { if (pos < 1 || pos > L.length) { return -1; // 获取位置不合法 } return L.data[pos - 1]; // 返回指定位置的元素值 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值