码迷,mamicode.com
首页 > 其他好文 > 详细

线性表的顺序实现

时间:2015-03-04 12:42:50      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

 

头文件

/*
*线性表的顺序表示与实现
*/

#ifndef SQLIST_H
#define SQLIST_H
#include "Sqlist.h"


#define TRUE 1
#define FALSE 0
#define ERROR -1

#define LIST_SIZE 10
#define LIST_INCREASE 2

typedef int Status;
typedef int ElemType;

typedef struct
{
    ElemType *elem;
    int length;
    int size;
}SqList;

Status IsEqual(ElemType lhs, ElemType rhs);

Status InitList(SqList &L);

Status InsertElem(SqList &L, int pos, ElemType elem);

Status DeleteElem(SqList &L, int pos, ElemType *elem);

Status GetElem(SqList &L, int pos, ElemType *elem);

Status UnionList(SqList srcL1, SqList srcL2, SqList &desL);

Status DestroyList(SqList &L);

Status ClearList(SqList &L);

Status ListEmpty(SqList L);

int GetListLength(SqList L);

void PrintList(SqList L);

int LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType));

#endif

源文件

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

Status IsEqual(ElemType lhs, ElemType rhs)
{
    if (lhs == rhs)
    {
        return TRUE;
    }
    return FALSE;
}

Status InitList(SqList &L)
{
    L.elem = (ElemType *)malloc(sizeof(ElemType)*LIST_SIZE);
    if (!L.elem)
    {
        return FALSE;
    }
    L.length = 0;
    L.size = LIST_SIZE;
    return TRUE;
}

Status InsertElem(SqList &L, int pos, ElemType elem)
{
    if (pos < 1 || pos > L.length+1)
    {
        return FALSE;
    }
    //判断容量
    if (L.length >= L.size)
    {
        ElemType *pElemNew = (ElemType *)realloc(L.elem, sizeof(ElemType)*(L.size + LIST_INCREASE));    
        if (!pElemNew)
        {
            return ERROR;
        }
        L.elem = pElemNew;
        L.size = L.size + LIST_INCREASE;
    }
    
    ElemType *pElem = L.elem + pos - 1;
    *pElem = elem;
    for (ElemType *p = L.elem + L.length - 1; p >= L.elem + pos; --p)
    {
        *p++ = *p;
    }
    L.length++;
    return TRUE;
}

Status DeleteElem(SqList &L, int pos, ElemType *elem)
{
    if (pos < 1 || pos > L.length)
    {
        return FALSE;
    }
    ElemType *pElem = L.elem + pos - 1;
    elem = pElem;
    for (ElemType *p =L.elem+ pos; p< L.elem+L.length; p++)
    {
        *(p-1)= *p;
    }
    L.length--;
    return TRUE;
}

Status GetElem(SqList &L, int pos, ElemType *elem)
{
    if (pos < 1 || pos > L.length)
    {
        return FALSE;
    }
    *elem = *(L.elem + pos - 1);
    return TRUE;
}

Status UnionList(SqList srcL1, SqList srcL2, SqList &desL)
{
    for (int i = 0; i < srcL1.length; i++)
    {
        InsertElem(desL, desL.length +1,*(srcL1.elem+i));
    }
    for (int i = 0; i < srcL2.length; i++)
    {
        InsertElem(desL, desL.length +1, *(srcL2.elem + i));
    }
    return TRUE;
}

Status DestroyList(SqList &L)
{
    
    free(L.elem);
    L.elem = NULL;
    L.length = L.size = 0;
    return TRUE;
}

Status ClearList(SqList &L)
{
    L.elem = NULL;
    L.length = 0;
    L.size = LIST_SIZE;
    return TRUE;
}

Status ListEmpty(SqList L)
{
    if (L.length > 0)
    {
        return FALSE;
    }
    return TRUE;
}

int GetListLength(SqList L)
{
    return L.length;
}

void PrintList(SqList L)
{
    for (int  i = 0; i < L.length; i++)
    {
        printf("%d  ",*(L.elem+i));
    }
}

int LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType))
{
    ElemType *pElem = L.elem;
    int i = 1;
    while (i <= L.length && !compare(*pElem++,e))
    {
        ++i;
    }
    if (i < L.length)
        return i;
    else
        return 0;
}

 

线性表的顺序实现

标签:

原文地址:http://www.cnblogs.com/wangliu/p/4312928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!