天天看点

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

#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=;
    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>=; i--)    //为什么是1?
    {
        scanf("%d",&L->data[i]);
    }
}

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

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

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

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

void print(SeqList *L)
{                           //打印顺序表的数据
    int i;
    for(i=;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); //输出该位置的元素值
}
           

继续阅读