数据结构-顺序表建立及运算

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
/*
* Project: 顺序表及基本运算
* Author: Jachin
* Date: 2016.11.9
*/
typedef struct 
{
    int data[MAXSIZE]; //存放顺序表中的元素
    int len;           //顺序表的表长
}SeqList;              //顺序表类型

SeqList *Init_SeqList()
{                      //顺序表初始化
    SeqList *L;
    L=(SeqList*)malloc(sizeof(SeqList));
    L->len=0;
    return L;
}

void CreatList(SeqList *L)
{                     //建立顺序表
    int i;
    printf("Input length of List:\n");
    scanf("%d",&L->len);
    printf("Input elements of List:\n");
    for (i=L->len;i>=1; i--)    //为什么是1?
    {
        scanf("%d",&L->data[i]);
    }
}

void InverseList(SeqList *L)
{                    //实现顺序表的逆置
    int tmp;
    int i;
    for(i=1;i<=L->len/2;i++)
    {
        tmp=L->data[i];
        L->data[i]=L->data[L->len-i+1];  //首尾对应关系
        L->data[L->len-i+1]=tmp;
    }
}

void Insert_SeqList(SeqList *L, int i, int x)
{                      //在顺序表中插入元素
    int j;
    if(L->len==MAXSIZE-1)   //表满
        printf("The List is full!\n");
    else
        if(i<1||i>L->len+1)  //插入位置非法
            printf("The position is invalid!\n");
        else
        {
            for (j=L->len;j>=i;j--) //将顺序后移一个元素位置
            {
                L->data[j+1]=L->data[j];
            }
            L->data[i]=x;  //插入x到第i个位置
            L->len++;     //表长增1
        }
}

void Delete_SeqList(SeqList *L ,int i)
{                                 //在顺序表中删除元素
    int j;
    if(L->len==0)   //表为空
        printf("The List is empty!\n");
    else
        if(i<1||i>L->len)  //删除位置非法
            printf("The position is invalid!\n");
        else
        {
            for(j=i+1;j<=L->len;j++)         //将顺序前移动一个位置
            {
                L->data[j-1]=L->data[j];
            }
            L->len--;   //表长减1
        }
}

int Location_SeqList(SeqList *L,int x)
{                            //在顺序表中查找元素
    int i=1;
    while(i<L->len&&L->data[i]!=x)
    {
        i++;
    }
    if(L->data[i]==x)
        return i;          //找到返回其位置值
    else 
        return 0;          //未找到返回0
}

void print(SeqList *L)
{                           //打印顺序表的数据
    int i;
    for(i=1;i<=L->len;i++)
    {
        printf("%4d ",L->data[i]);
    }
    printf("\n");
}

int main()
{
    SeqList *s; 
    int i,x;
    s=Init_SeqList();   //顺序表初始化
    printf("Creat List: \n");
    CreatList(s);       //建立顺序表
    printf("Output the List:\n");
    print(s);           //输出所建立的顺序表
    printf("Insert element and site of insert: \n");
    scanf("%d %d",&x,&i);  //输入要出入的元素值和位置
    Insert_SeqList(s,i,x);  //将x值插入到顺序表中
    printf("Output the list:\n");
    print(s);
    printf("Input element site of delete:\n");
    scanf("%d",&i);         //输入要删除的 元素位置
    Delete_SeqList(s,i);    //删除位置i的元素
    printf("Output the list:\n");
    print(s);
    printf("Input element value of location:\n");
    scanf("%d",&x);        //输入要查找的元素x;
    i=Location_SeqList(s,x); //在顺序表中查找元素x
    printf("Output element %d site is %d:\n",x,i); //输出该位置的元素值
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值