数据结构线性表

#include<stdio.h>
#include<stdlib.h>
#define Max 20
int length = 0;
typedef struct node
{
    int date[Max];
    int length;
}Seqlist,*PSeqlist;
//线性表的初始化
PSeqlist Init_Seqlist(void)
{
    PSeqlist PL;
    PL = (PSeqlist)malloc(sizeof(Seqlist));
    if (NULL==PL)
    {
        return NULL;
    }
    else
    {
        PL->length = 0;
    }
    return (PL);
}
//求线性表的长度:若找到则返回长度;没找到返回-1;
int Length_Seqlist(PSeqlist SeqlistPoint)
{
    if (SeqlistPoint)
    {
        return (SeqlistPoint->length);
    }
    return (-1);
}
//线性表的检索:-1表示表不存在,0表示查找失败
int Location_Seqlist(PSeqlist PL, int x)
{
    int i = 0;
    if (!PL)
    {
        printf("表不存在\n");
        return -1;
    }
    else
    {
        while (i < PL->length && x != PL->date[i])
        {
            i++;
        }
    }
    if (i >= PL->length)
    {
        return 0;
    }
    else
    {
        return (i + 1);
    }
}
//线性表的插入运算
int Insert_Seqlist(PSeqlist PL, int i, int num)
{
    int j;
    if (!PL)
    {
        printf("NO\n");
        return -1;
    }
    if (i > PL->length || i < 1)
    {
        printf("插入位置不合适\n");
        return 0;
    }
    for (j = PL->length - 1; j >= i-1; j--)
    {
        PL->date[j + 1] = PL->date[j];
    }
    PL->date[i-1] = num;
    PL->length++;
    return 1;
}
//线性表的删除
int Delete_Seqlist(PSeqlist PL, int i)
{
    int j;
    if (!PL)
    {
        printf("NO\n");
        return -1;
    }
    if (i > PL->length || i < 1)
    {
        printf("删除位置不合适\n");
        return 0;
    }
    for (j = i; j < PL->length; j++)
    {
        PL->date[j - 1] = PL->date[j];
    }
    PL->length--;
    return 1;
}


void PrintSeqlist(PSeqlist PL)
{
    int i;
    for (i = 0; i < PL->length ; i++)
    {
        printf("%d  ", PL->date[i]);
    }

}
int main()
{
    int i=0;
    int x;
    PSeqlist PL = Init_Seqlist();
    for ( i = 0; i < 5; i++)
    {
        scanf_s("%d", &PL->date[i]);
        PL->length++;
    }
    printf("初始化的线性表示为:\n");
    PrintSeqlist(PL);
    int length = Length_Seqlist(PL);
    printf("\n线性表长度为:%d\n", length);
    int locate = Location_Seqlist(PL, 5);
    printf("\n该元素在:%d\n", locate);
    Insert_Seqlist(PL, PL -> length, 8);
    printf("插入后:");
    PrintSeqlist(PL);
    Delete_Seqlist(PL, 3);
    printf("\n删除后:");
    PrintSeqlist(PL);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值