順序表操作

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
#define  INCREMENT   10   /* 線性表存儲分配量增量*/
#define OK 1
#define ERROR 0
typedef int Elemtype;
typedef bool Status;
typedef struct list
{
    Elemtype *elem;//元素存儲空間基地址
    int length;//表的當前長度
    int listsize;//表的初始分配存儲容量
} SqList;

Status InitList_Sq(SqList *L)//線性表的初始化
{
    L->elem=(Elemtype*)malloc(MAXSIZE*sizeof(Elemtype));
    //開闢空間,(Elemtype * )表示強制類型轉換,Eelemtype表示自定義的類型別名
//malloc返回的內存空間地址,返回類型爲void*
//L->elem,L必須是指針,通過指針引用結構體成員。
    //給表分配初始元素存儲容量
    if(!L->elem)//異常判斷
    {
        return ERROR;//return 0;
    }
    L->length=0;//初始化表的當前長度爲0
    L->listsize=MAXSIZE;//初始化表的分配存儲容量爲 INIT_SIZE

    return OK;
}
/*................................初始化....................................................*/
void InputList_Sq(SqList *L)
{
    int i,n;
    printf("輸入順序表的長度:");
    scanf("%d",&n);
    while(n>L->listsize)
    {
        printf("超出存儲空間!重新輸入:");
        scanf("%d",&n);
    }
    L->length=n;
    for(i=0; i<n; i++)
    {
        printf("請輸入第%d個元素的值:",i);
        scanf("%d",&L->elem[i]);
    }
}
/*.................................輸入內容...................................................*/
void Display_Sq(SqList *L)
//輸出
{
    int i;
    for(i=0; i<L->length; i++)
    {
        printf("%d   ",L->elem[i]);
    }

}
/*.............................輸出.......................................................*/
/*順序表L的插入,在第i個位置前插入新的元素e,成功返回1,否則返回0*/
Status InsertList_Sq(SqList *L,int i, Elemtype e)
{
    int j;
    Elemtype *NewSpace;
    //插入位置不合法
    if(i<1||i>L->length)
    {
        return ERROR;
    }

    //異常判斷,插入後如果發生溢出現象,增加內存分配.
    if(L->length>=L->listsize)
    {
        /* realloc擴容後,系統會自動釋放掉L->elem存儲空間*/
        NewSpace=(Elemtype*)realloc(L->elem,(L->listsize+INCREMENT)*sizeof(Elemtype));
        if(!NewSpace)                                    //分配失敗
        {
            return ERROR;
        }
        L->elem=NewSpace;                               //獲得新的內存分配
        L->listsize+=INCREMENT;                        //重分配後的存儲容量
    }


    for(j=L->length; j>=i; j--) //i位後的元素往後移動
    {
        L->elem[j]=L->elem[j-1];
    }
    L->elem[j]=e;//插入操作
    L->length++;//表長加一
    return OK;
}
/*.................................插入...................................................*/
Status  deleteList(SqList *L,int i)
{
    int j;
    printf("刪除第%d個",i);
    if(i=0||i>L->length-1)
    {

        printf("超出範圍");
        return ERROR;
    }
    else
    {
        for(j=i-1; j<=L->length; j++)
        {
            L->elem[j]=L->elem[j+1];

        }
        L->length--;
        return OK;
    }




}

/*.................................刪除..................................................*/



Status matchLocation(SqList *L,int  i)
{

    printf("將查詢第%d位",i);
    if(i=0||i>L->length-1)
    {
        printf("超過範圍");
        return ERROR;
    }
    else
    {


        printf("%d",L->elem[i]);


    }


}
/*.................................按位置查找..................................................*/

Status matchByNum(SqList *L, int s)
{
    int i;
    int flag=0;
    for(i=0; i<=L->length-1; i++)
    {

        if(s==L->elem[i])
        {
            printf("匹配成功\n");
            printf("第%d個的值爲%d",i+1,s);
            flag++;
        }

    }
    if(flag==0)
        printf("無匹配\n");
    else
    {

    }



}


int main()
{
    int n,num,e,value,num1,value1;
    SqList L;
    int choices;


    /*順序表初始化和輸入*/
    value=InitList_Sq(&L);
    if(value)
    {
        printf("線性表初始化成功!\n");
    }
    else
    {
        printf("線性表初始化失敗!\n");
        return ERROR;
    }
    InputList_Sq(&L);
    printf("順序表各元素的值爲:\n");
    Display_Sq(&L);

    printf("\n請輸入功能選擇:1.插入\t 2.刪除\t 3.按位查找4.按值查找\n");

    scanf("%d",&choices);
    if(choices==1||choices==2||choices==3||choices==4)
    {
        printf("你選擇的功能是%d",choices);

    }
    else
    {
        printf("無效操作");

    }
    if(choices==1)
    {
        /*順序表插入*/
        printf("\n請輸入插入位置:");
        scanf("%d",&n);
        if(n>L.length)
        {
            printf("超過範圍");
            return ERROR;
        }
        printf("請輸入插入元素:");
        scanf("%d",&num);
        value=InsertList_Sq(&L,n,num);
        if(value)
        {
            printf("進行插入操作後線性表依次爲:\n");
            Display_Sq(&L);
        }
        else
        {
            printf("插入失敗!\n");
            return ERROR;
        }
    }
    else if(choices==2)
    {
        printf("\n請輸入刪除位置");
        scanf("%d",&num1);
        printf("第%d個\n",num1);
        value1=deleteList(&L,num1);
        if(value1)
        {
            printf("進行插入操作後線性表依次爲:\n");
            Display_Sq(&L);
        }
        else
        {

            printf("刪除失敗!");
            return ERROR;
        }

    }
    else if(choices==3)
    {
        printf("\n請輸入查找位置");
        int n3;
        scanf("%d",&n3);
        matchLocation(&L,n3);

    }
    else if(choices==4)
    {
        printf("\n請輸入查找值");
        int m;
        scanf("%d",&m);
        matchByNum(&L,m);

    }
    else
    {

        printf("無效操作");


    }


    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章