天天看點

資料結構-順序表建立及運算

#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); //輸出該位置的元素值
}
           

繼續閱讀