`
silentpassing
  • 浏览: 6557 次
  • 性别: Icon_minigender_1
  • 来自: 冰岛
最近访客 更多访客>>
社区版块
存档分类
最新评论

顺序表的实现

阅读更多

/******************************
  Author: Fox
  Last Date: 2011-03-09 20:33:34
  Description: 顺序表
******************************/
/*sqlist.h*/

#define MAXSIZE 50    /*线性表的最大长度*/

typedef char ElemType;    /*根据实际修改*/

typedef struct
{
    ElemType data[MAXSIZE];
    int length;    /*线性表的长度*/
}SqList;

typedef SqList * List;    /*List是指向SqList结构的指针*/

void InitList(List L);    /*初始化线性表L*/
void DestroyList(List L);    /*销毁线性表L*/
int ListEmpty(List L);   /*判断线性表L是否为空*/
int ListLength(List L);   /*求线性表L长度*/
void DispList(List L);    /*输出线性表L中所有元素*/
void GetElem(List L, int i, ElemType *e);    /*用*e返回线性表L中第i个元素的值*/
int LocateElem(List L, const ElemType e);    /*返回线性表L中第一个与e相同的值的位序,返回0表示不存在*/
void ListInsert(List L, int i, const ElemType e);    /*将元素e插入线性表L中第i位*/
void ListDelete(List L, int i, ElemType *e);    /*删除线性表L中第i个元素,并用*e返回其值*/
/*以上i的取值范围:1<= i <= length*/


****************************************
****************************************
/*sqlist.c*/

#include <stdio.h>
#include "sqlist.h"

void InitList(List L)    /*初始化线性表L*/
{
    L->length = 0;
}

void DestroyList(List L);    /*销毁线性表L*/

int ListEmpty(List L)   /*判断线性表L是否为空*/
{
    return L->length == 0;    /*线性表L空返回1,非空返回0*/
}

int ListLength(List L)   /*求线性表L长度*/
{
    return L->length;
}

void DispList(List L)    /*输出线性表L中所有元素*/
{
    int i = 0;
    while(L->length > i)
        printf("%c ", L->data[i++]);    /*针对不同数据类型改变格式控制符*/
    printf("\n");
}

void GetElem(List L, int i, ElemType *e)    /*用*e返回线性表L中第i个元素的值*/
{
    if(i <= L->length)
        *e = L->data[i - 1];
    else
        printf("GetElem failed!\n");
}

int LocateElem(List L, const ElemType e)    /*返回线性表L中第一个与e相同的值的位序,返回0表示不存在*/
{
    int i = 0;
    while(i < L->length)
    {
        if(e == L->data[i])
            return i + 1;
        else
            ++i;
    }
    return 0;
}

void ListInsert(List L, int i, const ElemType e)    /*将元素e插入线性表L中第i位*/
{
    int n = L->length;
   
    if(i > n + 1)    /*可以插在线性表L尾部,但不能超出尾部后一位*/  
    {
        printf("Insert failed!\n");
        return ;    /*插入失败直接返回*/
    }
   
    /*将第n至i位依次向后移动一位*/
    while(n >= i)
    {
        L->data[n] = L->data[n - 1];
        --n;
    }
    L->data[i - 1] = e;
    ++L->length;    /*插入元素e后线性表L长度增1*/
}

void ListDelete(List L, int i, ElemType *e)    /*删除线性表L中第i个元素,并用*e返回其值*/
{
    int n = L->length;
   
    if(i > n)
    {
        printf("Delete failed!\n");
        return ;
    }      
   
    *e = L->data[i - 1];
   
    /*将第i+1至n位依次向前移动一位*/
    while(i < n)
    {
        L->data[i - 1] = L->data[i];
        ++i;
    }
    --L->length;    /*删除指定元素后线性表L长度减1*/
}


****************************************
****************************************
/*简单测试代码test.c*/
#include <stdio.h>
#include "sqlist.h"

int main(void)
{
    int i = 0;
    SqList L;    /*L是SqList类型变量*/
    InitList(&L);    /*用线性表L的地址做参数,初始化线性表*/
    printf("%d\n", ListLength(&L));
   
    while(i++ < 10)
    {
        ListInsert(&L, 1, 'A');
        if(!ListEmpty(&L))
            printf("Not Empty!\n");
        printf("%d\n", ListLength(&L));
        DispList(&L);
        printf("\n\n");
    }
   
    return 0;
}


****************************************
****************************************
/*makefile*/
test : test.o sqlist.o
[TAB]gcc -o test test.o sqlist.o

test.o : test.c sqlist.h
[TAB]gcc -c test.c

sqlist.o : sqlist.h sqlist.c
[TAB]gcc -c sqlist.c

clean :
[TAB]rm *.o

 

说明:由于水平制表符没法在文中显示,故以[TAB]表示一个制表符。

 

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics