线性表——顺序存储

        今天学习了线性表,更新一下,有兴趣的自行学习,可以结合下面的代码哦。

构建主体:

#define MAXSIZE 100

int data[MAXSIZE];
int length;

typedef struct node
{
    int data[MAXSIZE];
    int length;
}SeqList, *PSeqList;

PSeqList PL;

初始化:

PSeqList Init_SeqList(void)    ///返回的类型就是指针。
{

    PL = (PSeqList)malloc(sizeof(SeqList));
    if(PL)
        PL->length = 0;
    return (PL);
}

主函数验证部分(同时定义了一个之后一直用的指针):

int main()
{
    int t;
    PSeqList L;
    L = Init_SeqList();  ///把返回值拉出来,给一个名字。
    
    t=Length_SeqList(L);
    printf("%d\n",t);     
    
    Insert_SeqList(L,1,1);
    t=Length_SeqList(L);
    printf("%d\n",t);
    
    t=Location_Seqlist(L, 1);
    printf("%d\n",t);
    
    Delete_SeqList(L,1);
    t=Length_SeqList(L);
    printf("%d\n",t);
}

显示长度:

int Length_SeqList(PSeqList L)  ///注意这里的PSeqList L,是一个形参,L只是名字,重要的是类型。
{
    return (L->length);
}

检索:

//检索
int Location_Seqlist(PSeqList L, int x)
{
    int i = 0;
    while(i<L->length && L->data[i]!=x)
        i++;
    if(i>= L->length) return 0;
    else return (i+1);
}

插入:

//插入
int Insert_SeqList(PSeqList L,int i,int x)
{
    int j;
    if(!L)
    {
        printf("list is not exist");
        return (-2);
    }
    if(L->length>=MAXSIZE)
    {
        printf("FULL");
        return (-1);
    }
    if(i<1||i>L->length+1)
    {
        printf("it is illegal");
        return 0 ;
    }

    for(j=L->length-1;j>=i-1;j--)  ///i-1是插入的位置
    {
        L->data[j+1]=PL->data[j];
    }
    L->data[i-1]=x;
    L->length++;
    return 1;
}

删除:

//删除
int Delete_SeqList(PSeqList L,int i)
{
    int j;
    if(!L)
    {
        printf("list is not exist");
        return (-1);
    }
    if(i<1||i<L->length)
    {
        printf("it is illegal");
        return (0);
    }

    for(j=i;j<L->length;j++)
        L->data[j-1] = L->data[j];
    L->length--;
    return 1;
}
///在写删除与添加程序的时候由于有输入的参数,要考虑到函数对各种各样的参数的反应

完整的程序如下,有需求的自取:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAXSIZE 100

int data[MAXSIZE];
int length;

typedef struct node
{
    int data[MAXSIZE];
    int length;
}SeqList, *PSeqList;


PSeqList PL;



PSeqList Init_SeqList(void)    ///返回的类型就是指针。
{

    PL = (PSeqList)malloc(sizeof(SeqList));
    if(PL)
        PL->length = 0;
    return (PL);
}

int Length_SeqList(PSeqList L)  ///注意这里的PSeqList L,是一个形参,L只是名字,重要的是类型。
{
    return (L->length);
}

//检索
int Location_Seqlist(PSeqList L, int x)
{
    int i = 0;
    while(i<L->length && L->data[i]!=x)
        i++;
    if(i>= L->length) return 0;
    else return (i+1);
}

//插入
int Insert_SeqList(PSeqList L,int i,int x)
{
    int j;
    if(!L)
    {
        printf("list is not exist");
        return (-2);
    }
    if(L->length>=MAXSIZE)
    {
        printf("FULL");
        return (-1);
    }
    if(i<1||i>L->length+1)
    {
        printf("it is illegal");
        return 0 ;
    }

    for(j=L->length-1;j>=i-1;j--)  ///i-1是插入的位置
    {
        L->data[j+1]=PL->data[j];
    }
    L->data[i-1]=x;
    L->length++;
    return 1;
}

//删除
int Delete_SeqList(PSeqList L,int i)
{
    int j;
    if(!L)
    {
        printf("list is not exist");
        return (-1);
    }
    if(i<1||i<L->length)
    {
        printf("it is illegal");
        return (0);
    }

    for(j=i;j<L->length;j++)
        L->data[j-1] = L->data[j];
    L->length--;
    return 1;
}
///再写删除与添加程序的时候由于有输入的参数,要考虑到函数对各种各样的参数的反应

int main()
{
    int t;
    PSeqList L;
    L = Init_SeqList();  ///把返回值拉出来,给一个名字。
    
    t=Length_SeqList(L);
    printf("%d\n",t);     
    
    Insert_SeqList(L,1,1);
    t=Length_SeqList(L);
    printf("%d\n",t);
    
    t=Location_Seqlist(L, 1);
    printf("%d\n",t);
    
    Delete_SeqList(L,1);
    t=Length_SeqList(L);
    printf("%d\n",t);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JOJO桑

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

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

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

打赏作者

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

抵扣说明:

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

余额充值