zoukankan      html  css  js  c++  java
  • 1.线性表-Array

    线性表-Array

    fatal.h

    #include <stdio.h>
    #include <stdlib.h>
    
    #define Error(Str)        FatalError(Str)
    #define FatalError(Str)   fprintf(stderr, "%s
    ", 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
    ");
        else
        {
            do
            {
                P = Advance(P);
                printf("%d ", Retrieve(P, L));
            } while (!IsLast(P, L));
            printf("
    ");
        }
    }
    
    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
    ", i);
    
        printf("Finished deletions
    ");
    
        PrintList(L);
    
        DeleteList(L);
    
        return 0;
    }
    
  • 相关阅读:
    面试试题
    使用NSURLSessionDataTask请求数据(get post方式)
    使用SSZipArchive第三方库解压zip包
    实现图文混编界面
    使用多线程创建单例对象
    SQL语句的种类_外键_表连接(内连接和左外连接)
    使用第三方库(FMDB) 本地数据库存储数据 --使用为了保证线程安全做法
    利用第三方库XML解析 (TBXML)转化成模型数据
    蓝牙传送_多点连接 (适用于>iOS7)
    Unity 产生各不相同的随机数
  • 原文地址:https://www.cnblogs.com/typewriter/p/6045520.html
Copyright © 2011-2022 走看看