zoukankan      html  css  js  c++  java
  • 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构算法分析——C语言描述 第三章的单链表

    很基础的东西。走一遍流程。有人说学编程最简单最笨的方法就是把书上的代码敲一遍。这个我是头文件是照抄的。.c源文件自己实现。

    list.h

    [cpp] view plain copy
     
    1. typedef int ElementType;  
    2. #ifndef _List_H  
    3. #define _List_H  
    4.   
    5. struct Node;  
    6. typedef struct Node *PtrToNode;  
    7. typedef PtrToNode List;  
    8. typedef PtrToNode Position;  
    9.   
    10. List CreatList();  
    11. List MakeEmpty(List L);  
    12. int IsEmpty(List L);  
    13. int IsLast(Position P, List L);  
    14. Position Find(ElementType X, List L);  
    15. void Delete(ElementType X, List L);  
    16. Position FindPrevious(ElementType X, List L);  
    17. void Insert(ElementType X, Position P);  
    18. void DeleteList(List L);  
    19. Position Header(List L);  
    20. Position First(List L);  
    21. Position Advance(Position P);  
    22. ElementType Retrieve(Position P);  
    23.   
    24. #endif  



    list.c

    [cpp] view plain copy
     
    1. #include"list.h"  
    2. #include<stdlib.h>  
    3. #include"fatal.h"  
    4.   
    5. struct  Node  
    6. {  
    7.     ElementType Element;  
    8.     Position Next;  
    9. };  
    10.   
    11. List CreatList() {  
    12.     List l = malloc(sizeof(struct Node));  
    13.     if (l == NULL)  
    14.         Error("out of memory");  
    15.     l->Next = NULL;  
    16.     return l;  
    17. }  
    18.   
    19. List MakeEmpty(List L) {  
    20.     if (L != NULL)  
    21.         DeleteList(L);  
    22.     L = malloc(sizeof(struct Node));  
    23.     if (L == NULL)  
    24.         FatalError("Out of memory");  
    25.     L->Next = NULL;  
    26.     return L;  
    27. }  
    28.   
    29. int IsEmpty(List L) {  
    30.     return L->Next == NULL;  
    31. }  
    32.   
    33. int IsLast(Position P, List L) {  
    34.     return P->Next == NULL;  
    35. }  
    36.   
    37. Position Find(ElementType X, List L) {  
    38.     Position P;  
    39.     P = L->Next;  
    40.     while (P != NULL&&P->Element != X)  
    41.     {  
    42.         P = P->Next;  
    43.     }  
    44.     return P;  
    45. }  
    46.   
    47. void Delete(ElementType X, List L) {  
    48.     Position P;  
    49.     P = FindPrevious(X, L);  
    50.     if (!IsLast(P, L)) {  
    51.         Position TmpCell = P->Next;  
    52.         P->Next = TmpCell->Next;  
    53.         free(TmpCell);  
    54.     }  
    55. }  
    56.   
    57. Position FindPrevious(ElementType X, List L) {  
    58.     Position P;  
    59.     P = L;  
    60.     while (P->Next != NULL&&P->Next->Element != X)  
    61.         P = P->Next;  
    62.     return P;  
    63. }  
    64.   
    65. void Insert(ElementType X, Position P) {  
    66.     Position tmpCell;  
    67.     tmpCell = malloc(sizeof(struct Node));  
    68.     if (tmpCell == NULL)  
    69.         FatalError("Out of space!!");  
    70.     tmpCell->Element = X;  
    71.     tmpCell->Next = P->Next;  
    72.     P->Next = tmpCell;  
    73. }  
    74.   
    75. void DeleteList(List L) {  
    76.     Position p;  
    77.     p = L->Next;  
    78.     L->Next = NULL;  
    79.     while (p != NULL){  
    80.         Position tmp;  
    81.         tmp = p->Next;  
    82.         free(p);  
    83.         p = tmp;  
    84.     }  
    85. }  
    86.   
    87. Position Header(List L) {  
    88.     return L;  
    89. }  
    90.   
    91. Position First(List L) {  
    92.     return L->Next;  
    93. }  
    94.   
    95. Position Advance(Position P) {  
    96.     return P->Next;  
    97. }  
    98.   
    99. ElementType Retrieve(Position P) {  
    100.     return P->Element;  
    101. }  



    fatal.h

    [cpp] view plain copy
     
      1. #include<stdio.h>  
      2. #include<stdlib.h>  
      3.   
      4. #define Error(Str) FatalError(Str)  
      5. #define FatalError(Str) fprintf(stderr,"%s ",Str),exit(1)  
  • 相关阅读:
    【Linux开发】Linux下jpeglib库的安装详解
    【Linux开发】Linux下jpeglib库的安装详解
    【Linux开发】jpeglib使用指南
    【Linux开发】jpeglib使用指南
    【Linux开发】为qt-embedded添加jpeg库的交叉编译方法for arm
    【Linux开发】为qt-embedded添加jpeg库的交叉编译方法for arm
    Windows 7 64bit上安装Oracle Database 12c [INS-30131] 错误的解决方法
    Log4j 日志记录
    如何根据Ip获取地址信息--Java----待整理完善!!!
    Struts如何获取客户端ip地址
  • 原文地址:https://www.cnblogs.com/aituming/p/6131702.html
Copyright © 2011-2022 走看看