【項目1-順序表的基本運算】

(1)目的是要測試“建立線性表”的算法CreateList,爲查看建表的結果,需要實現“輸出線性表”的算法DispList。在研習DispList中發現,要輸出線性表,還要判斷表是否爲空,這樣,實現判斷線性表是否爲空的算法ListEmpty成爲必要。這樣,再加上main函數,這個程序由4個函數構成。main函數用於寫測試相關的代碼。 

程序的結構如下所示:

頭文件:

#ifndef FUKA_H_INCLUDED
#define FUKA_H_INCLUDED
#include"fuka.h"
#include <iostream>
#define MaxSize 50
typedef int ElemType;
typedef struct
{
    ElemType date[MaxSize];
    int length;
}SqList;
void CreateList(SqList *&L,ElemType a[],int n);
void DispList(SqList*L);
bool ListEmpty(SqList *L);

#endif // FUKA_H_INCLUDED

主要函數:

#include <iostream>
#include "fuka.h"
#include <malloc.h>
using namespace std;
void CreateList(SqList *&L,ElemType a[],int n)
{
    int i;
    L=(SqList *)malloc(sizeof(SqList));
    for(i=0;i<n;i++)
    {
        L->date[i]=a[i];
    }
    L->length=n;

}
void DispList(SqList *L)
{
    int i;
    for(i=0;i<L->length;i++)
        cout<<L->date[i]<<" ";
    cout<<endl;
}
bool ListEmpty(SqList *L)
{
    return(L->length==0);
}
Main 函數:

#include <iostream>
#include "fuka.h"
using namespace std;

int main()
{
    SqList *l;
    ElemType a[6]={3,4,6,8,9,5};
    CreateList(l,a,6);
    if(ListEmpty(l))
        return 0;
    else
        DispList(l);
    return 0;
}

運行結果:


學習心得:

通過自己的編寫,加深了對順序表的印象與理解。

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