1.线性表-Array

线性表-Array

fatal.h

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

#define Error(Str)        FatalError(Str)
#define FatalError(Str)   fprintf(stderr, "%s\n", Str), exit(1)

array.h

typedef int ElementType;

#ifndef _List_H
#define _List_H

struct ListRecord;
typedef struct ListRecord *List;
typedef int Position;

List MakeEmpty(List L);
int IsEmpty(List L);
int IsFull(List L);
int IsLast(Position P, List L);
Position Find(ElementType X, List L);
void Delete(ElementType X, List L);
void Insert(ElementType X, List L, Position P);
void DeleteList(List L);
Position Header(List L);
Position First(List L);
Position Advance(Position P);
ElementType Retrieve(Position P, List L);

#endif

array.c

#include "array.h"
#include <stdlib.h>
#include "fatal.h"

#define CAPACITY 100

struct ListRecord
{
    ElementType *Array;
    int Last;
};

List MakeEmpty(List L)
{
    if (L != NULL)
        DeleteList(L);
    L = malloc(sizeof(struct ListRecord));
    if (L == NULL)
        FatalError("Out of memory!");
    L->Array = malloc(sizeof(ElementType) * CAPACITY);
    if (L->Array == NULL)
        FatalError("Out of space!!!");
    L->Last = -1;
    return L;
}

int IsEmpty(List L)
{
    return L->Last == -1;
}

int IsFull(List L)
{
    return L->Last == CAPACITY-1;
}

int IsLast(Position P, List L)
{
    return P == L->Last;
}

Position Find(ElementType X, List L)
{
    Position P;

    P = 0;
    while (P <= L->Last && L->Array[P] != X)
        P++;
    if (P > L->Last)
        return -1;
    else
        return P;
}

void Delete(ElementType X, List L)
{
    Position P;

    P = Find(X, L);

    Position i;
    if (P < 0 || P > L->Last)
        Error("Illegal position");
    for (i = P; i < L->Last; i++)
        L->Array[i] = L->Array[i+1];
    L->Last--;
}

/* Insert (after legal position P) */
void Insert(ElementType X, List L, Position P)
{
    Position i, Q;

    if (IsFull(L))
        Error("Full list");
    Q = P+1;
    if (Q < 0 || Q > L->Last+1)
        Error("Illegal position");
    for (i = L->Last; i >= Q; i--)
        L->Array[i+1] = L->Array[i];
    L->Array[Q] = X;
    L->Last++;
}

void DeleteList(List L)
{
    if (L != NULL)
    {
        free(L->Array);
        free(L);
    }
}

Position Header(List L)
{
    return -1;
}

Position First(List L)
{
    return 0;
}

Position Advance(Position P)
{
    return ++P;
}

ElementType Retrieve(Position P, List L)
{
    return L->Array[P];
}

testarray.c

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

void PrintList(const List L)
{
    Position P = Header(L);

    if (IsEmpty(L))
        printf("Empty list\n");
    else
    {
        do
        {
            P = Advance(P);
            printf("%d ", Retrieve(P, L));
        } while (!IsLast(P, L));
        printf("\n");
    }
}

int main()
{
    List L;
    Position P;
    int i;

    L = MakeEmpty(NULL);
    P = Header(L);
    PrintList(L);

    for (i = 0; i < 10; i++)
    {
        Insert(i, L, P);
        PrintList(L);
        P = Advance(P);
    }
    for (i = 0; i < 10; i += 2)
        Delete(i, L);

    for (i = 0; i < 10; i++)
        if (Find(i, L) == -1)
            printf("Element %d Find fails\n", i);

    printf("Finished deletions\n");

    PrintList(L);

    DeleteList(L);

    return 0;
}
posted @ 2016-11-09 03:25  typewriter  阅读(265)  评论(0编辑  收藏  举报