首页 > 其他分享 >C语言 顺序表

C语言 顺序表

时间:2024-06-01 18:02:23浏览次数:15  
标签:ps arr 顺序 void C语言 SL SLDataType size

顺序表底层逻辑是数组,是用一段物理地址连续的存储单位依次存储数据元素的,

首先先创建一个结构体类型

typedef int SLDataType;//方便以后存储别的类型的数据

typedef struct SeqList
{
    SLDataType* arr;
    int size;//有效数据的个数
    int capacity;//空间大小
}SL;

1初始化顺序表

void SLInit(SL*ps)
{
    ps->arr = NULL;
    ps->capacity = ps->size = 0;
}

2

//在插入时,需要先检查空间是否充足(头插,尾插,指定位置插入)

void SLCheckCapacity(SL* ps)//检查空间是否充足
{
    assert(ps);
    if (ps->capacity == ps->size)
    {
        SLDataType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        SLDataType* tem = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
        if (tem == NULL)//扩容是否成功
        {
            perror("realloc fail!");
            exit(1);
        }
        ps->arr = tem;
        ps->capacity = newcapacity;
    }

3 尾插

void SLPushBack(SL* ps, SLDataType x)
{
    SLCheckCapacity(ps);
    assert(ps);
        ps->arr[ps->size++] = x;
}

4 尾删

void SLPopBack(SL* ps)//尾删
{
    assert(ps&&ps->size);
    --ps->size;
}

5 头插 

void SLPushFront(SL* ps, SLDataType x)//头插
{
    assert(ps);
    SLCheckCapacity(ps);
    for (int i = ps->size; i>0; i--)//整体往后面退一位
    {
        ps->arr[i] = ps->arr[i - 1];
    }
    ps->arr[0] = x;
    ps->size++;
}

 6 头删

void SLPopFront(SL* ps)//头删
{
    assert(ps && ps->size);
    for (int i =0; i<ps->size-1; i++)//整体往前移动一位
    {
        ps->arr[i] = ps->arr[i+1];
    }
    --ps->size;
}

7 顺序表的销毁

void SLDestroy(SL* ps)
{
    if (ps->arr)
    {
        free(ps->arr);
        ps->arr = NULL;
    }
    ps->capacity = ps->size = 0;

打印顺序表的数据 

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

全部代码

#include"SeqList.h"
void SLInit(SL*ps)
{
    ps->arr = NULL;
    ps->capacity = ps->size = 0;
}
void SLDestroy(SL* ps)
{
    if (ps->arr)
    {
        free(ps->arr);
        ps->arr = NULL;
    }
    ps->capacity = ps->size = 0;
}
void SLCheckCapacity(SL* ps)//检查空间是否充足
{
    assert(ps);
    if (ps->capacity == ps->size)
    {
        SLDataType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        SLDataType* tem = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
        if (tem == NULL)//扩容是否成功
        {
            perror("realloc fail!");
            exit(1);
        }
        ps->arr = tem;
        ps->capacity = newcapacity;
    }

}
void SLPushBack(SL* ps, SLDataType x)//尾插 先检查空间是否充足
{
    SLCheckCapacity(ps);
    assert(ps);
        ps->arr[ps->size++] = x;
}
void SLPushFront(SL* ps, SLDataType x)//头插
{
    assert(ps);
    SLCheckCapacity(ps);
    for (int i = ps->size; i>0; i--)//整体往后面退一位
    {
        ps->arr[i] = ps->arr[i - 1];
    }
    ps->arr[0] = x;
    ps->size++;
}
void SLPrint(SL ps)//打印
{
    for (int i = 0; i < ps.size; i++)
    {
        printf("%d", ps.arr[i]);
    }
    printf("\n");
}
void SLPopBack(SL* ps)//尾删
{
    assert(ps&&ps->size);
    --ps->size;
}
void SLPopFront(SL* ps)//头删
{
    assert(ps && ps->size);
    for (int i =0; i<ps->size-1; i++)//整体往前移动一位
    {
        ps->arr[i] = ps->arr[i+1];
    }
    --ps->size;
}

#define  _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
    SLDataType* arr;
    int size;//有效数据的个数
    int capacity;//空间大小
}SL;
void SLInit(SL* ps);//初始化顺序表
void SLDestroy(SL* ps);//顺序表的销毁
void SLPrint(SL ps);//打印
//尾插 头插
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);
//尾删 头删
void SLPopBack(SL* ps);
void SLPopFront(SL*ps);
 

#include"SeqList.h"
void SLTest01()
{
    SL s1;
    SLInit(&s1);
    SLPushBack(&s1, 1);
    SLPushBack(NULL,2);
    SLPopBack(&s1);
    SLPopBack(&s1);
    SLPopBack(&s1);
    SLPrint(s1);
    SLDestroy(&s1);
};
int main()
{
    SLTest01();
    return 0;

 补充三个

一 在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)//注意pos是下标
{
    assert(ps);
    assert(pos >= 0 && pos < ps->size);
    SLCheckCapacity(ps);
    for (int i = ps->size; i > pos; i--)//从pos开始到size-1的数据 都往后移动一位
    {
        ps->arr[i] = ps->arr[i - 1];
    }
    ps->arr[pos] = x;

ps->size++;
}

二 删除指定位置的数据
void SLErase(SL* ps, int pos)
{
    assert(ps);
    assert(ps->size);
    for (int i = pos; i < ps->size-1; i++)
    {
        ps->arr[i] = ps->arr[i+1];
    }

ps->size--;
}

三 查找数据

int SLFind(SL* ps, SLDataType x)
{
    assert(ps);
    for (int i = 0; i < ps->size; i++)
    {
        if (ps->arr[i] == x)
            return i;
    }
    return -1;
}

    int find = SLFind(&s1, 1);
    if (find < 0)
    {
        printf("没有此数据");
    }
    else
    {
        printf("找到了对应数据下标的数:%d ", find);

    }

标签:ps,arr,顺序,void,C语言,SL,SLDataType,size
From: https://blog.csdn.net/2301_81618689/article/details/139376273

相关文章

  • C语言--CH06--操作符(下)
    C语言–CH06–操作符(下)四、赋值操作符1、赋值和初始化的区别赋值和初始化有显著的区别inta=10;//这是初始化a=20;//这是赋值2、连续赋值赋值是一种从左往右的运算,并且可以连续赋值:inta=0;intb=10;intc=20;a=b=c+1;printf("%d\n",a);猜......
  • C语言--CH05--操作符(上)
    C语言–CH05–操作符(上)一、算术操作符+-*/%操作规则:1、%的操作数只能为整数。2、/的操作数中只要有一个浮点数,运算结果就为浮点数。3、除了%以外的操作符的操作数都可以不是整数。4、%10即取个位数,%100即取个位十位数,以此类推。5、整数/整数,结果向下取整......
  • C语言学生成绩管理系统
    部分源码//Anhighlightedblock#include<stdio.h>#include<stdlib.h>#include<string.h>#defineMAX_STUDENTS1000typedefstruct{charid[......
  • C语言文件操作指南:读写与管理
    在C语言编程中,文件操作是一个重要的主题。无论是读取配置文件、记录日志,还是处理大量数据,文件操作都是必不可少的。本文将介绍C语言中文件操作的基本概念和常用方法,帮助你轻松掌握这项技能。一、文件操作的基本概念在C语言中,文件操作主要包括以下几步:打开文件:创建一个文件指......
  • 下面提供一些C语言的入门示例代码
    下面提供一些C语言的入门示例代码,并附有注释,以帮助理解每个部分的功能。1.HelloWorld程序#include<stdio.h> //引入标准输入输出库intmain(){ //主函数的开始   printf("Hello,World!\n"); //打印"Hello,World!"到控制台   return0; //返回......
  • C语言练习题之——从简单到烧脑(12)(每日两道)
    题目1:找出1到99之间的全部同构数, 同构数:它出现在平方数的右边,例如:5是25右边的数,25是625右边的数,5和25都是同构数。#include<stdio.h>intmain(){ intsum=0,left=0,right=0; printf("1-99之间的同构数:\n"); for(inti=1;i<100;i++) { inta=i*i;......
  • C语言练习题之——从简单到烧脑(10)(每日两道)
    题目1:二位数组的应用:输入一个3X5的整数矩阵,输出其中的最大值,最小值,和他们的下标#include<stdio.h>intmain(){ inta[3][5],max,min,maxi,maxj,mini,minj; //最大值最小值,和下标分别定义变量存储 inti,j; for(i=0;i<3;i++) for(j=0;j<5;j++)......
  • Scanner的进阶使用、顺序结构和选择结构
    Java流程控制02:Scanner的进阶使用一道练习题packagecom.xiwen.scanner;importjavafx.beans.binding.DoubleExpression;importjava.util.Scanner;publicclassDemo05{publicstaticvoidmain(String[]args){//我们可以输入多个数字,并且要求其......
  • 按时间顺序将表 A 中的每条信息与表 B 中的最早回复列出
    我有三个表:conversations,user_messages和system_messages.它们的基本结构(为简洁起见删除了无关列)是:创建表conversations(默认生成idint作为身份主键);创建表user_messages(创建表user_messages(,conversation_idint引用conversation......
  • 【C语言】基于C语言实现的贪吃蛇游戏
    【C语言】基于C语言实现的贪吃蛇游戏......