zoukankan      html  css  js  c++  java
  • 数据结构单向链表

    单向链表主要操作有:

    判断链表为空、查找、删除元素、插入元素、找前一个元素、删除链表...

    需要分清楚链表的单元结构
    List, Position 都是 Node, 但 List 只是入口 Node, 如果 List 中的指针为空链表就为空, 其他节点都是 Posit
     1 /* List.h */
     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 typedef int ElementType;
    10 
    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, List L, Position P);
    18 void DeletList(List L);
    19 
    20 struct Node
    21 {
    22     ElementType Element;
    23     Position Next;
    24 };
    25 
    26 #endif
     1 /* List.cpp */
     2 #include "List.h"
     3 #include <stdlib.h>
     4 #include <stdio.h>
     5 
     6 // Return true if L is empty, 测试链表是否为空表
     7 int IsEmpty(List L)
     8 {
     9     return L->Next == NULL;
    10 }
    11 
    12 // Return true if P is the last position in list L
    13 // Parameter L is unused in this implementation
    14 // 测试当前位置是否是链表末尾
    15 int IsLast(Position P, List L)
    16 {
    17     return P->Next == NULL;
    18 }
    19 
    20 // Return Position of X in L; NULL if not found
    21 Position Find(ElementType X, List L)
    22 {
    23     Position P;
    24     P = L->Next;
    25     while(P != NULL && P->Element != X)
    26         P = P->Next;
    27 
    28     return P;
    29 }
    30 
    31 void Delete(ElementType X, List L)
    32 {
    33     Position P, TmpCell;
    34 
    35     P = FindPrevious(X, L);
    36 
    37     if (!IsLast(P, L))
    38     {
    39         TmpCell = P->Next;
    40         P->Next = TmpCell->Next;  //P->Next->Next;
    41         free(TmpCell);
    42     }
    43 }
    44 Position FindPrevious(ElementType X, List L)
    45 {
    46     Position P;
    47 
    48     P = L;
    49     while(P->Next != NULL && P->Next->Element != X)
    50         P = P->Next;
    51 
    52     return P;
    53 }
    54 
    55 // Insert (after legal position)
    56 // Header implementation assumed
    57 // Parameter L is unused in this implementation
    58 void Insert(ElementType X, List L, Position P)
    59 {
    60     Position TmpCell;
    61 
    62     TmpCell = (struct Node*)malloc(sizeof(struct Node));
    63     if (TmpCell == NULL)
    64         printf("Fatal Error: Out of space!!");
    65 
    66     TmpCell->Element = X;
    67     TmpCell->Next = P->Next;
    68     P->Next = TmpCell;
    69 }
    70 
    71 void DeleteList(List L)
    72 {
    73     Position P, Tmp;
    74 
    75     P = L->Next;
    76     L->Next = NULL; // Header assumed
    77     while(P != NULL)
    78     {
    79         Tmp = P->Next;
    80         free(P);
    81         P = Tmp;
    82     }
    83 }

     参考:

  • 相关阅读:
    回调函数
    箭头函数和普通函数的区别?
    实现 (5).add(3).minus(2),使其输出结果为:6
    call 和 apply 的区别?哪个性能更好?
    用 JS 的 RegExp 进行正则判断,字符串内是否有特殊符号
    如何理解和描述“原型和原型链”
    JS 的防抖和节流
    Linux 服务器 关闭防火墙命令
    前端在Linux服务器搭建安装Nginx的步骤记录
    记录一些CSS的实用技巧
  • 原文地址:https://www.cnblogs.com/nobodyzhou/p/5459237.html
Copyright © 2011-2022 走看看