天天看點

第三周項目二—建設“順序表”算法庫

*Copyright(c)2017,煙台大學計算機與控制工程學院      
*All rights reservrd.         
*作者:劉文平      
*完成時間:2017年9月16日      
*版本号:v1.0      
*問題描述:請采用程式的多檔案組織形式,在項目1的基礎上,建立如上的兩個檔案,另外再建立一個源檔案,編制main函數,完成相關的測試工作。      
*輸入描述:無      
*程式輸出:依據各個函數而定     

list.h
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

#define MaxSize 50
typedef int ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int length;
} SqList;
void CreateList(SqList *&L, ElemType a[], int n);//用數組建立線性表
void InitList(SqList *&L);//初始化線性表InitList(L)
void DestroyList(SqList *&L);//銷毀線性表DestroyList(L)
bool ListEmpty(SqList *L);//判定是否為空表ListEmpty(L)
int ListLength(SqList *L);//求線性表的長度ListLength(L)
void DispList(SqList *L);//輸出線性表DispList(L)
bool GetElem(SqList *L,int i,ElemType &e);//求某個資料元素值GetElem(L,i,e)
int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)
bool ListInsert(SqList *&L,int i,ElemType e);//插入資料元素ListInsert(L,i,e)
bool ListDelete(SqList *&L,int i,ElemType &e);//删除資料元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED
#endif
list.cpp
#include <stdio.h>
#include <malloc.h>
#include "list.h"

//用數組建立線性表
void CreateList(SqList *&L, ElemType a[], int n)
{
    int i;
    L=(SqList *)malloc(sizeof(SqList));
    for (i=0; i<n; i++)
        L->data[i]=a[i];
    L->length=n;
}

//初始化線性表InitList(L)
void InitList(SqList *&L)   //引用型指針
{
    L=(SqList *)malloc(sizeof(SqList));
    //配置設定存放線性表的空間
    L->length=0;
}

//銷毀線性表DestroyList(L)
void DestroyList(SqList *&L)
{
    free(L);
}

//判定是否為空表ListEmpty(L)
bool ListEmpty(SqList *L)
{
    return(L->length==0);
}

//求線性表的長度ListLength(L)
int ListLength(SqList *L)
{
    return(L->length);
}

//輸出線性表DispList(L)
void DispList(SqList *L)
{
    int i;
    if (ListEmpty(L)) return;
    for (i=0; i<L->length; i++)
        printf("%d ",L->data[i]);
    printf("\n");
}

//求某個資料元素值GetElem(L,i,e)
bool GetElem(SqList *L,int i,ElemType &e)
{
    if (i<1 || i>L->length)  return false;
    e=L->data[i-1];
    return true;
}

//按元素值查找LocateElem(L,e)
int LocateElem(SqList *L, ElemType e)
{
    int i=0;
    while (i<L->length && L->data[i]!=e) i++;
    if (i>=L->length)  return 0;
    else  return i+1;
}

//插入資料元素ListInsert(L,i,e)
bool ListInsert(SqList *&L,int i,ElemType e)
{
    int j;
    if (i<1 || i>L->length+1)
        return false;   //參數錯誤時傳回false
    i--;            //将順序表邏輯序号轉化為實體序号
    for (j=L->length; j>i; j--) //将data[i..n]元素後移一個位置
        L->data[j]=L->data[j-1];
    L->data[i]=e;           //插入元素e
    L->length++;            //順序表長度增1
    return true;            //成功插入傳回true
}

//删除資料元素ListDelete(L,i,e)
bool ListDelete(SqList *&L,int i,ElemType &e)
{
    int j;
    if (i<1 || i>L->length)  //參數錯誤時傳回false
        return false;
    i--;        //将順序表邏輯序号轉化為實體序号
    e=L->data[i];
    for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移
        L->data[j]=L->data[j+1];
    L->length--;              //順序表長度減1
    return true;              //成功删除傳回true
}
main.cpp
#include "list.h"
#include<stdio.h>
int main()
{
    SqList *sq;
    ElemType E;

    printf("初始化線性表\n");
    InitList(sq);

    printf("在第1個位置插入元素1\n");
    ListInsert(sq, 1, 1);
    DispList(sq);

    printf("在第2個位置插入元素6\n");
    ListInsert(sq, 2, 6);
    DispList(sq);


    printf("在第1個位置插入元素9\n");
    ListInsert(sq, 1, 9);
    DispList(sq);

    printf("删除第2個位置的元素\n");
    ListDelete(sq,2,E);
    DispList(sq);


    printf("銷毀線性表\n");
    DestroyList(sq);
    DispList(sq);

}
知識點總結:
算法庫,建立一個項目。
學習心得:
學會從不同的角度去思考問題,進而找到更好的方法。
           
第三周項目二—建設“順序表”算法庫

繼續閱讀