zoukankan      html  css  js  c++  java
  • 2.线性表-Linked list

    线性表-Linked list

    fatal.h

    #include <stdio.h>
    #include <stdlib.h>
    
    #define Error(Str)        FatalError(Str)
    #define FatalError(Str)   fprintf(stderr, "%s
    ", Str), exit(1)
    

    linkedlist.h

    typedef int ElementType;
    
    #ifndef _List_H
    #define _List_H
    
    struct Node;
    typedef struct Node *PtrToNode;
    typedef PtrToNode List;
    typedef PtrToNode Position;
    
    List MakeEmpty(List L);
    int IsEmpty(List L);
    int IsLast(Position P, List L);
    Position Find(ElementType X, List L);
    void Delete(ElementType X, List L);
    Position FindPrevious(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);
    
    #endif
    

    linkedlist.c

    #include "linkedlist.h"
    #include <stdlib.h>
    #include "fatal.h"
    
    /* Place in the interface file */
    struct Node
    {
        ElementType Element;
        Position    Next;
    };
    
    List MakeEmpty(List L)
    {
        if (L != NULL)
            DeleteList(L);
        L = malloc(sizeof(struct Node));
        if (L == NULL)
            FatalError("Out of memory!");
        L->Next = NULL;
        return L;
    }
    
    /* Return true if L is empty */
    int IsEmpty(List L)
    {
        return L->Next == NULL;
    }
    
    /* Return true if P is the last position in list L */
    /* Parameter L is unused in this implementation */
    int IsLast(Position P, List L)
    {
        return P->Next == NULL;
    }
    
    /* Return Position of X in L; NULL if not found */
    Position Find(ElementType X, List L)
    {
        Position P;
    
        P = L->Next;
        while (P != NULL && P->Element != X)
            P = P->Next;
    
        return P;
    }
    
    /* Delete from a list */
    /* Cell pointed to by P->Next is wiped out */
    /* Assume that the position is legal */
    /* Assume use of a header node */
    void Delete(ElementType X, List L)
    {
        Position P, TmpCell;
    
        P = FindPrevious(X, L);
    
        if (!IsLast(P, L))  /* Assumption of header use */
        {                      /* X is found; delete it */
            TmpCell = P->Next;
            P->Next = TmpCell->Next;  /* Bypass deleted cell */
            free(TmpCell);
        }
    }
    
    /* If X is not found, then Next field of returned value is NULL */
    /* Assumes a header */
    Position FindPrevious(ElementType X, List L)
    {
        Position P;
    
        P = L;
        while (P->Next != NULL && P->Next->Element != X)
            P = P->Next;
    
        return P;
    }
    
    /* Insert (after legal position P) */
    /* Header implementation assumed */
    /* Parameter L is unused in this implementation */
    void Insert(ElementType X, List L, Position P)
    {
        Position TmpCell;
    
        TmpCell = malloc(sizeof(struct Node));
        if (TmpCell == NULL)
            FatalError("Out of space!!!");
    
        TmpCell->Element = X;
        TmpCell->Next = P->Next;
        P->Next = TmpCell;
    }
    
    /* Correct DeleteList algorithm */
    void DeleteList(List L)
    {
        Position P, Tmp;
    
        P = L->Next;  /* Header assumed */
        L->Next = NULL;
        while (P != NULL)
        {
            Tmp = P->Next;
            free(P);
            P = Tmp;
        }
    }
    
    Position Header(List L)
    {
        return L;
    }
    
    Position First(List L)
    {
        return L->Next;
    }
    
    Position Advance(Position P)
    {
        return P->Next;
    }
    
    ElementType Retrieve(Position P)
    {
        return P->Element;
    }
    

    testlinkedlist.c

    #include <stdio.h>
    #include "linkedlist.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));
            } 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) == NULL)
                printf("Element %d Find fails
    ", i);
    
        printf("Finished deletions
    ");
    
        PrintList(L);
    
        DeleteList(L);
    
        return 0;
    }
    

    函数调用关系图(Call graph)

  • 相关阅读:
    为蓝桥杯准备
    Java里子类调用父类构造方法问题
    boost库的Singleton的实现以及static成员的初始化问题
    VS2005调试小记
    <转载>程序员每天该做的事
    vs2005, 2008 使用了未初始化的msg变量
    转载 vs2005 快捷键大全
    VS2005右键点击转到定义后出现“未定义符号”的提示及其解决
    软件工程配置规范(VC2005) 第二版(转载)
    匆忙赶路的时候别忘了适时停下来回头看看
  • 原文地址:https://www.cnblogs.com/typewriter/p/6047827.html
Copyright © 2011-2022 走看看